I've heard it n times, but I'm not impressed. After reading this, you'll understand

Bite the apple and cry 2021-02-22 15:15:12
ve heard times impressed. impressed


I always feel tall when I hear recursion , Why? ? Because I'm not familiar with it , So let's write today to remember what recursion is .

But don't worry , Let's look at a problem : seek 10 The factorial (10!).

seek x The factorial , In fact, from 1 Start to multiply one by one to x. that 10 The factorial of is 1*2*3*4*5*6*7*8*9*10

One 、 Factorial in a non recursive way

If , We don't touch recursion , How to solve such a problem ?

The simplest and rudest way direct print(1*2*3*4*5*6*7*8*9*10) The result is OK , The result is 3628800.

But this is obviously not what we want , Then you can try for In a circular way .

def factorial(n):
"""
n It's the number of the required factorial
"""
result = n
for i in range(1, n):
result *= i
return result
if __name__ == '__main__':
print(factorial(10))

Two 、 Factorial recursion

1. What is recursion ?

I believe everyone must have heard such a story :

 Once upon a time there was a mountain , There are temples in the mountains , There is an old monk in the temple telling a story , What are you talking about ?
Once upon a time there was a mountain , There are temples in the mountains , There is an old monk in the temple telling a story , What are you talking about ?
Once upon a time there was a mountain , There are temples in the mountains , There is an old monk in the temple telling a story , What are you talking about ?
...

In fact, this is recursion , To put it bluntly , It's about quoting yourself .
that , Recursion is used in functions , It could look like this :

def factorial():
factorial()
if __name__ == '__main__':
factorial()

Calling function factorial When Continue calling... In the function factorial, Just like the story above , You can recurse endlessly ,
Until the old monk who told the story fainted , As well as the computer's memory overflow downtime .

however , One important point , Recursion is just a way to solve problems , Like factorial above , I use for It's the same as the loop .

2. Recursively solve factorial

If you want to solve the factorial problem above with recursion , You can learn more about the whole idea of recursion .

The whole idea of recursion is , Decompose a big problem into small ones , Until there's no way to break it down , therefore , Then solve the problem .
that , A recursive function has to satisfy 2 Conditions :

  • Baseline condition : The problem can be decomposed into the smallest problem , When the baseline conditions are met , Recursion is no longer going on
  • Recursive conditions : Continue to decompose the problem
    We can use this idea to try to solve the factorial problem recursively .
10! = 10 * 9! # 10 The factorial of is actually 10 * 9 The factorial
9! = 9 * 8! # 9 The factorial of is 9 * 8 The factorial
8! = 8 * 7!
...
2! = 2 * 1!
1! = 1

You can see , Finally, it's broken down to 1 You can't continue to decompose when you're in trouble , that 1 That's the baseline condition .

def factorial(n):
# Baseline condition , When satisfied , No more recursion
if n == 1:
return 1
# Recursive conditions , When n It's not equal to 1 when , Continue to recursive
return n * factorial(n - 1)
if __name__ == '__main__':
print(factorial(10))

3、 ... and 、 summary

  • recursive : It's just a way to solve problems , It doesn't have to be
  • Recursive functions : The function calls itself
  • Recursive 2 Conditions : Baseline condition ( If satisfied, there is no recursion )、 Recursive conditions ( If satisfied, the baseline recursion )
  • Recursion is like a loop : Basically, they can replace each other
  • It's easier to write loops , It's hard to read . Recursion is hard to write , But it's easy to read
版权声明
本文为[Bite the apple and cry]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210221221433715S.html

  1. Python中的解决中文字符编码的问题
  2. Solving the problem of Chinese character coding in Python
  3. Translation: practical Python Programming 02_ 01_ Datatypes
  4. Installation and use of Python and tensorflow in win10 environment (Python version 3.6, tensorflow version 1.6)
  5. Python series 46
  6. Linux安装Python3
  7. 【python接口自动化】- 正则用例参数化
  8. Python RestFul Api 设计
  9. filecmp --- 文件及目录的比较│Python标准库
  10. Installing python3 on Linux
  11. [Python] Matplotlib 圖表的繪製和美化技巧
  12. (資料科學學習手札108)Python+Dash快速web應用開發——靜態部件篇(上)
  13. 翻譯:《實用的Python程式設計》02_01_Datatypes
  14. 【python接口自动化】- 正则用例参数化
  15. 翻译:《实用的Python编程》02_02_Containers
  16. 两年Java,去字节跳动写Python和Go
  17. [Python interface automation] - regular use case parameterization
  18. Python restful API design
  19. 翻译:《实用的Python编程》02_02_Containers
  20. 两年Java,去字节跳动写Python和Go
  21. 翻译:《实用的Python编程》02_02_Containers
  22. Python基于粒子群优化的投资组合优化研究
  23. ubuntu部署django项目
  24. 兩年Java,去位元組跳動寫Python和Go
  25. 翻譯:《實用的Python程式設計》02_02_Containers
  26. 这样学习Python,爷爷都学会了!超简单Python入门
  27. [Python] 基于 jieba 的中文分词总结
  28. 【python】递归听了N次也没印象,读完这篇你就懂了
  29. [Python] 基于 jieba 的中文分词总结
  30. 人理解迭代,神则体会递归,从电影艺术到Python代码实现神的逆向思维模式
  31. [Python] 基於 jieba 的中文分詞總結
  32. Python属于后端开发还是前端开发?Python入门!
  33. 【python】递归听了N次也没印象,读完这篇你就懂了
  34. 一天快速入门python
  35. 学习Python对年龄有没有要求?30岁可以吗?
  36. 清华教授!12小时整理的最全Python教程(文末无偿分享)
  37. 使用Python开发DeFi项目
  38. python 函数详解
  39. Python工程师是做什么的?前景如何?
  40. Filecmp -- comparison of files and directories
  41. Python - zip() 函数
  42. 30 周年生日,Python 先驱是怎么评价这门语言的?
  43. Drawing and beautifying skills of [Python] Matplotlib chart
  44. Python + dash rapid web application development static components
  45. Translation: practical Python Programming 02_ 01_ Datatypes
  46. python将excel自适应导入数据库
  47. 从小白到大师,这里有一份Pandas入门指南
  48. [Python] 茎叶图和复合饼图的画法
  49. [Python interface automation] - regular use case parameterization
  50. Translation: practical Python Programming 02_ 02_ Containers
  51. Two years of Java, to write Python and go
  52. Translation: practical Python Programming 02_ 02_ Containers
  53. Two years of Java, to write Python and go
  54. Python-geoplot 空间核密度估计图绘制
  55. Python-seaborn 经济学人经典图表仿制
  56. python空间绘图- regionmask掩膜操作示例
  57. Python 空间绘图 - Cartopy 经纬度添加
  58. Python-pykrige包-克里金(Kriging)插值计算及可视化绘制
  59. Python 批量重采样、掩膜、坡度提取
  60. python - 多种交通方式可达圈分析