recursion using reduce method

so, I saw someone using the reduce method to do recursion. here is his idea (1,0) , (1,1) , (2,1) , (3,2) , (5,3) which is corresponding 1,1,2,3,5,8,13,21 .......

and the code looks like this

def fib_reduce(n):
    initial =(1,0)
    dummy = range(n)
    fib_n = reduce(lambda prev ,b : (prev[0] + prev[1], prev[0]),
                   dummy,
                   initial)
    
    return fib_n[0]

I understand the (perve[0] + perve[1] , perv[0]) which is like a ,b = b , b+a however, I don't understand what is this 'b' stands for ?

May someone please explains this 'b'?



Read more here: https://stackoverflow.com/questions/64888397/recursion-using-reduce-method

Content Attribution

This content was originally published by Kevin Su at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: