How to write the Fibonacci Sequence?

前端 未结 30 2355
醉酒成梦
醉酒成梦 2020-11-22 00:32

I had originally coded the program wrongly. Instead of returning the Fibonacci numbers between a range (ie. startNumber 1, endNumber 20 should = only those numbers between 1

30条回答
  •  遇见更好的自我
    2020-11-22 01:16

    Another way of doing it:

    a,n=[0,1],10
    map(lambda i: reduce(lambda x,y: a.append(x+y),a[-2:]),range(n-2))
    

    Assigning list to 'a', assigning integer to 'n' Map and reduce are 2 of three most powerful functions in python. Here map is used just to iterate 'n-2' times. a[-2:] will get the last two elements of an array. a.append(x+y) will add the last two elements and will append to the array

提交回复
热议问题