def discount(rewards, impatience):
    """Sum the rewards, where far off rewards are devalued by impatience.

    For example, if rewards were [1, 1, 1, 1] and impatience was 0.5,
    returns: sum([1, 0.5, 0.25, 0.125])

    Args:
        rewards (list): list of rewards to accumulate
        impatience (float): factor to discount the rewards at
    """
    total, factor = 0, 1.0
    for r in rewards:
        total += r * factor
        factor *= impatience
    return total