Skip to content

Instantly share code, notes, and snippets.

@beiweiqiang
Last active March 8, 2017 12:45
Show Gist options
  • Save beiweiqiang/ae925f78dfbd0dcb47fdc0506c1c361c to your computer and use it in GitHub Desktop.
Save beiweiqiang/ae925f78dfbd0dcb47fdc0506c1c361c to your computer and use it in GitHub Desktop.
python triangles
def triangles(max):
n = 3
my_list = [1]
if max > 1:
yield my_list
if max > 2:
my_list = [1, 1]
yield my_list
while n <= max:
n = n + 1
new_list = [1]
for i in range(n - 3):
new_list.append(my_list[i] + my_list[i + 1])
new_list.append(1)
my_list = new_list
yield my_list
return 'done'
tr = triangles(9)
while True:
try:
x = next(tr)
print(x)
except StopIteration as e:
print('Generator return value: ', e.value)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment