+ 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))
15 odpowiedzi
+ 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.
+ 4
does power(2,2) multiplied the two parameter and multiplied to the variable x?
+ 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?
+ 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)
+ 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.
+ 2
I got the answer 8 but I don't know why since it has already 2 parameter. I only know 1.
+ 2
Bhavik Mahalle i understand recursion when it has 1 parameter like factorial 5!. But in this example makes me hard to understand.
+ 2
This code is similar to yours, I hope it helps in understanding recursion.☺️
https://code.sololearn.com/cLCXUR62zR8S/?ref=app
+ 1
I used to also be confused by recursion. Download Thonny and run your code and debug
+ 1
Hi guys I'm new and i will be something in the future 🙂
+ 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
+ 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.
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)
0
看不懂
0
傻逼吗