Discuss
/
Python
/
亲测通过,附上一个对这个问题讲的比较好的一个链接:https://blog.csdn.net/qq_37701443/article/details/82707526
亲测通过,附上一个对这个问题讲的比较好的一个链接:https://blog.csdn.net/qq_37701443/article/details/82707526
Topic source# 杨辉三角
import copy
def triangles():
L = [1]
yield L
Li = []
n = 0
while True:
while True:
if (n <= len(L)):
if (n == 0 or n == len(L)):
Li.append(1)
else:
Li.append(L[n - 1] + L[n])
else:
L = copy.copy(Li)
yield Li
Li = []
n = 0
break
n += 1
return 'done'
- 1
都市里的北极星
#* coding: utf-8 *
def triangles(): N=[1] while True: yield N N=[1]+[N[i]+N[i+1] for i in range(len(N)-1)]+[1]
期待输出:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
[1, 7, 21, 35, 35, 21, 7, 1]
[1, 8, 28, 56, 70, 56, 28, 8, 1]
[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
n = 0 results = [] for t in triangles(): print(t) results.append(t) n = n + 1 if n == 10: break if results == [ [1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1], [1, 6, 15, 20, 15, 6, 1], [1, 7, 21, 35, 35, 21, 7, 1], [1, 8, 28, 56, 70, 56, 28, 8, 1], [1, 9, 36, 84, 126, 126, 84, 36, 9, 1] ]: print('测试通过!') else: print('测试失败!')