def triangles(lines) :
num = 1
last = []
while num < lines :
if num == 1 :
last = [1]
yield [1]
num = num + 1
continue
now = []
for i in range(0, num) :
left = 0
right = 0
if i - 1 < 0 :
left = 0
else:
left = last[i - 1]
if i >= len(last) :
right = 0
else :
right = last[i]
now.append(left + right)
last = now
yield now
num = num + 1
xinyujaychou
我感觉我写得好繁琐
def triangles(lines) : num = 1 last = [] while num < lines : if num == 1 : last = [1] yield [1] num = num + 1 continue now = [] for i in range(0, num) : left = 0 right = 0 if i - 1 < 0 : left = 0 else: left = last[i - 1] if i >= len(last) : right = 0 else : right = last[i] now.append(left + right) last = now yield now num = num + 1