Programming performance/RaymondH Python
Jump to navigation
Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This took about 20 minutes to write with plain vanilla Python,
<code-python> data = [] for line in open('gspc.txt'):
if not line.startswith('#'): data.append(float(line.split()[-1]))
data.reverse()
purchases = {} # price --> quantity at that price cash = 10000.00 for prev, close in zip(data, data[1:]):
change = close / prev if change <= 0.97: invest = cash * 0.10 cash -= invest purchases[close] = purchases.get(close, 0) + invest / close for pprice, qty in purchases.items(): if close >= pprice * 1.06: cash += qty * close del purchases[pprice]
cash += sum(purchases.values()) * close print cash </code-python>