编写一个类,具有栈和队列的功能。实现以下4个方法:
shit() 返回并删除列表中第一个元素
unshit() 在列表的头部‘压入’一个新的元素
push() 在列表尾部增加一个元素
pop() 返回并删除最后一个元素
outAllData() 输出所有的数据
class stackAndQueue(object): data = [] def isEmpty(self): return len(self.data)==0 def shift(self): if stackAndQueue.isEmpty(self): print 'the operator is illegal' else : ret = self.data[0] del self.data[0] return ret def unshift(self, val): self.data.insert(0, val) def push(self, val): self.data.append(val) def pop(self): if stackAndQueue.isEmpty(self): print 'the operator is illegal' else : return self.data.pop() def outAllData(self): print ' '.join( map(str, self.data) )
快速排序:
def quickSort(num): if num == []: return [] n = len(num) return quickSort([num[i] for i in xrange(1, n) if num[i]<=num[0] ]) + [num[0]] + quickSort([num[i] for i in xrange(1, n) if num[i]>num[0] ]) def main(): l1 = [9, 8, 7, 5, 4, 1, 7, 56, 34] l2 = quickSort(l1) print l1 print l2 if __name__ == '__main__': main()
选择排序:
def selection_sort(L): Len = len(L) for i in xrange(Len): smallest = find_min(L, i) L[i], L[smallest] = L[smallest], L[i] def find_min(L, b): smallest = b Len = len(L) for i in xrange(b, Len): if L[i] < L[smallest]: smallest = i return smallest def main(): l1 = [6, 5, 4, 3, 2, 2, 9, -3, 56] print 'before sort ', l1 selection_sort(l1) print 'after sort ',l1 if __name__ == '__main__': main()
插入排序
def insertion_sort(L): Len = len(L) for i in xrange(1,Len): insert(L, i) def insert(L, b): Len = len(L) i = b while (L[i-1] > L[i]) and (i-1>=0): L[i-1], L[i] = L[i], L[i-1] i-= 1 def main(): l1 = [6, 5, 4, 3, 2, 2, 9, -3, 56] print 'before sort ', l1 insertion_sort(l1) print 'after sort ',l1 if __name__ == '__main__': main()
另外一个版本,使用bisect模块实现。bisect模块有以下几个方法: 这些方法返回的都是位置信息
bisect(x) 默认是bisect_right(a, x)
bisect_left(a, x) 在有序序列里查找第一个大于x的位置,如果有与x相同的元素,则返回与x相等元素中最左边的那个位置
bisect_right(a, x) 在有序序列里查找第一个大于x的位置,如果有与x相同的元素,则返回与x相等元素中最右边的那个位置
insort 默认是insort_right(a, x)
insort_left(a, x) 在有序序列里插入x,如果有与x相同的元素,则当前元素插入在这些相同元素的最左边
insort_right(a, x) 在有序序列里插入x,如果有与x相同的元素,则当前元素插入在这些相同元素的最右边
import bisect
def bin_sort(values): '''sort values , creating a new list''' result = [] for v in values: bisect.insort_left(result, v) return result def main(): l1 = [6, 5, 4, 3, 2, 2, 9, -3, 56] print 'before sort ', l1 l1=bin_sort(l1) print 'after sort ',l1 if __name__ == '__main__': main()
合并排序(递归版)
def mergesort(L): if len(L) < 2: return L mid = (len(L))>>1 L1 = mergesort(L[:mid]) L2 = mergesort(L[mid:]) return merge(L1, L2) def merge(L1, L2): newL = [] i1 = i2 =0 Len1, Len2 =len(L1), len(L2) while i1!=Len1 and i2!=Len2: if L1[i1] <= L2[i2]: newL.append(L1[i1]) i1 += 1 else : newL.append(L2[i2]) i2 += 1 newL.extend(L1[i1:]) newL.extend(L2[i2:]) return newL def main(): l1 = [6, 5, 4, 3, 2, 2, 9, -3, 56] print 'before sort ', l1 l1=mergesort(l1) print 'after sort ',l1 if __name__ == '__main__': main()
合并排序(非递归版)
def mergesort(L):
''' segNum 未合并的序列数 segLength 每个序列的长度,不包括最后那段 lastSegLength 最后那短的长度,这个是不确定的 ''' segNum, segLength, lastSegLength = len(L), 1, 1 while segNum > 1: tmp_segNum = segNum segNum >>= 1 tmp_segLength = segLength<<1 if tmp_segNum&1: j = 0 for i in xrange(segNum): merge(L, j, j+segLength,j+tmp_segLength) j += tmp_segLength j = len(L)-lastSegLength-tmp_segLength+1 merge(L, j, j+tmp_segLength, len(L)) else : j = 0 for i in xrange(segNum): if i != (segNum-1): merge(L, j, j+segLength, j+tmp_segLength) else : merge(L, j, j+segLength, len(L)) j += tmp_segLength lastSegLength += segLength segLength = tmp_segLength def merge(L, s, mid, t): tmp = [] i1, i2 = s, mid while i1!=mid and i2!=t: if L[i1]<=L[i2]: tmp.append(L[i1]) i1 += 1 else : tmp.append(L[i2]) i2 += 1 tmp.extend(L[i1:mid]) tmp.extend(L[i2:t]) for i in xrange(t-1, s-1, -1): L[i] = tmp.pop() def main(): l1 = [6, 5, 4, 3, 2, 2, 9, -3, 56] print 'before sort ', l1 mergesort(l1) print 'after sort ', l1 if __name__ == '__main__': main()
表达式的计算,带括号:
ss = raw_input().strip() Len = len(ss) info, sign = [], [] def isdigit(x): return True if ord('0')<=ord(x)<=ord('9') else False def count(): b, a = info.pop(), info.pop() op = sign.pop() if op=='+': a += b elif op=='-': a -= b elif op=='*': a *= b elif op=='/': a /= b info.append(a) i = 0 while i < Len: if isdigit(ss[i] ): tmp = 0 while i<Len and isdigit(ss[i]) : tmp = tmp*10 + ord(ss[i])-ord('0'); i+=1 i-=1; info.append(tmp) elif ss[i]=='(': sign.append(ss[i]) elif ss[i]==')': while sign and sign[-1]!='(': count() sign.pop() elif ss[i]=='+' or ss[i]=='-': while sign and sign[-1]!='(': count() sign.append(ss[i]) elif ss[i]=='*' or ss[i]=='/': while sign and sign[-1]!='(' and sign[-1]!='+' and sign[-1]!='-': count() sign.append(ss[i]) i += 1 while sign: count() print info[0]