I am generating all binary strings of a given length and am trying to store them in a list which I am having trouble with. Here is the code:
def generateAllBinaryStrings(n, arr, l, i):
if i == n:
l.append(arr)
print(arr)
return
arr[i] = 0
generateAllBinaryStrings(n, arr, l, i + 1)
arr[i] = 1
generateAllBinaryStrings(n, arr, l, i + 1)
return l
n = 3
l = []
arr = [None] * n
generateAllBinaryStrings(n, arr, l, 0)
i.e. first creating all possible strings with the starting bit as 0 and then all strings with the starting bit as 1. arr stores strings and am trying to append them into a list l. The following print result is right for print(arr) statement but l is not correct. May I know where I went wrong?:
[0, 0, 0]
[0, 0, 1]
[0, 1, 0]
[0, 1, 1]
[1, 0, 0]
[1, 0, 1]
[1, 1, 0]
[1, 1, 1]
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]