杨辉三角generator

def triangles(num):
    L2 = [1, 1]
    if num >= 1:
        L1=1
        yield L1
    if num >= 2:
        L = [1, 1]
        yield L
    if num >= 3:
        for i in range(num-2):
            L3 = [L2[i]+L2[i+1] for i in range(len(L2)-1)]
            L3.insert(0,1)
            L3.append(1)
            L2 = L3
            yield L3

 

for n in triangles(10):
    print(n)

posted @ 2018-08-19 20:59  地瓜来了  阅读(151)  评论(0编辑  收藏  举报