scala - Why doesn't this Python code for powerset work? -


i can't identify blatantly wrong except trying scala-type operations python.

def powerset(arr):         if len(arr) == 0:             return []         elif len(arr) > 1:             return powerset(arr[1:])+[arr[0]] 

i consistently error:

return powerset(arr[1:])+[arr[0]]

typeerror: unsupported operand type(s) +: 'nonetype' , 'list'

if len(arr) == 1

nothing returned ...

just change

def powerset(arr):     if len(arr)>0:          return powerset(arr[1:])+[arr[0]]     else:          return [] 

here powerset function https://docs.python.org/2/library/itertools.html

def powerset(iterable):     "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"     s = list(iterable)     return chain.from_iterable(combinations(s, r) r in range(len(s)+1)) 

here powerset implementation uses less magic

def powerset(seq):     """     returns subsets of set. generator.     """     if len(seq) <= 1:         yield seq         yield []     else:         item in powerset(seq[1:]):             yield [seq[0]]+item             yield item  print list(powerset([1,2])) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -