Hackbright Code Challenges

Stock Prices: Solution

Stock Prices: Solution

Problem

Stock Prices

Whiteboard

Harder

Challenge

Medium

Concepts

Logic

Download

stock-solution.zip


stock.py
def best(prices):
    """"Given a list of prices, return the maximum profit.

    If no profit is possible, return 0.
    """

    # START SOLUTION

    max_diff = 0
    low_so_far = None

    for p in prices:

        if low_so_far is None or p < low_so_far:
            low_so_far = p

        diff = p - low_so_far

        if diff > max_diff:
            max_diff = diff

    return max_diff