SHARE
TWEET

Untitled

a guest Mar 21st, 2017 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. import operator
  5. from itertools import starmap, tee
  6. from functools import lru_cache
  7.  
  8.  
  9. def pairwise(iterable):
  10.     """
  11.     s -> (s0,s1), (s1,s2), (s2,s3), ...
  12.     https://docs.python.org/3/library/itertools.html#itertools-recipes
  13.     """
  14.     a, b = tee(iterable)
  15.     next(b, None)
  16.     return zip(a, b)
  17.  
  18.  
  19. def pascal_row(n):
  20.     """
  21.     Print the nth row of a Pascal triangle
  22.     """
  23.     if n < 2:
  24.         return (x for x in [1])
  25.     else:
  26.         def dispatch():
  27.             yield 1
  28.             yield from starmap(operator.add, pairwise(pascal_row(n-1)))
  29.             yield 1
  30.         return dispatch()
  31.  
  32.  
  33. def pascal(n):
  34.     """
  35.     Print an n-order Pascal triangle
  36.     """
  37.     for i in range(1, n):
  38.         print(pascal_row(i))
  39.  
  40. print([x for x in pascal_row(500)])
RAW Paste Data
Top