Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.