python3:打印杨辉三角

孤街浪徒 提交于 2020-02-29 17:39:47
#!/usr/bin/env python3
'''
打印杨辉三角
'''

def triangle():
    yield [1]
    yield [1, 1]
    upline = [1, 1]
    count = 0
    while True:
        count += 1
        line = [1]
        for x in range(count):
            line.append(upline[x] + upline[x + 1])
        line.append(1)
        yield line
        upline = line


if __name__ == '__main__':
    yang = triangle()
    for x in range(10):
        print(next(yang))
    
    yang = triangle()
    row = 6
    width = 2
    for x in range(row):
        l = [ str(e).center(width) for e in next(yang)]
        print(''.join(l).center(row * width))

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!