Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. from itertools import product
  2. from functools import reduce
  3.  
  4. def ilen(iterable):
  5.     return reduce(lambda sum, element: sum + 1, iterable, 0)
  6.  
  7. def proditer(xs):
  8.     for i in product(*map(lambda x: range(x + 1), xs)):
  9.       yield i
  10.  
  11. def is_valid(ds):
  12.     if sum(ds) == 0:
  13.         return False
  14.     elif ((True in map(lambda x: x >= 2, ds)) and (ilen(filter(lambda x: x > 0, ds)) < 2)):
  15.         return False
  16.     else:
  17.         return True
  18.  
  19. def is_zero(ds):
  20.     return sum(ds) == 0
  21.  
  22.  
  23. def pts(xs, prt=[]):
  24.     if is_zero(xs):
  25.         return 0
  26.     out = 0
  27.     for ds in proditer(xs):
  28.         if not is_valid(ds):
  29.             continue
  30.         dff = [x - d for x, d in zip(xs, ds)]
  31.         if sorted(prt + [ds]) != prt + [ds]:
  32.             continue
  33.            
  34.         if is_zero(dff):
  35.             q = prt + [ds]
  36.             print(q)
  37.             continue
  38.         else:
  39.             out += pts(dff, prt + [ds])
  40.     return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement