I've recently discovered how cool reduce()
could be and I want to do this:
>>> a = [1, 1] + [0] * 11
>>> count = 1
>>> def fib(x,n):
... global count
... r = x + n
... if count < len(a) - 1: a[count+1] = r
... count += 1
... return r
>>>
>>> reduce(fib,a,1)
610
>>> a
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
But this just looks so messy and almost defeats the purpose of the last line:
reduce(fib,a,1)
What would be a better way to use Python to make a Fibonacci number with reduce()
?