Discuss / Python / 交作业啦

交作业啦

Topic source

BillCode

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

强大的评论团

def triangles(): l =[1] yield l while True:
l = [1] +[l[i] + l[i+1] for i in range(len(l) - 1)] + [1] yield l

n = 0 for t in triangles(): print(t) n = n + 1 if n == 10: break

奔跑吧Sky

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

def triangles(): L=[1] yield L while True: L=[1]+[L[i]+L[i+1] for i in range(len(L)-1)]+[1] yield L 这个算法确实很巧妙,但是有一点我一直没明白 就是在第一次执行while循环的时候L=[1],那么range(len(L)-1)就是range(0)了,中间这一串[L[0]+L[1]]的L[1]是什么?还有range(0)生成的是什么? 希望得到大神解答


  • 1

Reply