Discuss / Python / Homework

Homework

Topic source

逝去的9211

#1 Created at ... [Delete] [Delete and Lock User]

while循环的实现

# -*- coding:utf:8 -*-
def triangles():
    n=1
    L=[[1]]
    while n>=0:
        yield L [n-1]
        L.append([1])
        i=1
        while i<len(L[n-1]):
            L[n].append(L[n-1][i-1]+L[n-1][i])
            i=i+1
        L[n].append(1)
        n=n+1

别人的简便方法:

def triangles():
    N=[1]
    while True:
        yield N
        N.append(0)
        N=[N[i-1]+N[i] for i in range(len(N))]

  • 1

Reply