W
W
weranda2016-08-02 11:52:19
Python
weranda, 2016-08-02 11:52:19

How to understand recursion in Python?

Greetings
I came across the problem of raising a number to a power through function recursion, but I can not understand the recursion algorithm.
The solution of the problem:

def rec(a,b):
    if b == 0:
        return 1
    else:
        return a * rec(a, b-1)

a, b = 2, 3
print(rec(a,b))

In the function in the else condition , a is evaluated to the required degree, with each recursion , b is reduced by one and, as a result, equates to zero. When b is zero, the first condition is met ( return 1 ), but that condition returns one, not the number raised to the power. So how, then, is the degree, and not the unit, derived?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Gornostaev, 2016-08-02
@weranda

The unit is returned to the previous function call, where it is multiplied by a equal to two and returned to the previous call again. So until it reaches the first call, which will return the value of its print () function. To better represent this, mentally replace the function calls with its body

(
    if 3 == 0:
        return 1
    else:
        return 2 * (
            if 2 == 0:
                return 1
            else:
                return 2 * (
                    if 1 == 0:
                        return 1
                    else:
                        return 2 *  (
                            if 0 == 0:
                                return 1
                            else:
                                #Эта ветка никогда не выполнится
                        )
                )
        )
)

3
3dr1aN, 2016-08-02
@3dr1aN

How to understand recursion in Python?
here is the answer

D
Dmitry, 2016-08-02
@EvilsInterrupt

Before learning recursion, understand recursion first.
Recursion is a way of solving a problem by simplifying it to the point where the problem can already be taken and solved, and not simplified.
You have already been given an example with a degree:
2 ^ 2
It is known that if we simplify to:
2 ^ 0, then we should get the result 1
So, simplify the task of raising to a power until the current exponent becomes equal to 0.

def pow(num, n2):
1 if n2 == 0 else num * pow(num, n2-1)
And yes, it is useful to think recursively. Don't give up trying to figure it out.
For example, traversing trees is much easier recursively than iteratively.
Also, I recommend reading SICP. This book gives a clear understanding between two nuances about recursion that not every programmer knows about. Example problem : Write a recursive function that calculates the factorial iteratively. Once again I will pay attention to the wording: the function is recursive, but the calculation is iterative! All this is not nonsense and is quite logical, more details in SICP

I
igorzakhar, 2018-08-19
@igorzakhar

def listsum(numList):
   if len(numList) == 1:
        return numList[0]
   else:
        return numList[0] + listsum(numList[1:])

print(listsum([1,3,5,7,9]))

aliev.me/runestone/Recursion/CalculatingtheSumofaL...
Where there are code examples, click the "Show in Codelens" button and you can see the execution of the python code step by step.

M
Mr Hobot, 2016-08-02
@vashaaa

And what does python have to do with it?) Recursion is recursion, it's not a bun of any language, it's a mathematical concept. She is the same everywhere. What is the actual problem? The function calls itself. In your case, it will call itself on itself (yes, that sounds so-so), decrementing the variable b (your degree) until the "if b == 0:" test is true.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question