Jan-08-2020, 04:58 PM
In the spirit of this being homework, here is code that almost works. Stores in intermediate variable (output) a list of prior values.
The bug for you to fix is that it does not make the final calculation.
The bug for you to fix is that it does not make the final calculation.
def _factorial_sample_(n,output): if n == 0 or n == 1: output.append(1) return n,output else: x, output = _factorial_sample_(n-1,output) new_val = x * output[-1] output.append(new_val) print(output) return n,output print(_factorial_sample_(6,[]))
Output:[1, 1]
[1, 1, 2]
[1, 1, 2, 6]
[1, 1, 2, 6, 24]
[1, 1, 2, 6, 24, 120]
(6, [1, 1, 2, 6, 24, 120])