Last active
August 8, 2021 23:31
-
-
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,?
This file contains 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
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) | |
This file contains 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
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:]) |
This file contains 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
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