-
-
Save fredrikpaues/2251091 to your computer and use it in GitHub Desktop.
Stock trading
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
10 | |
9 | |
94 66 53 82 87 89 69 24 56 | |
6 | |
65 80 74 98 1 15 | |
5 | |
3 72 72 95 63 | |
6 | |
7 74 34 81 100 5 | |
7 | |
34 65 16 79 66 16 28 | |
9 | |
41 42 62 31 49 24 68 3 30 | |
9 | |
94 70 86 9 43 87 65 6 80 | |
4 | |
35 84 31 26 | |
5 | |
58 31 84 77 42 | |
10 | |
2 21 93 16 20 20 10 67 32 8 |
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
100 | |
89 | |
138 | |
204 | |
134 | |
186 | |
229 | |
49 | |
79 | |
365 |
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
import sys | |
def main(): | |
if type(sys.stdin).__name__!='file': | |
sys.exit() | |
else: | |
data=[] | |
for line in sys.stdin: | |
data.append(line.strip()) | |
cases=int(data[0]) | |
data=data[1:] | |
if len(data)%2!=0|len(data)/2!=cases: | |
sys.exit() | |
else: | |
for evenrow in range(0,len(data)-1,2): | |
process(data[evenrow],map(int,data[evenrow+1].split(' '))) | |
def process(days,prices): | |
if days<2: | |
print 0 | |
else: | |
indices=range(len(prices)) | |
indices.sort(lambda x,y:prices[y]-prices[x]) | |
total_profit=0 | |
i=0 | |
for j in indices: | |
if j>i: | |
total_profit+=calculate_profit(prices[i:j+1]) | |
i=max(i,j+1) | |
if i==len(prices): | |
break | |
print total_profit | |
def calculate_profit(prices): | |
revenue=(len(prices)-1)*prices[-1] | |
cost=sum(prices[:len(prices)-1]) | |
return revenue-cost | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment