Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # b_sum_n_by_len.py
- from itertools import combinations
- def place_combo(theLength,theList):
- aaa = [0] * theLength
- ttt = []
- zzz = len(theList)
- forLoc = range(theLength)
- for subset in combinations(forLoc, zzz):
- xyz = theList[:]
- arr = aaa[:]
- for i in subset:
- arr[i] = xyz.pop(0)
- ttt += [arr]
- ttt.sort()
- return ttt
- def sum_n_by_len(n,L=None):
- if not L:
- L = n
- result = []
- ttt = []
- for i in range(n):
- t = [n-i, i]
- result.extend(place_combo(L,t))
- if i > 1:
- ttt.append(t)
- while ttt:
- carry = ttt.pop()
- n = carry[-1]
- for i in range(1,n):
- t = carry[:-1]+[n-i, i]
- result.extend(place_combo(L,t))
- if i > 1:
- ttt.append(t)
- result.sort()
- return result
- zzz = sum_n_by_len(10)
- for z in zzz:
- print z
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement