Discuss / Python / 据上层代码改进而来,发现规律,只有中间的值在相加得到下一层,首尾未参与相加,所以只需要迭代中间的数即可

据上层代码改进而来,发现规律,只有中间的值在相加得到下一层,首尾未参与相加,所以只需要迭代中间的数即可

Topic source

Newself.

#1 Created at ... [Delete] [Delete and Lock User]
def triangles(num):    n = 0    line = [1]    while n < num:        n = n + 1        yield line        line = [line[s] + line[s-1] for s in range(1,len(line))]        line.insert(0,1)        line.append(1)for t in triangles(10):    print(t)

  • 1

Reply