Sunday, April 22, 2018

Replace where ever question mark is there in a string with 0 and 1 with all the patterns created.

Example : input string "a??b?" outputs are a00b0,a00b1,a01b0, a01b1... like that all possiblities Here is the sample code for same
#!/usr/bin/env python
str = "a??b?"
string = list(str)
dictionary = {'?':['0','1']}
results = list()
result_str = ''
start = -1
def getResults(word, start):
for element in word[start:]:
start += 1
if start == len(word):
if element == '?':
word[start-1] =dictionary[element][0]
results.append(''.join(word))
word[start-1] =dictionary[element][1]
results.append(''.join(word))
return
results.append(''.join(word))
return
if element == '?':
word[start-1] =dictionary[element][0]
getResults(word, start)
word[start-1] = dictionary[element][1]
getResults(word, start)
start = 0
getResults(string, start)
print results
view raw qmarkpattern.py hosted with ❤ by GitHub

No comments:

Post a Comment