标签:
#!/usr/bin/env python # -*- coding: utf-8 -*- def triangles(): n = 1 aboveList = [] while True: if n == 1: aboveList = [1] n = n + 1 yield [1] if n == 2: aboveList = [1,1] n = n + 1 yield [1,1] newList = [] for x in getMiddleList(aboveList): newList.append(x) newList.insert(0,1) newList.append(1) aboveList = newList n = n + 1 yield newList return ‘done‘ def getMiddleList(aboveList): newList = [] leftNodeVal=0 n=1 for x in aboveList: if n == 1: leftNodeVal = x else: newList.append(x+leftNodeVal) leftNodeVal = x n += 1 return newList n=0 for t in triangles(): print(t) n = n + 1 if n == 10: break
输出结果:
不是最佳的算法,代码比较多,后面继续研究
标签:
原文地址:http://www.cnblogs.com/frankyou/p/5730257.html