In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to realize the recursive function in Python. It is very detailed and has certain reference value. Friends who are interested must finish it.
1. What is a recursive function
I described how to create and call functions earlier. You know, functions can call other functions, but what may surprise you is that functions can also call themselves. If you haven't encountered this situation before, you may want to know what recursion means. In a nutshell, recursion means referencing (in this case, calling) itself.
2. Python recursive function
Here is a recursive function definition:
Def recursion ():
Return recursion ()
This definition obviously does nothing and is as stupid as the previous definition of "recursion". What will be the result if you run it? You will find that the program crashes (throws an exception) after running for a while. In theory, the program will continue to run, but it will consume some memory each time a function is called. Therefore, when the number of function calls reaches a certain extent (and the previous function calls do not return), all memory space will be exhausted, causing the program to terminate and display the error message "more than large recursive depth".
What you want is a recursive function that can help you, and such a recursive function usually contains the following two parts.
Baseline condition (for small problems): when this condition is met, the function will directly return a value.
Recursive condition: contains one or more calls designed to solve part of the problem.
The key here is that by breaking down the problem into smaller parts, you can avoid endless recursion, because the problem will eventually be broken down into small problems that can be solved by baseline conditions.
3. Python recursive function
So how do you get the function to call itself? It's not as hard to understand as it looks. As mentioned earlier, each time a function is called, a new namespace is created for this purpose. This means that when a function calls itself, it is two different functions (more accurately, different versions of the same function (that is, different namespaces) are communicating.
Classic case 1, calculate the factorial of the number n. The factorial of n is n × (nmur1) × (nmel2) × … × 1 is widely used in the field of mathematics.
Loops can be used. This implementation is feasible and straightforward.
Deffactorial (n):
Result = n
For i in range (1, n):
Result * = I
Return result
Let's consider how to use functions to implement this definition. After understanding this definition, it is actually very simple to implement. Def factorial (n):
If n = = 1:
Return 1
Else:
Return n * factorial (n-1)
This is a direct implementation of the previous definition, but don't forget that function calls factorial (n) and factorial (nMel 1) are different entities.
Classic case 2. Calculate a power, just as the built-in function pow and the operator * * do. There are many ways to define the integer power of a number, but let's first look at a simple definition: power (x, n) (the n power of x) is the result of multiplying the number x by n-1 times, that is, the result of multiplying n x. In other words, power (2,3) is the result of two self-multiplications, that is, 2 × 2 × 2 = 8.
This is easy to achieve.
Def power (x, n):
Result = 1
For i in range (n):
Result * = x
Return result this is a very simple small function, but you can also change the definition to recursive.
For any number x _ power (x, 0) is 1. When n > 0, power is the product of power and x. This definition provides exactly the same results as the simpler iteration definition. It is difficult to understand the definition, but it is easy to implement.
Def power (x, n):
If n = = 0:
Return1
Else:
Return x * power (x, n-1)
4. Why use recursive functions
In most cases, using loops may be more efficient. However, in many cases, using recursion is more readable and sometimes much higher, especially if you understand the recursive definition of a function. In addition, although you can completely avoid writing recursive functions, as a programmer, you must be able to read recursive algorithms and functions written by others.
These are all the contents of the article "how to implement Recursive functions in Python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.