concept
Discounted return
Finite discounted sum of rewards. Gamma lies in [0,1]; empty lists return zero. Teaching function for small finite numbers.
Formal Syntax & Structure
artificial-intelligence syntaxartificial-intelligence
discounted_return(rewards, gamma)Working Example
Example Snippetartificial-intelligence
def discounted_return(rewards, gamma): if not 0 <= gamma <= 1: raise ValueError("gamma must be in [0, 1]") total = 0 for reward in reversed(rewards): total = reward + gamma * total return total
print(discounted_return([2, 4], 0.5))print(discounted_return([-1, 5], 0.9))print(discounted_return([], 1))