Data Structure basics of Python: python Data Structure
I. Data Structure Basics
A. What is data structure?
B. Data Structure Classification
C. List
Import randomfrom timewrap import * def list_to_buckets (li, iteration): "": param li: List: param iteration: the number of iterations of the bucket: return: "buckets = [[] for _ in range (10)] for num in li: digit = (num // (10 ** iteration )) % 10 buckets [digit]. append (num) return bucketsdef buckets_to_list (buckets): return [num for bucket in buckets for num in bucket] # li = [] # for bucket in buckets: # for num in bucket: # li. append (num) @ cal_timedef radix_sort (li): maxval = max (li) #10000 it = 0 while 10 ** it <= maxval: li = buckets_to_list (list_to_buckets (li, it) it + = 1 return lili = [random. randint (100000) for _ in range ()] radix_sort (li)
List
D. Stack
Ii. Stack Python implementation
A. Stack application-Question of matching brackets
Def brace_match (s): stack = [] match = {')': '(', ']': '[', '}': '{'} match2 = {'(': ')', '[': ']', '{': '} for ch in s: if ch in {'(', '[', '{'}: stack. append (ch) elif len (stack) = 0: print ("% s missing" % match [ch]) return False elif stack [-1] = match [ch]: stack. pop () else: print ("Parentheses do not match") return False if len (stack)> 0: print ("% s missing" % (match2 [stack [-1]) return False return Truebrace_match ("[{() [] }{}{}")
Parentheses matching
B. Queue
C. Queue implementation
D. Queue implementation principle-circular queue
E. Queue implementation principle-circular queue
F. built-in queue modules
3. Stack application-questions about the maze
Solution
From collections import dequemaze =, 0, 1], [,], [,], [,],, 1, 1, 1, 1, 1, 1] dirs = [lambda x, y :( X-1, y), # lambda x, y :( x, y + 1 ), # Right lambda x, y :( x + 1, y), # lambda x, y :( x, Y-1), # Left] def solve_maze (x1, y1, x2, y2): stack = [] stack. append (x1, y1) maze [x1] [y1] = 2 while len (stack)> 0: # When the stack is not empty, cycle cur_node = stack [-1] if cur_node = (x2, y2): # reach the end point for p in stack: print (p) return True for dir in dirs: next_node = dir (* cur_node) if maze [next_node [0] [next_node [1] = 0: # locate a stack that can go. append (next_node) maze [next_node [0] [next_node [1] = 2 #2 indicates the previous vertex break else: # stack cannot be found in either direction. pop () else: print ("no path to go") return Falsedef solve_maze2 (x1, y1, x2, y2): queue = deque () path = [] # record the node queue after the departure. append (x1, y1,-1) maze [x1] [y1] = 2 while len (queue)> 0: cur_node = queue. popleft () path. append (cur_node) if cur_node [0] = x2 and cur_node [1] = y2: # To the end real_path = [] x, y, I = path [-1] real_path.append (x, y) while I> = 0: node = path [I] real_path.append (node [0: 2]) I = node [2] real_path.reverse () for p in real_path: print (p) return True for dir in dirs: next_node = dir (cur_node [0], cur_node [1]) if maze [next_node [0] [next_node [1] = 0: queue. append (next_node [0], next_node [1], len (path)-1 )) maze [next_node [0] [next_node [1] = 2 # mark as else: print ("no path to go") return Falsesolve_maze2)
Maze Problems
A. Queue applications
Def solve_maze2 (x1, y1, x2, y2): queue = deque () path = [] # record the queue of the node after the release. append (x1, y1,-1) maze [x1] [y1] = 2 while len (queue)> 0: cur_node = queue. popleft () path. append (cur_node) if cur_node [0] = x2 and cur_node [1] = y2: # To the end real_path = [] x, y, I = path [-1] real_path.append (x, y) while I> = 0: node = path [I] real_path.append (node [0: 2]) I = node [2] real_path.reverse () for p in real_path: print (p) return True for dir in dirs: next_node = dir (cur_node [0], cur_node [1]) if maze [next_node [0] [next_node [1] = 0: queue. append (next_node [0], next_node [1], len (path)-1 )) maze [next_node [0] [next_node [1] = 2 # mark as else: print ("no path to go") return Falsesolve_maze2)
Maze problem-queue implementation
Iv. Linked List
Import randomfrom timewrap import * def list_to_buckets (li, iteration): "": param li: List: param iteration: the number of iterations of the bucket: return: "buckets = [[] for _ in range (10)] for num in li: digit = (num // (10 ** iteration )) % 10 buckets [digit]. append (num) return bucketsdef buckets_to_list (buckets): return [num for bucket in buckets for num in bucket] # li = [] # for bucket in buckets: # for num in bucket: # li. append (num) @ cal_timedef radix_sort (li): maxval = max (li) #10000 it = 0 while 10 ** it <= maxval: li = buckets_to_list (list_to_buckets (li, it) it + = 1 return lili = [random. randint (100000) for _ in range ()] radix_sort (li)
List
Def insert_sort (li): for I in range (1, len (li )): # I indicates the first number of disordered zones; tmp = li [I] # The card j = I-1 # j points to the last position of the ordered area while li [j]> tmp and j> = 0: # cyclic termination conditions: 1. li [j] <= tmp; 2. j =-1 li [j + 1] = li [j] j-= 1 li [j + 1] = tmpdef shell_sort (li): d = len (li) // 2 while d> 0: for I in range (d, len (li )): tmp = li [I] j = I-d while li [j]> tmp and j> = 0: li [j + d] = li [j] j-= d li [j + d] = tmp d = d> 1
Exercise I -- insert
from timewrap import *@cal_timedef binary_search(li, val): low = 0 high = len(li) - 1 while low <= high: mid = (low + high) // 2 if li[mid] > val: high = mid - 1 elif li[mid] < val: low = mid + 1 else: return mid else: return -1def find_a(nums, target): low = 0 high = len(nums) - 1 while low <= high: mid = (low + high) // 2 if target <= nums[mid]: high = mid - 1 else: low = mid + 1 #[1, 2, 2, 2, 4, 8, 10] if low < len(nums): return low else: return -1def find_b(nums, target): low = 0 high = len(nums) - 1 while low <= high: mid = (low + high) // 2 if target < nums[mid]: high = mid - 1 else: low = mid + 1 if low < len(nums): return low else: return -1@cal_timedef linear_search(li, val): try: return li.index(val) except ValueError: return -1li = [1,2,2,2,4,8,10]print(find_a(li, 10))
View Code
Def insert_sort (li): for I in range (1, len (li )): # I indicates the first number of disordered zones; tmp = li [I] # The card j = I-1 # j points to the last position of the ordered area while li [j]> tmp and j> = 0: # cyclic termination conditions: 1. li [j] <= tmp; 2. j =-1 li [j + 1] = li [j] j-= 1 li [j + 1] = tmpdef shell_sort (li): d = len (li) // 2 while d> 0: for I in range (d, len (li )): tmp = li [I] j = I-d while li [j]> tmp and j> = 0: li [j + d] = li [j] j-= d li [j + d] = tmp d = d> 1
View Code