Skip to content

Instantly share code, notes, and snippets.

@cyang-el
Created August 13, 2016 07:34
Show Gist options
  • Save cyang-el/71dddb61b73431a53448c0c3c480545c to your computer and use it in GitHub Desktop.
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
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