process-oriented programming
The core is the process of the word, the process is to solve the problem steps, based on the process to design the program is like the design of an industrial assembly line, is a mechanical way of thinking
Advantages: The program structure clearly can simplify the complex problems, the process of
Cons: Poor scalability, a streamline is just to solve a problem
Application scenario:Linux kernel,git,httpd,Shell script
Exercise: Filtering files in the directory that contain the error file contents
Grep–rl ' ERROR '/dir
using the OS module walk method:
Os.walk will make a directory of two levels of directories and files into an iterator, multiple uses to implement file path stitching
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/9D/92/wKiom1mCAsCj8FMCAAFWif5hlXI802.png "title=" QQ picture 20170803004928.png "alt=" Wkiom1mcascj8fmcaafwif5hlxi802.png "/>
#grep -rl ' ERROR ' /dir/import osdef init (func): def Wrapper (*args,**kwargs): g=func (*args,**kwargs) next (g) return g return wrapper# First stage: Find the absolute path of all files @initdef search (target): while true: filepath=yield g=os.walk (filepath) for pardir,_,files in g: for file in files: Abspath=r '%s\%s ' % (pardir,file) target.Send (Abspath) #第二阶段: Open file @initdef opener (target): while true: abspath=yield with open ( Abspath, ' RB ') as f: Target.send ((abspath,f)) #第三阶段: Loop through each line of content @initdef cat (target): while true: abspath,f=yield # (abspath,f) for line in f: res=target.send ((abspath,line)) if res:break# Fourth Stage: Filter @initdef grep (pattern,target): tag=false while true: abspath,line=yield tag tag=false if pattern in line: target.send (Abspath) tag=true# Fifth stage: Print the name of the line that belongs to @initdef printer (): while True: abspath=yield print (Abspath) G = search (Opener ("Error". Encode (' Utf-8 '), printer ()))) G.send (R ' d:\python location\python36\day05\a '))
3. Recursion
Recursive invocation: The function itself is called directly or indirectly during a call to a function
Recursion in Python must be saved in the next recursive time, inefficient, no optimization means, so the recursive hierarchy is limited (other programming languages have a tail recursive method to optimize)
1. There must be a clear end condition
2. each time a deeper level of recursion is reached, the problem size should be reduced compared to the previous recursion
3. recursive efficiency is not high, too many recursive hierarchy will lead to stack overflow (in the computer, function calls through the stack (stack) This data structure implementation, each time into a function call, the stack will add a stack of frames, whenever the function returns, the stack will reduce the stack frame. Because the size of the stack is not infinite, there are too many recursive calls, which can cause the stack to overflow .
tail recursion optimization:http://egon09.blog.51cto.com/9161406/1842475
#直接def func (): Print (' from Func ') func () func () output: from Funcfrom func...from Functraceback (most recent call last): Fi Le "D:/python location/python36/day05/recursive. Py", line 8, in <module> func () [Previous Line repeated 993 more times] Recursionerror:maximum recursion depth exceeded while calling a Python object #调用Python对象时的最大递归深度超过了限制
If the recursion level is too large, it will be reported as above error
#间接def foo (): Print (' from Foo ') bar () def bar (): Print (' from Bar ') foo () foo () output: Recursionerror:maximum recurs Ion depth exceeded while calling a Python object #调用Python对象时的最大递归深度超过了限制
Modify recursion level limit (default )
>>> import sys>>> sys.getrecursionlimit () 1000>>> sys.setrecursionlimit (+) >> > Sys.getrecursionlimit () 2000
Practice:
Known:
Age (5) =age (4) +2
Age (4) =age (3) +2
Age (3) =age (2) +2
Age (2) =age (1) +2
Age (1) =18
First make a judgment:
Age (N) =age (n-1) +2 #n >1
Age (1) =18 #n =1
def age (N): if n = = 1:return (n-1) +2print (age (5))
Recursive execution is divided into two stages:
1 recursion
2 Backtracking
650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9D/92/wKioL1mCA-azEeaZAABbE7nPVcM799.png "title=" QQ picture 20170803005444.png "alt=" Wkiol1mca-azeeazaabbe7npvcm799.png "/>
Recursive and looping functions are similar, but recursion is appropriate when the number of loops is not known
Practice:
Take out all the elements in the list
L =[1, 2, [3, [4, 5, 6, [7, 8, [9, ten, [One, one,, [14,15,[16,[17,]],19]]]]] #def search (L): for item in L:if type (item) is List:search (item) else:print (item) search (L)
4. dichotomy
Method:
determine if a value exists in a particularly large list, if using the in method will traverse the list, taking up too much memory, using dichotomy to split the list each time, take up less memory
Practice:
#二分法l = [1,2,5,7,10,31,44,47,56,99,102,130,240]def binary_search (l,num): print (L) #[10, 31] if len (L) > 1: mid_index=len (l)//2 #1 if num > l[mid_index]: #in the right l=l[mid_index:] #l =[31] binary_search (l,num) elif num < l[mid_index]: #in the left l=l[:mid_index] binary_searcH (l,num) else: print (' find it ') else: if l[0] == num: print (' find it ') else: print (' not exist ') returnbinary_search (l,32)
This article is from the "Lyndon" blog, make sure to keep this source http://lyndon.blog.51cto.com/11474010/1953169
Python Basics---process-oriented programming