How to generate all possible binary strings by replacing ? from a given pattern in Python

1 Answer

0 votes
def generate_all_possible_binary_strings(s):
    q = []
    q.append(s)
    
    while(len(q) > 0):
        temp = q[0]
        try:
            index = temp.index('?')
        except ValueError:
            index = -1
             
        if (index != -1):
            s1 = temp.replace('?', '0', 1)
            q.append(s1)
            s2 = temp.replace('?', '1', 1)
            q.append(s2)
        else:
            print(temp)
        q.pop(0)
             

s = "1?0?1";
generate_all_possible_binary_strings(s)




'''
run:

10001
10011
11001
11011

'''

 



answered Aug 24, 2023 by avibootz
edited Aug 24, 2023 by avibootz
...