Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def comb_num(size):
- '''Accept a digit and return a list of lists of digits between 0 and
- that digit including 0, selected 1 at a time, 2 digits at a time,...
- up till the position of the specified digit.
- For example the function accepts a digit 3 and returns a list of lists:
- [[0], [1], [2], [0, 1], [0, 2], [1, 2], [0, 1, 2]]
- '''
- finalresult = []
- def comb_num_norep(size, numdigits):
- supresult = {}
- def comb_num_rep(size, numdigits):
- if numdigits == 1: result = ([elements] for elements in range(size))
- else:
- result = (element2 + [element3] for element2 in \
- comb_num_rep(size, numdigits - 1) for element3 in range(size) \
- if element3 not in element2)
- return result
- for elements in comb_num_rep(size, numdigits):
- elements.sort()
- keytuple = tuple(elements)
- try:
- supresult[keytuple] += 1
- except KeyError:
- supresult[keytuple] = 1
- return [list(element) for element in list(supresult)]
- for numdigits in range(1, size + 1):
- finalresult += comb_num_norep(size, numdigits)
- return finalresult
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement