Created
May 11, 2015 17:07
-
-
Save dalang/b14ad5ab3a20795ae55c to your computer and use it in GitHub Desktop.
print yanghui triangle
This file contains hidden or 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 yanghui_triangle(): | |
def safe_get_value(l, index): | |
length = len(l) | |
return 0<= index < length and l[index] or 0 | |
tmp = [1] | |
while True: | |
yield tmp | |
length = len(tmp) | |
new = [] | |
for idx in range(length+1): | |
item = safe_get_value(tmp, idx-1) + safe_get_value(tmp, idx) | |
new.append(item) | |
tmp = new | |
if __name__ == '__main__': | |
yh_gen = yanghui_triangle() | |
for _ in range(10): | |
print next(yh_gen) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment