Advertisement
Guest User

comb_num

a guest
May 7th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. def comb_num(size):
  2.     '''Accept a digit and return a list of lists of digits between 0 and
  3.    that digit including 0, selected 1 at a time, 2 digits at a time,...
  4.    up till the position of the specified digit.
  5.    
  6.    For example the function accepts a digit 3 and returns a list of lists:
  7.    [[0], [1], [2], [0, 1], [0, 2], [1, 2], [0, 1, 2]]
  8.    '''
  9.     finalresult = []
  10.     def comb_num_norep(size, numdigits):
  11.         supresult = {}
  12.         def comb_num_rep(size, numdigits):
  13.             if numdigits == 1: result = ([elements] for elements in range(size))
  14.             else:
  15.                 result = (element2 + [element3] for element2 in \
  16.                 comb_num_rep(size, numdigits - 1) for element3 in range(size) \
  17.                 if element3 not in element2)
  18.             return result
  19.         for elements in comb_num_rep(size, numdigits):
  20.             elements.sort()
  21.             keytuple = tuple(elements)
  22.             try:
  23.                 supresult[keytuple] += 1
  24.             except KeyError:
  25.                 supresult[keytuple] = 1
  26.         return [list(element) for element in list(supresult)]
  27.     for numdigits in range(1, size + 1):
  28.         finalresult += comb_num_norep(size, numdigits)
  29.     return finalresult
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement