Skip to content

Instantly share code, notes, and snippets.

@dalang
Created May 11, 2015 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalang/b14ad5ab3a20795ae55c to your computer and use it in GitHub Desktop.
Save dalang/b14ad5ab3a20795ae55c to your computer and use it in GitHub Desktop.
print yanghui triangle
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