Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python Recursive function

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Python recursive function". In daily operation, I believe many people have doubts about how to use Python recursive function. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt about "how to use Python recursive function". Next, please follow the editor to study!

1. Recursive function # Recursive function "" Recursive function: call your own function, called Recursive function Recursion: def digui (n): print (n, ") if n > 0: digui (nMuk 1) print (n,") digui (5) "" process n = 5 print (5) "") if 5 > 0: digui (5-1) = > digui (4) code blocking on line 12 n = 4 print (4, ") if 4 > 0: digui (4-1) = > digui (3) code blocking on line 12 n = 3 print (3,") if 3 > 0: digui (3-1) = > digui (2) code blocking on line 12 n = 2 print (2) "") if 2 > 0: digui (2-1) = > digui (1) Code blocking on line 12 n = 1 print (1, ") if 1 > 0: digui (1-1) = > digui (0) Code blocking on line 12 n = 0 print (0,") if 0 > 0: not valid print (0) ") to the last level of function space, the process of complete execution # return to the position of the upper level function space n = 1 code at line 12, continue to execute print (1,") back to the position of the upper layer function space n = 2 code at line 12, and continue to execute print (2, ") back to the position of the upper level function space n = 3 code at line 12 Continue to execute print (3, ") back to the position of the upper function space n = 4 code at line 12, continue to execute print (4,") back to the position of the previous function space n = 5 code at line 12, continue to execute print (5, ") to the end of recursive function execution. Print 543210012345 "every time a function is called, it is necessary to open up a space in memory, called stack frame space, to summarize recursively with the code in the running function: (1) Recursion is actually a process of constantly opening up stack frame space and releasing stack frame space. Opening up is the process of going, and release is the process of returning. (2) when recursion triggers the process of recursion: 1. When the last layer of stack frame space execution ends, the return process is triggered. two。 When the return value of return is encountered, the current function is terminated and the return process is triggered. (3) Recursion cannot open up space indefinitely, which may cause memory overflow and blue screen crash, so the condition of jumping out must be given (if the number of layers of recursion is too large, it is not recommended) (4) the stack frame space opened up one by one. The data is independent and not shared with each other. "# Recursion cannot open up space"officially, the maximum default is 1000 layers." def deepfunc (): deepfunc () deepfunc ()

two。 Recursive exercise # 1. Use recursion to achieve factorial of any number n # common implementation # 5! = 5 * 4'3' 2'1n = 5total = 1for i in range (n < 0 -1): total * = iprint (total) # 12 recursive implementation of def jiecheng (n): if n 1 # jiecheng (2) = > jiecheng (1) * 2 = > 1 * jiecheng (3) = > jiecheng (2) * 3 = > 1 * 2 * jiecheng (4) = > jiecheng (3) * 4 = > 1 * 2 * 3 * jiecheng (5) = > jiecheng (4) * 5 = > 1 * 2 * 3 * 4 * 5print (jiecheng (4) 5) "" Code parsing: the process of going: n = 5 return jiecheng (nmur1) * n = > jiecheng (4) * 5n = 4 return jiecheng (nMul 1) * n = > jiecheng (3) * 4n = 3 return jiecheng (NMu1) * n = > jiecheng (2) * 3n = 2 return jiecheng (nMul) * n = > jiecheng (1) * 2n = 1 return 1 back process: n = 2 return jiecheng (1) * 2 = > 1 * 2n = 3 return jiecheng (2) * 3 = > 1 * 2 * 3n = 4 return jiecheng (3) * 4 = > 1 * 2 * 3 * 4n = 5 return jiecheng (4) * 5 = > 1 * 2 * 3 * 4 * 5 to the end of this program: return 1 * 2 * 3 * 4 * 5 "" print (") # 2. Use tail recursion to achieve factorial of any number "" where return is called and where it returns "call yourself, and return is not an operational expression, but the function itself" feature: tail recursion only opens up a space, will not open up infinitely, and calculates the final result in a space to return, which saves space. Some interpreters support tail recursive calls, but the cpython interpreter does not support writing at present: the values of all operations are calculated in the parameters of the function, and finally return the parameters of the operation. "" def jiecheng (njinendval): if n zip ([1pje 4je 7], [2pje 5je 8], [3je 6pje 9]) it = zip (* lst) print (list (it)) func = lambda n: zip (* [listvar [iit] for i in range (n)]) it = func (2) # convert the tuples inside into a list print (list (map (list,it) # 2. Frog jumps steps (Recursive implementation) A frog can jump one step at a time if it wants to jump n stories high Can you also jump two steps? how many ways does this frog jump up this n-level high step? n = 11 = > 1n = 22 = > 11 | 2n = 33 = > 111 | 1.2 | 21n = 45 = > 111 | 1.2 | 21 | 21 | 112 | 22n = 58 = > 1111 | 1 121 | 2 11 | 1 21 | 2 1 | 2 1 | 1 2 2 | 2 12 'def func (n): if n = = 1 or n = = 2: return n return func (NMel 1) + func (NMel 2) print (func (5)) # 3. Recursive reverse string "reverse 14235 to 53241" (recursive implementation) # move the following characters forward-strvar = "14235" # lst.append (5) # lst.append (3) # lst.append (2) # lst.append (4) # lst.append (1) # lth = the total length of the string lst to insert the list def func (lth Lst= []: if lth = = 0: return lst res = strvar[ lth-1] lst.append (res) return func (lth-1) lth = len (strvar) lst= func (lth) print (lst) # ['54th,' 34th, '44th,' 1'] print (".join (lst)) # abbreviated def func (lth) Lst= []: if lth = 0: return "" .join (lst) res = strvar[ lth-1] lst.append (res) return func (lth-1) print (func (lth)) # move the preceding characters back method 2 strvar = "14235" def func (strvar): if len (strvar) = = 1: return strvar Return func (strvar [1:]) + strvar [0] res = func (strvar) print (res) "" pass: return func (4235) + 1return func (235) + 4return func (35) + 2return func (5) + 3return 5: return func (5) + 3 = > 5 + 3return func (35) + 2 = > 5 + 3 + 2return func (35) + 4 = > 5 + 3 + 2 + 4return func (4235) + 1 = > 5 + 3 + 2 + 4 + 1return 5 + 3 + 2 + 4 + 1 "# 4. The Fibonacci sequence is realized by tail recursion to realize a 5while b = 0me 1I = 0n = I

< n: print(b) a,b = b,a+b i +=1a,b = 0,1n = 5while n >

0: print (b) return b return func (b) print (func (6)) so far, the study on "how to use Python recursive functions" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report