Last active
March 8, 2017 12:45
-
-
Save beiweiqiang/ae925f78dfbd0dcb47fdc0506c1c361c to your computer and use it in GitHub Desktop.
python triangles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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