冒泡排序,斐波拉契迭代、生成器
冒泡排序:
l=[1,3,0,100,1000,8,2,9,5,11,8] def maopao(l): for i in range(len(l)-1): if l[i+1] < l[i]: a= l[i] b=l[i+1] l[i] = b l[i+1] = a return l for i in range(len(l)): maopao(l) print l 返回:[0, 1, 2, 3, 5, 8, 8, 9, 11, 100, 1000]
斐波拉契-生成器(返回小于100的数):
def fbg(max): x,y=0,1 while y < max: yield y x,y=y,x+y g=fbg(100) for i in g: print i 返回: 1 1 2 3 5 8 13 21 34 55 89
斐波拉契-迭代1(返回小于100的数):
def fbd(x,y,max): print y x,y=y,x+y if y<max: fbd(x,y,max) fbd(0,1,100)
斐波拉契-迭代2(返回前10个数):
def fbd(n): if n==0: return 0 if n==1: return 1 else: r=fbd(n-1) + fbd(n-2) return r for i in range(10): print fbd(i) 返回: 0 1 1 2 3 5 8 13 21 34