I am having issues with this python problem; I have been working for the past hour on it and I am completely stuck.. I am still not very confortable with recursion and the problems keep getting harder, so any type of help is very much appreciated!
The problem is simple, I need to write a function that takes as inputs n and w; where n is the size of the bit string and w is the number of ones in a string. The output should be all of its permutations.
Example:
n = 3, w = 1 : ['001', '010', '100']
n = 4, w = 2: ['0011', '0101', '0110', '1001', '1010', '1100']
This is what I have written so far but as much as I tweak it or run it in python visualizer I just can't figure it out:
def genBinStr2(n,w):
if n <=0 or w <= 0 :
return [""]
X = genBinStr2(n-1,w)
Y = genBinStr2(n-1,w-1)
M = []
for s in X:
M.append("0"+s)
for m in Y:
M.append("1"+s)
return M
print (genBinStr2(3,1))
And the output is:
runfile('/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /Revision/untitled0.py', wdir='/Users/Rayan/Desktop/AUB Spring 2019/EECE 230 /Revision')
['000', '001', '011', '111']
Again any help is appreciated! I really want to be able to solve this
Thank you!!