1. recursion:
Own tune their own difficulties: bad think, need to find a good read law
2. dichotomy
Head end, take the middle, left and right constantly changing, the indirect intermediate change, query efficiency is very high
1. Recursive (method :)
# Recursive use: traverse the tree structure (data take a traversing, two results obtained during the traverse, ... four results obtained, which is the tree structure) Import OS filePath = " D: / Untitled " IT = os.listdir (filePath) # view the files in the folder, # Print (IT) # print in the form of a list of Print ( " __iter__ " in dir (IT)) # determine whether the iteration, the list of which can be iterative, returns True for I in IT: Print (I) # direct printing, not in the form of a list Import OS filePath = " D: / Untitled " DEF Read (filePath, n-): IT = os.listdir (filePath) # listdir view (open) file folder # Print ( "__ iter__" in dir (IT)) # iterables # Print ( "__ next__" in dir (IT)) is not an iterator for I in IT: iF os.path.isdir (os.path.join (filePath, I)): # isdir determines whether folder os.path.join fixed wording (splicing) Print ( " \ T " * n-, I) # print folder, # Print (I) # this function is called again # Read (I) Read (the os.path.join (filePath, I), n-+. 1) # get the path (the path with the original into + files add a layer to form a new path) the else : #Regular files, not folders of Print ( " \ t " * the n-, i) # exit recursive, recursive once, n is judged recursive number of the Read (filePath, 0) # explanation: # is to put a folder open, determine whether the file, if the emergence of another folder, and then stitching, the new path appears # If you then open the folder or file, then continue stitching until the emergence of file, print out the contents.
2. Recursive (Method II :)
Import OS filePath = " D: / Untitled " DEF Read (filePath, n-): # n-recursion depth IT = the os.listdir (filePath) # the listdir view (open) in the folder # Print ( "__ iter__" in the dir ( IT)) # iterables for I in IT: # open layer # get path FP = the os.path.join (filePath, I) # splicing iF os.path.isdir (FP): # determines whether folder Print ( " \ T " * n-, I) Read (FP, n- + 1'd) # Is a folder, and continue to read the contents of internal recursive entrance the else : Print ( " \ t " * the n-, i) # Recursive export the Read (filePath, 1)
3. binary search
# Method a: (general method) LST = [11,22,33,44,55,66,77,88,99] # // floor except n-66 = left = 0 right = len (LST) -. 1 # length --1 index = COUNT. 1 = the while left <= right: Middle = (left + right) // 2 IF n-> LST [Middle]: left = Middle +. 1 elif n-< LST [Middle]: right = Middle --1 the else : Print (COUNT) Print ( " presence " ) Print (Middle) # Middle intermediate index is BREAK COUNT = COUNT +. 1 the else : Print ( " not present " ) # Method two: (recursive bisection) LST = [11,22,33,44,55,66,77, 88,99 ] DEF binary_search (left, right, n-): Middle = (left + right) // 2 IF left> right: Print ( " no " ) return -1 # is not returned -1 IF n-> LST [Middle ]: left = Middle +. 1 elif n-< LST [Middle]: right = Middle -. 1 the else : return Middle # yes, returns the index value of the input return binary_search (left, right, n-) # n-represents an index value you entered the value judgment print (binary_search (0, len (lst ) -1,65))