0

I'm stuck on a question that should be an easy one but I can't possibly solve. Given an integer n, I need to find all the possible combinations of lists that contain only 1s and 0s. For example , with n=3, the answer is:[0,0,0],[1,1,1],[1,0,0],[0,1,0],[0,0,1],[0,1,1],[1,0,1],[1,1,0] if I'm not missing any. I found that we can find this number of lists adding 1 and 0 to all lists of lenght n-1 following this condition. I then made this function:

def ensemble_liste(n):
if n==1:
    return [[1],[0]] 
return [[0]+ensemble_liste(n-1),[1]+ensemble_liste(n-1)]]

But it's returning me a syntax error on the second return. Formatting it like this

return [[[0]+ensemble_liste(n-1)],[[1]+ensemble_liste(n-1)]]

works , but I get the following output with n=3(it's a list made of two elements )

[[0, [[0, [1], [0]]], [[1, [1], [0]]]]]#First element
[[1, [[0, [1], [0]]], [[1, [1], [0]]]]]#Second element

which isn't quite formatted like I would like and it is giving an incorrect answer... Does anyone has an idea on how to correct this ? Thank you very much

Francesco
  • 67
  • 1
  • 1
  • 7

0 Answers0