+ 4

Recursion in python

Can someone full explain this to me? I have a hard time to understand this yesterday def power(x, y): if y == 0: return 1 else: k = power(x,y-1) print(k) return x * k print("-->", power(2, 3))

8th Jul 2021, 8:43 AM
Kakai
Kakai - avatar
15 Respostas
+ 12
This is a solution to something like this 2^3 = 2 x 2 x 2 For recursion the aim is to divide the problem into sub problems until the sub problems (can be solved directly ie 2^1) which in this case, the sub problem is the base (2). You need to have an exit condition which for this case is y=0 because it 2^0 which gives 1 Execution of power(2,3) First function call: to power(2,3) -> 2*power(2, 2) power(2, 2) -> 2*power(2, 1) power(2, 1) -> 2*power(2, 0) power(2,0) -> 1 Substituting for each function call in the above execution will result in this: 2*2*2*1 To keep it simple, I have left certain things about how the stack is built coz this might make it look complex than it is. Hope this helps đŸ€—đŸ€—đŸ€— coz I know recursion isn't sth you take in at first glance.
8th Jul 2021, 9:02 AM
Kirabo Ibrahim
Kirabo Ibrahim - avatar
+ 4
does power(2,2) multiplied the two parameter and multiplied to the variable x?
8th Jul 2021, 9:19 AM
Kakai
Kakai - avatar
+ 3
Recursion basically similar to while loops. While y is not 0 do stuff. Power(2,3): print power(2,2): print power(2,1): print power(2,0): return 1 And what does your comment mean?
8th Jul 2021, 9:04 AM
Bhavik Mahalle
Bhavik Mahalle - avatar
+ 3
Kirabo Ibrahim does the fuction () returns a number and multiplied to the 2? Like... x * power(2, 3) will be 2 * power(2, 2)
8th Jul 2021, 9:18 AM
Kakai
Kakai - avatar
+ 3
Each function function call yields a 2 * another_function_call() where another_function_call() will yield 2 * another_ function_call() and so on till you reach when y is 0 and this will return 2 x 1. This is building the solution step by step. The current function call uses the value returned from the previous function call.
8th Jul 2021, 9:25 AM
Kirabo Ibrahim
Kirabo Ibrahim - avatar
+ 2
I got the answer 8 but I don't know why since it has already 2 parameter. I only know 1.
8th Jul 2021, 8:45 AM
Kakai
Kakai - avatar
+ 2
Bhavik Mahalle i understand recursion when it has 1 parameter like factorial 5!. But in this example makes me hard to understand.
8th Jul 2021, 9:15 AM
Kakai
Kakai - avatar
+ 2
This code is similar to yours, I hope it helps in understanding recursion.â˜ș https://code.sololearn.com/cLCXUR62zR8S/?ref=app
8th Jul 2021, 10:13 AM
Solo
Solo - avatar
+ 1
I used to also be confused by recursion. Download Thonny and run your code and debug
9th Jul 2021, 12:19 AM
Chuks AJ
Chuks AJ - avatar
+ 1
Hi guys I'm new and i will be something in the future 🙂
10th Jul 2021, 1:14 AM
Google s
+ 1
Here's a one-liner possibility: print("-->", (pow := lambda x, y: x * (k := pow(x, y-1)) * (print(k) is None) if y else 1)(2, 3)) A simpler approach would be to use the "**" operator. # Hope this helps
10th Jul 2021, 4:22 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
This is a solution to something like this 2^3 = 2 x 2 x 2 For recursion the aim is to divide the problem into sub problems until the sub problems (can be solved directly ie 2^1) which in this case, the sub problem is the base (2). You need to have an exit condition which for this case is y=0 because it 2^0 which gives 1 Execution of power(2,3) First function call: to power(2,3) -> 2*power(2, 2) power(2, 3) -> 2*power(2, 1) power(2, 5) -> 2*power(2, 0) power(2,0) -> 1 Substituting for each function call in the above execution will result in this: 2*2*3*5 To keep it simple, I have left certain things about how the stack is built coz this might make it look complex than it is. Hope this helps because I know recursion isn't something you take in at first glance.
10th Jul 2021, 8:29 AM
Joseph Haastrup
Joseph Haastrup - avatar
0
1. Add a <title> element to the head section and write the name of your CV inside it. 2. Write your first and last name in the body section. (Plz solve the problem)
10th Jul 2021, 12:16 AM
Bipul Kumar
Bipul Kumar - avatar
0
看䞍懂
10th Jul 2021, 7:07 AM
ć©· 王
0
ć‚»é€Œć—
10th Jul 2021, 7:12 AM
ć©· 王