-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxProfit.py
More file actions
38 lines (31 loc) · 785 Bytes
/
Copy pathmaxProfit.py
File metadata and controls
38 lines (31 loc) · 785 Bytes
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
#Question3
#Algorithm to find max profit
#Create an array
stockPrice = list()
#Get user input for the array
i = 0
print("Enter '!' to end the array")
while True:
x = raw_input("Enter an integer: ")
if(x == "!"):
break
stockPrice.append(x)
i += 1
#calculate the maximum profit
minPrice = maxPrice = int(stockPrice[0])
for i in range(len(stockPrice)):
if stockPrice[i] < minPrice:
minPrice = stockPrice[i]
if maxPrice < stockPrice[i]:
maxPrice = stockPrice[i]
if maxPrice == stockPrice[0]:
profit = 0
else:
profit = int(maxPrice) - int(minPrice)
print "Maximum Profit = %d" % profit
#Input: [7,6,4,3,1]
#Output: Maximum Profit = 0
#Input: [1,2,4,5,8]
#Output: Maximum Profit = 7
#Input: [-1,2,4,8,1]
#Output: Maximum Profit = 9