-1

So on internet they solve using:

from functools import reduce

fib = lambda n: reduce(lambda x, _: x+[x[-1]+x[-2]],range(n-2), [0, 1])

print(fib(5))

Fibonacci program

I don't understand what's the use of underscore _ and actually how this range(n-2) is working inside program

2
  • Refer to this post: stackoverflow.com/questions/23453007/… Commented Aug 2, 2022 at 3:37
  • 1
    _ is just a variable name. It is commonly used to indicate that you are ignoring a value. Commented Aug 2, 2022 at 4:01

1 Answer 1

1

fib = lambda n: ... creates a one liner lambda function and assigns it to fib. Now, fib is a callable like a function e.g. fib(5).

reduce accepts 3 params: reduce(function, iterable, [, initializer]).

lambda x, _: x+[x[-1]+x[-2]] is your function. It concats current x sequence to a single-element sequence of sum of last and second last elements in the sequence x (see table below).

range(n-2) is iterable which generates a sequence of numbers [0,1,2] for n=5.

[0, 1] is the initial sequence to start with i.e. first 2 fixed numbers in fib sequence.

_ is used as second argument of function which is ignored. It is a convention to use _ for ignored argument in a functional program. The second argument receives one value from iterable for each run i.e. 0, 1, & 2.

Dry run for n=5

--------------------------------------------------------------
range(n-2)            x    _    x+[x[-1]+x[-2]]         output
--------------------------------------------------------------
         0        [0,1]    0        [0,1]+[0+1]        [0,1,1]
         1      [0,1,1]    1      [0,1,1]+[1+1]      [0,1,1,2]
         2    [0,1,1,2]    2    [0,1,1,2]+[1+2]    [0,1,1,2,3]

Note: [0,1] + [0+1] is list concatenation of [0,1] and [1]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.