+ 2

Fibonacci sequence from Python Core

Hi brilliant people of Sololearn! I've not had a question for some time... I thought I was doing well. I've solved the task but I had to search for some direction. I feel that the task and the provided code (see my attachment) are either misleading, if the solution HAS to be solved in the way I have solved it or we've all missed something about recursion (and I'm still missing it). The task is to accept input of sequence lines and, using recursion, output the Fibonacci sequence until that value. The attached code shows the default code for the task and my main issue is the last line: fibonacci(num) I would like to solve it using this line as it is. I've also included other examples from the lesson that are all "clean" recursions - not using something like a 'for n in range(number)'. Can it be done? https://code.sololearn.com/coJrU8F7zz1d/?ref=app

7th Jan 2023, 9:08 PM
Ausgrindtube
Ausgrindtube - avatar
15 Answers
+ 6
We could pass additional arguments next to n to the function which we can use as helper variables. https://code.sololearn.com/cw8ZvQlKskjn/?ref=app
7th Jan 2023, 9:55 PM
Lisa
Lisa - avatar
+ 4
Comparison with the factorial is a little misleading because in factorial code, a single value must be calculated recursively, but in this fibonacci task a sequence of values is required. One way to do it is to create an inner function inside fibonacci() which is just a pure recursion and doesn't print anything. Then you can still do the printing in the outer function. https://code.sololearn.com/cptbuW73uqLO/?ref=app
27th Mar 2023, 6:04 AM
Tibor Santa
Tibor Santa - avatar
+ 3
Would you like the recursive function to return a list?
7th Jan 2023, 9:22 PM
Lisa
Lisa - avatar
+ 2
That could be it. It doesn't look too pretty 😄 I think I'm just being pedantic and I "really want" it to look like this factorial recursion code. def factorial(x): if x == 1: return 1 else: return x * factorial(x-1) print(factorial(5))
7th Jan 2023, 10:06 PM
Ausgrindtube
Ausgrindtube - avatar
+ 2
num = int(input()) def fibonacci(n): if n <= 1: return n else: return (fibonacci(n-1) + fibonacci(n-2)) for i in range(num): print(fibonacci(i)) https://code.sololearn.com/cEH5qfxjUoLg/?ref=app
9th Jan 2023, 3:15 PM
José Ignacio Espadas Prieto
José Ignacio Espadas Prieto - avatar
+ 1
I don't believe it should be a list. The task asks for the output format as: 0 1 1 2 3 5 8 13 And so on.
7th Jan 2023, 9:27 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
When you mean to print the numbers directly, how would you do that?
7th Jan 2023, 9:41 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
Here's my thought process (which doesn't work), using the lesson and the default code as provided. num = int(input()) def fibonacci(n): #complete the recursive function if n<=1: print(n) else: print(fibonacci(n-1) + fibonacci(n-2)) fibonacci(num)
7th Jan 2023, 9:43 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
You also solved it Rajat Goel but you didn't do anything like recursion in your solution. Your solution is similar to the Java ones I found in my search. It's very clean and well done.
10th Jan 2023, 7:34 AM
Ausgrindtube
Ausgrindtube - avatar
0
Well, you could print the list with unpacking if you want to avoid a for-loop. You could also write the function to print the numbers directly?
7th Jan 2023, 9:31 PM
Lisa
Lisa - avatar
0
Idk if this is what you want but this is what I did for it ___https://www.sololearn.com/compiler-playground/cIwLAAt6Rvn7___ i hope this helps but i will come back every 2 days to check on it
9th Jan 2023, 11:54 AM
Solo-Rusty
Solo-Rusty - avatar
0
Hi Ethan, thanks for your effort. Your code looks good but doesn't output the sequence as required. Look above for how it should come out.
9th Jan 2023, 1:27 PM
Ausgrindtube
Ausgrindtube - avatar
0
Well done Nacho Espadas. I also have that solution in the code posted above. My post is in search of another solution.
9th Jan 2023, 6:18 PM
Ausgrindtube
Ausgrindtube - avatar
9th Jan 2023, 7:33 PM
Rajat Goel
Rajat Goel - avatar
0
x = int(input()) def fib(x): if x<=1: return x else: return fib(x-1) + fib(x-2) for n in range (0,x+1): print(fib(n)) fib = lambda x: x if x <= 1 else fib(x - 1) + fib(x - 2) for n in range (0,x+1): print(fib(n))
19th Jan 2023, 9:31 AM
Jonathan Brooks
Jonathan Brooks - avatar