Advertisement
here2share

# b_sum_n_by_len.py

Nov 20th, 2021
1,346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. # b_sum_n_by_len.py
  2.  
  3. from itertools import combinations
  4.  
  5. def place_combo(theLength,theList):
  6.     aaa = [0] * theLength
  7.     ttt = []
  8.     zzz = len(theList)
  9.     forLoc = range(theLength)
  10.     for subset in combinations(forLoc, zzz):
  11.         xyz = theList[:]
  12.         arr = aaa[:]
  13.         for i in subset:
  14.             arr[i] = xyz.pop(0)
  15.         ttt += [arr]
  16.     ttt.sort()
  17.     return ttt
  18.  
  19. def sum_n_by_len(n,L=None):
  20.     if not L:
  21.         L = n
  22.     result = []
  23.     ttt = []
  24.     for i in range(n):
  25.             t = [n-i, i]
  26.             result.extend(place_combo(L,t))
  27.             if i > 1:
  28.                 ttt.append(t)
  29.     while ttt:
  30.         carry = ttt.pop()
  31.         n = carry[-1]
  32.         for i in range(1,n):
  33.             t = carry[:-1]+[n-i, i]
  34.             result.extend(place_combo(L,t))
  35.             if i > 1:
  36.                 ttt.append(t)
  37.     result.sort()
  38.     return result
  39.  
  40. zzz = sum_n_by_len(10)
  41. for z in zzz:
  42.     print z
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement