Advertisement
here2share

# list_combos_sum_of_12.py

Feb 4th, 2021
1,572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. # list_combos_sum_of_12.py
  2.  
  3. def t12(z):
  4.     L = z
  5.     sss = [[z]]
  6.     ppp = []
  7.     zzz = []
  8.     while 1:
  9.         while len(sss[0]) < L:
  10.             if z > 1:
  11.                 t = sss[0][:-1]+[z-1]
  12.                 if t not in ppp:
  13.                     if sum(t) < L:
  14.                         sss.append(t)
  15.                         ppp.append(t)
  16.             if sum(sss[0])+z > L:
  17.                 z = max(0,z-1)
  18.             else:
  19.                 sss[0].append(z)
  20.         zzz.append(sss.pop(0))
  21.         if sss:
  22.             z = sss[0][-1]
  23.         else:
  24.             break
  25.     return zzz
  26.  
  27. combos = t12(12)
  28.  
  29. def permute_unique(nums):
  30.     perms = [[]]
  31.     for n in nums:
  32.         new_perm = []
  33.         for perm in perms:
  34.             for i in range(len(perm) + 1):
  35.                 new_perm.append(perm[:i] + [n] + perm[i:])
  36.                 # handle duplication
  37.                 if i < len(perm) and perm[i] == n: # skips a few
  38.                     break
  39.         perms = new_perm
  40.     return perms
  41.  
  42. zzz = sum([[z for z in permute_unique(combo)] for combo in combos],[])
  43. zzz.sort(reverse=0)
  44. L = len(zzz)
  45.  
  46. for z in zzz:
  47.     print z,L
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement