Hướng dẫn thử thách
1 / 1
Recursion Review
- Recursion is a programming concept that allows you to call a function repeatedly until a base case is reached.
Here is an example of a recursive function that calculates the factorial of a number:
```py
def find_factorial(n):
if n == 0:
return 1
return n * find_factorial(n - 1)
```
In the above example, the `find_factorial` function is called recursively until `n` reaches `0`. When `n` is `0`, the base case is reached and the function returns `1`. The function then returns the product of `n` and the result of the recursive call to `find_factorial(n - 1)`.
- Recursion allows you to handle something with an unknown depth, such as deeply nested dictionaries and lists, or a file tree.
- A call stack is used to keep track of the function calls in a recursive function. Each time a function is called, it is added to the call stack. When the base case is reached, the function calls are removed from the stack.
- You should carefully define the base case. Otherwise, the function can keep calling itself until Python raises a `RecursionError`.
- Recursion is useful for solving mathematical problems such as factorial and Fibonacci, traversing trees and graphs, and generating permutations and combinations.
Nhiệm vụ của bạn
Review the Recursion topics and concepts.
Vượt qua bài kiểm tra hiện tại để mở khóa bài tiếp theo.
main.py
UTF-8 • Tab Size: 2Kiểm tra bài:⌘↵