#### What is the output of this code snippet?

# Profit of a single # buying low and selling high def maximumProfit(A): m = 0; for i in range(0, len(A)): for j in range (i + 1, len(A)): m = max(m, A[j] - A[i]) return m # Ethereum daily prices in Dec 2017 ($) prices = [455,460,465,451,414,415,441] print(maximumProfit(prices))

This puzzle presents an algorithmic problem with practical value for stock market analysis. For instance, suppose you are trading the cryptocurrency Ethereum. How much profit in dollars can you make by buying low and selling high based on historical data?

The function `maximumProfit`

takes an input sequence A, e.g. a week of Ethereum prices in December 2017. It returns the largest profit of buying low and selling high.

The algorithm works as follows. It iterates over all sequence indices i, i.e., the buying points, and over all sequence indices `j>i`

, i.e., the selling points. For each buying/selling pair `(i,j)`

, it calculates the profit as the difference between the prices at the selling and the buying points, i.e., `A[j]-A[i]`

. The variable `profit`

maintains the largest possible profit: $27 on $414 invested capital.

Are you a master coder?

Test your skills now!

#### Related Video

#### Solution

27