杨辉三角的Python实现
1 1 1 1 2 1 1 3 3 1 1 4 6 4 11 5 10 10 5 1
Python生成器实现杨辉三角:
# pythondef yanghui_trangle(n): if not isinstance(n, int) or n <= 0: print('error') line = [1] for i in range(n): yield line line = [1] + [line[i] + line[i+1] for i in range(len(line) - 1)] + [1] # 生成下一行
for i in yanghui_trangle(10): print(i)