-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
305f2e9
commit cd01ef9
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Question: Given a list of stock prices, find the maximum profit that can be made by buying | ||
# and selling one unit of stock | ||
|
||
|
||
def maxPrice(stocks): | ||
minUntilNow = stocks[0] | ||
maxProfit = 0 | ||
|
||
for i in range(1, len(stocks)): | ||
if stocks[i] < minUntilNow: | ||
minUntilNow = stocks[i] | ||
else: | ||
maxProfit = max(maxProfit, stocks[i] - minUntilNow) | ||
return maxProfit | ||
|
||
|
||
def expect(a, b): | ||
result = a == b | ||
print("Test for " + str(a) + (" passed!" if result else " failed!")) | ||
|
||
|
||
a = [310, 315, 275, 295, 260, 270, 290, 230, 255, 250] | ||
maxProfit = 30 | ||
|
||
expect(maxProfit, maxPrice(a)) |