-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstock.py
55 lines (45 loc) · 1.49 KB
/
stock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def get_profit(prices):
max_profit = 0
count = 0
for i in range(len(prices)):
for j in range(len(prices)):
start = min(i, j)
end = max(i, j)
start_price = min(prices[i], prices[j])
end_price = max(prices[i], prices[j])
profit = end_price - start_price
max_profit = max(max_profit, profit)
count += 1
print ('Loop:', count)
return max_profit
def get_profit2(prices):
max_profit = 0
count = 0
for i in range(len(prices)):
for j in range(i+1, len(prices)):
start = min(i, j)
end = max(i, j)
start_price = min(prices[i], prices[j])
end_price = max(prices[i], prices[j])
profit = end_price - start_price
max_profit = max(max_profit, profit)
count += 1
print ('Loop:', count)
return max_profit
def get_profit3(prices):
if len(prices) < 2:
raise IndexError('Not enough information')
max_profit = prices[1] - prices[0]
count = 0
smallest = prices[0]
for i in range(1, len(prices)):
max_profit = max(max_profit, prices[i]-smallest)
smallest = min(smallest, prices[i])
count+=1
print ('Loop:', count)
return max_profit
stock_prices_yesterday = [10, 9]
print('Items in array: ', len(stock_prices_yesterday))
print(get_profit(stock_prices_yesterday))
print(get_profit2(stock_prices_yesterday))
print(get_profit3(stock_prices_yesterday))