-
冒泡排序
一般写法
def bubbleSort(my_list): length = len(my_list) for i in range(0,length): for j in range(i+1,length): if my_list[i] > my_list[j]: my_list[i],my_list[j] = my_list[j],my_list[i] return my_list
reduce写法
def bubble_Sort(my_list): new_list = [] while len(my_list)>0: ret = reduce(lambda x,y:y if x>y else x,my_list) new_list.append(ret) my_list.remove(ret) return new_list
-
斐波那契数列
def fb(n): new_list = [] for i in range(1,n+1): new_list.append(1) if i<3 else new_list.append(reduce(lambda x,y:x+y,[new_list[-2],new_list[-1]])) return new_list
-
二分查找/折半查找
def ef_match(my_list,value): length = len(my_list) left,right=0,length while left<right: middle = (left + right) // 2 if my_list[middle]>value:right=middle-1 elif my_list[middle]<value:left=middle+1 else:return middle return None
-
八皇后问题
def queen(A, cur=0): if cur == len(A):#如果组合完毕,打印结果返回 print(A) return 0 for col in range(len(A)):#每行遍历8个位置进行选择 A[cur], flag = col, True for row in range(cur):#检查前每行的位置是否与当前有冲突 if A[row] == col or abs(col - A[row]) == cur - row: #前面已选择的列=当前列 或者 (当前选择的值与第n行的差值 == 当前行与第n行的差值)即在对角线上 flag = False#标记失败 break#退出 if flag:#如果不冲突 queen(A, cur+1)#开始进行下一行的选择 queen([None]*8)
-
KMP
def kmp_next(string): my_mext= [0]*len(string) my_mext[0]=-1#特殊标记 now,select=0,-1 while now < len(string)-1: if select== -1 or string[now] ==string[select]: now += 1 select += 1 my_mext[now]=select else: select=my_mext[select] return my_mext def kmp_nextval(string): my_mext= [0]*len(string) my_mext[0]=-1#特殊标记 now,select=0,-1 while now < len(string)-1: if select== -1 or string[now] ==string[select]: now += 1 select += 1 if string[now] !=string[select]: my_mext[now]=select else: my_mext[now] = my_mext[select] else: select=my_mext[select] return my_mext def kmp_match(s,t): slen,tlen=len(s),len if slen >= tlen: i=j=0 next = kmp_next(t) while i< slen: if j==-1 or s[i]==t[j]: i+=1 j+=1 else: j =next[j] if j==tlen: return i-tlen return False if __name__ == '__main__': ret_next = kmp_next('abcabcaa') ret_nextavl = kmp_nextval('abcabcaa') print(ret_next) print(ret_nextavl)
-
二叉树遍历
#!/usr/bin/python3 #coding:utf-8 ''' Created on 2018/09/14 10:48 @author: 刘蒙华 ''' class Node(): def __init__(self, data=-1, lchild=None, rchild=None): self.data = data self.lchild = lchild self.rchild = rchild class Tree(): def __init__(self): self.root = Node() self.myQueue=[] def add(self, data): """为树添加节点""" node = Node(data) if self.root.data == -1: # 如果树是空的,则对根节点赋值 self.root = node self.myQueue.append(self.root) else: treeNode = self.myQueue[0] # 此结点的子树还没有齐。 if treeNode.lchild == None: treeNode.lchild = node self.myQueue.append(treeNode.lchild) else: treeNode.rchild = node self.myQueue.append(treeNode.rchild) self.myQueue.pop(0) # 如果该结点存在右子树,将此结点丢弃。 def front_digui(self, root): """ 利用递归实现树的先序遍历: 若二叉树为空,则空操作返回。否则先访问根结点,然后前序遍历左子树,再前序遍历右子数 """ if root == None: return print(root.data) self.front_digui(root.lchild) self.front_digui(root.rchild) def middle_digui(self, root): """ 利用递归实现树的中序遍历: 若二叉树为空,则空操作返回。否则从根结点开始(注意并不是先访问根结点),中序遍历根结点的左子树,然后是访问根结点,最后中序遍历右子树 """ if root == None: return self.middle_digui(root.lchild) print(root.data) self.middle_digui(root.rchild) def later_digui(self, root): """ 利用递归实现树的后序遍历: 若二叉树为空,则空操作返回。否则从左到右先叶子后结点的方式遍历访问左右子树,最后访问根结点。 """ if root == None: return self.later_digui(root.lchild) self.later_digui(root.rchild) print(root.data) def front_stack(self, root): if root == None: return myStack = [] node = root while node or myStack: while node: # 从根节点开始,一直找它的左子树 print(node.data) myStack.append(node) node = node.lchild node = myStack.pop() # while结束表示当前节点node为空,即前一个节点没有左子树了 node = node.rchild # 开始查看它的右子树 def middle_stack(self, root): if root == None: return myStack = [] node = root while node or myStack: while node: # 从根节点开始,一直找它的左子树 myStack.append(node) node = node.lchild node = myStack.pop() # while结束表示当前节点node为空,即前一个节点没有左子树了 print(node.data) node = node.rchild # 开始查看它的右子树 def later_stack(self, root): """利用堆栈实现树的后序遍历""" if root == None: return myStack1 = [] myStack2 = [] node = root myStack1.append(node) while myStack1: # 这个while循环的功能是找出后序遍历的逆序,存在myStack2里面 node = myStack1.pop() if node.lchild: myStack1.append(node.lchild) if node.rchild: myStack1.append(node.rchild) myStack2.append(node) while myStack2: # 将myStack2中的元素出栈,即为后序遍历次序 print(myStack2.pop().data) def level_queue(self,root): '''利用队列实现数的层次遍历''' if root == None: return myQueue = [] node = root myQueue.append(node) while myQueue: node = myQueue.pop(0) print(node.data) if node.lchild != None: myQueue.append(node.lchild) if node.rchild != None: myQueue.append(node.rchild) if __name__ == '__main__': elems = range(10) # 生成十个数据作为树节点 tree = Tree() # 新建一个树对象 for elem in elems: tree.add(elem) # 逐个添加树的节点 print(tree.__dict__) tree.middle_digui(tree.root)
-
线索二叉树(中序)
#!/usr/bin/python3 #coding:utf-8 ''' Created on 2018/09/18 10:56 @author: 刘蒙华 ''' class Node(): def __init__(self,data=-1): self.data = data self.ltag = 1 self.lchild = None self.rtag = 1 self.rchild = None class Tree(): def __init__(self): self.root = Node() self.queue = [] def add(self,root): if self.root.data == -1: self.root = root self.queue.append(root) else: front = self.queue[0] if self.queue: if front.lchild == None: front.ltag = 0 front.lchild = root self.queue.append(root) else: front.rtag = 0 front.rchild = root self.queue.append(root) self.queue.pop(0) def middle_digui(self,root): if root == None: return self.middle_digui(root.lchild) tree.queue.append(root) self.middle_digui(root.rchild) elems = range(10) # 生成十个数据作为树节点 tree = Tree() # 新建一个树对象 for elem in elems: tree.add(Node(elem)) # 逐个添加树的节点 tree.queue =[] tree.middle_digui(tree.root) for number,i in enumerate(tree.queue): if i.ltag == 1 and number != 0: i.lchild = tree.queue[number-1] if i.rtag == 1 and number != len(tree.queue)-1: i.rchild = tree.queue[number+1] for i in tree.queue: print(i.__dict__)