[CentralOH] Anonymous Solution
deeppunster at gmail.com
Thu Mar 7 20:05:22 EST 2019
Anonymous solution to the following problem:
Given a array of numbers representing the stock prices of a company in
chronological order, write a function that calculates the maximum profit you
could have made from buying and selling that stock once. You must buy
before you can sell it.
For example, given [9, 11, 8, 5, 7, 10], you should return 5, since you
could buy the stock at 5 dollars and sell it at 10 dollars.
This solution accepts the array on th command line.
return list(map(int, args[1:]))
maximum_profit = 0
maximum_price = None
for price in reversed(prices):
if maximum_price is None or price > maximum_price:
maximum_price = price
profit = maximum_price - price
if profit > maximum_profit:
maximum_profit = profit
prices = get_prices_from_arguments(args)
maximum_profit = calculate_maximum_profit(prices)
if __name__ == '__main__':
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the CentralOH