python 楊輝三角 算法實現

 1 def triangles(level):
 2     n = 1
 3     L = []
 4     while n <=level:
 5         if n <= 2:
 6             L.append(1)
 7             yield L
 8         elif n > 2 :
 9             LL = [1]
10             y = None
11             num = len(L)
12             for i,v in enumerate(L) :
13                 if i < num-1 :
14                     y = v + L[i+1]
15                     LL.append(y)
16                 pass
17             LL.append(1)
18             L = LL
19             yield LL
20         n = n +1
for n in triangles(10) :
	print(n)

 結果輸出:python

  

相關文章
相關標籤/搜索