Skip to content

Instantly share code, notes, and snippets.

@firemanxbr
Created September 16, 2019 20:12
Show Gist options
  • Save firemanxbr/78f0b545f80f8e37d323f510915bc076 to your computer and use it in GitHub Desktop.
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
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