Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- import operator
- from itertools import starmap, tee
- from functools import lru_cache
- def pairwise(iterable):
- """
- s -> (s0,s1), (s1,s2), (s2,s3), ...
- https://docs.python.org/3/library/itertools.html#itertools-recipes
- """
- a, b = tee(iterable)
- next(b, None)
- return zip(a, b)
- def pascal_row(n):
- """
- Print the nth row of a Pascal triangle
- """
- if n < 2:
- return (x for x in [1])
- else:
- def dispatch():
- yield 1
- yield from starmap(operator.add, pairwise(pascal_row(n-1)))
- yield 1
- return dispatch()
- def pascal(n):
- """
- Print an n-order Pascal triangle
- """
- for i in range(1, n):
- print(pascal_row(i))
- print([x for x in pascal_row(500)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement