Created
September 16, 2019 20:12
-
-
Save firemanxbr/78f0b545f80f8e37d323f510915bc076 to your computer and use it in GitHub Desktop.
# Recursive Python program to generate all binary strings formed by replacing each wildcard character by 0 or 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array = [] | |
def _print(string, index): | |
if index == len(string): | |
array.append(''.join(string)) | |
if index == len(array): | |
print(array) | |
return | |
if string[index] == "?": | |
# replace '?' by '0' and recurse | |
string[index] = '0' | |
_print(string, index + 1) | |
# replace '?' by '1' and recurse | |
string[index] = '1' | |
_print(string, index + 1) | |
# NOTE: Need to backtrack as string | |
# is passed by reference to the | |
# function | |
string[index] = '?' | |
else: | |
_print(string, index + 1) | |
# Driver code | |
if __name__ == "__main__": | |
string = "0?1?" | |
string = list(string) | |
_print(string, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment