Created
August 13, 2016 07:34
-
-
Save cyang-el/71dddb61b73431a53448c0c3c480545c to your computer and use it in GitHub Desktop.
given stock price change with time, find best buy and sell point
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
def find_max_profit(args): | |
_min = None | |
diff = 0 | |
buyPoint = 0 | |
sellPoint = 0 | |
for i in range(len(args)): | |
if not _min: | |
_min = args[i] | |
if args[i] <= _min: | |
_min = args[i] | |
buyPoint = i | |
if (args[i] - _min) > diff: | |
sellPoint = i | |
diff = args[i] - _min | |
return buyPoint, sellPoint, diff | |
def test_find_max_profit(): | |
args = [5, 5, 5, 2, 3, 1, 5, 5, 7, 1, 9] | |
assert find_max_profit(args) == (9, 10, 8) | |
test_find_max_profit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment