九州体育网 - 十年信誉网站

关于作者

Discuss / Python / 交作业(利用一个部分变量存储上一行的值)

交作业(利用一个部分变量存储上一行的值)

Topic source

_漫游龙飞雪_

#1 Created at ...
#杨辉三角
def triangles():
    line, temp, count = [1], [], 1 #上一行结果数组;#每行结果数组;#行数

    while True:
        yield line

        temp.clear()
        count = count + 1
        temp.append(line[0]) #拼接第一个元素
        if len(line) > 1:
            index = 1
            for item in line[1:]:
                temp.append(line[index-1] + item)
                index = index + 1
        temp.append(line[-1]) #拼接最后一个元素
        line = temp[:]

    return line

_漫游龙飞雪_

#2 Created at ...
This reply was deleted

  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.