0

Use case 1 :

str = '?0?'   #str is input string , we need to replace every '?' with [0,1].

so the resulting output will be :

['000','100','001','101']

use case 2 :

str = '?0' #input

Expected output :

['00','10']

use case 3 :

str='?'

Expected output :

['0','1']

The length of the string and number of '?' in string may vary for different inputs.

3
  • 1
    What have tried so far? Commented Jun 14, 2022 at 5:59
  • I see plenty of question marks but no question. Commented Jun 14, 2022 at 6:00
  • Are you aware of the differences between strings and integers? Your question suggest you are not, so you may want to read up on beginners tutorials on that. Commented Jun 14, 2022 at 6:00

2 Answers 2

1

Here's a solution using itertools.product to produce all possible products of [0,1] for a ? or the digit itself:

str = '?0?'
[''.join(p) for p in itertools.product(*[['0', '1'] if c == '?' else c for c in str])]

Output:

['000', '001', '100', '101']
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your solution , it is exactly what i am looking for. I am new to the python collections framework , will explore more on itertools to handle similar kind of problems.
0

Without itertools:

s = '?0?'

a = ['']
for c in s:
    a = [b+d for b in a for d in c.replace('?', '01')]

print(a)

Try it online!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.