Discuss / Python / 三角

三角

Topic source

Carbon

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

# 学习时间: 2022/9/22 15:30

def triangles():

    n, a, b = 0, 1, 1

    lst = []

    while True:

        if n == 0:  # 第一行

            lst = [1]

            n += 1

            yield lst

        elif n == 1:  # 第二行

            lst = [1, 1]

            n += 1

            yield lst

        else:  # 三行和之后

            new_lst = []

            for index, item in enumerate(lst):

                if index == 0:

                    new_lst.append(1)

                else:

                    new_lst.append(lst[index-1] + lst[index])

                    if index == len(lst) - 1:

                        new_lst.append(1)

            n += 1

            lst = new_lst

            yield lst


  • 1

Reply