Skip to content

Instantly share code, notes, and snippets.

@jhidajat
Last active August 8, 2021 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhidajat/6ffbfc73f69d67c7cd9fb7fbb14a3de3 to your computer and use it in GitHub Desktop.
Save jhidajat/6ffbfc73f69d67c7cd9fb7fbb14a3de3 to your computer and use it in GitHub Desktop.
Get the combination of possible outcome of input string s, which contains 0,1,?
def get_combi(s):
return get_combi_with_refixes([], s)
"""
The idea is to assume you have a calculated array of strings that you need
to append to. if its a ?, you "branch out" adding more to the array. When
suffix is empty, that means you have successfully recursed through the whole
string.
"""
def get_combi_with_prefixes(prefixes, suffix):
if not suffix:
return prefixes
curr, remaining_suffix = suffix
new_prefixes = []
if curr == '?':
for p in prefixes:
new_prefixes.extends([p+'0',p+'1'])
else:
new_prefixes += [p+curr]
return get_combi_with_refixes(new_prefixes, reimaining_suffix)
def get_combi_with_prefixes(prefixes, suffix):
if not suffix:
return prefixes
new_prefixes = [p+'0' for p in prefixes]+[p+'1' for p in prefixes] if suffix[0] == '?' else [p+suffix[0]]
return get_combi_with_refixes(new_prefixes, suffix[1:])
def get_combi_with_prefixes(prefixes, suffix):
return get_combi_with_refixes([p+'0' for p in prefixes]+[p+'1' for p in prefixes] if suffix[0] == '?' else [p+suffix[0]], suffix[1:]) if suffix else prefixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment