Skip to content

Commit f3cbc5a

Browse files
authored
Added coderbyte array challenge
1 parent db77d72 commit f3cbc5a

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

stockPicker.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
Have the function StockPicker(arr) take the array of numbers stored
3+
in arr which will contain integers that represent the amount in dollars
4+
that a single stock is worth, and return the maximum profit that could
5+
have been made by buying stock on day x and selling stock on day y
6+
where y > x. For example: if arr is [44, 30, 24, 32, 35, 30, 40, 38,
7+
15] then your program should return 16 because at index 2 the stock
8+
was worth $24 and at index 6 the stock was then worth $40, so if you
9+
bought the stock at 24 and sold it at 40, you would have made a profit
10+
of $16, which is the maximum profit that could have been made with this
11+
list of stock prices.
12+
13+
If there is not profit that could have been made with the stock prices,
14+
then your program should return -1. For exmaple: arr is [10, 9, 8, 2]
15+
then your program should return -1.
16+
17+
Examples:
18+
19+
Input: [10, 12, 4, 5, 9]
20+
Output: 5
21+
22+
'''
23+
24+
def StockPicker(arr):
25+
cost = 0
26+
maxcost = 0
27+
28+
mini = arr[0]
29+
for i in range(len(arr)):
30+
mini = min(mini, arr[i])
31+
cost = arr[i] - mini
32+
maxcost = max(maxcost, cost)
33+
return maxcost
34+
35+
print(StockPicker([10, 12, 4, 5, 9])

0 commit comments

Comments
 (0)