Skip to content

Instantly share code, notes, and snippets.

@neizod
Forked from Python1Liners/LICENSE.txt
Created February 27, 2012 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/1925940 to your computer and use it in GitHub Desktop.
Save neizod/1925940 to your computer and use it in GitHub Desktop.
Stockbroker
sum(c[-1] - i if c[-1] > i else c.append(i) or 0 for c in [[0]] for i in [int(v) for v in input().split()][::-1])

Stockbroker

Assume a stockbroker can predict exactly price of stock for days. How much can he make profit? If in any day he can buy one stock, sell some stocks he had, or do nothing.

sum(c[-1] - i if c[-1] > i else c.append(i) or 0 # return profit each day.
for c in [[0]] # store day of the largest price.
for i in [int(v) for v in input().split()][::-1]) # reversed list of price each day.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Nattawut Phetmak <http://about.me/neizod>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "stockbroker",
"description": "calculate for largest profit of trading if stockbroker can predict future price.",
"keywords": [
"trading",
"optimization",
"problem"
]
}
profit = sum(c[-1] - i if c[-1] > i else c.append(i) or 0 for c in [[0]] for i in [int(v) for v in input().split()][::-1])
# stdin:
# 4 23 42 8 16 15
expected = 65
print("expected value: " + expected)
print("actual output: " + profit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment