Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,874 questions

51,797 answers

573 users

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
...