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.
-
-
Save neizod/1925940 to your computer and use it in GitHub Desktop.
Stockbroker
This file contains hidden or 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
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]) |
This file contains hidden or 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
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. |
This file contains hidden or 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
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. |
This file contains hidden or 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
{ | |
"name": "stockbroker", | |
"description": "calculate for largest profit of trading if stockbroker can predict future price.", | |
"keywords": [ | |
"trading", | |
"optimization", | |
"problem" | |
] | |
} |
This file contains hidden or 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
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