Last active
February 1, 2016 03:13
-
-
Save alexlafroscia/5adda040f90e4f578d83 to your computer and use it in GitHub Desktop.
Find mutations of a string
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
from sys import argv | |
def has_mutation(c): | |
if c == 't': | |
return '7' | |
if c == 'e': | |
return '3' | |
return None | |
class RWay(object): | |
def __init__(self, string): | |
self.root = RNode('') | |
self.root.encode_string(string) | |
def retrieve_strings(self): | |
return self.root.retrieve_strings() | |
class RNode(object): | |
def __init__(self, character): | |
self.character = character | |
self.permutation_child = None | |
self.original_child = None | |
def encode_string(self, string): | |
# Base case: empty string | |
if len(string) == 0: | |
return | |
c = string[0] | |
perm = has_mutation(c) | |
if perm is not None: | |
self.permutation_child = RNode(perm) | |
self.permutation_child.encode_string(string[1:]) | |
self.original_child = RNode(c) | |
self.original_child.encode_string(string[1:]) | |
def retrieve_strings(self): | |
""" | |
Retrieve the strings from this point downward | |
Returns: | |
List<String> | |
""" | |
# Base case: Leaf node | |
if self.original_child is None: | |
return [self.character] | |
base_strings = self.original_child.retrieve_strings() | |
if self.permutation_child is not None: | |
base_strings = base_strings + \ | |
self.permutation_child.retrieve_strings() | |
return list(map(lambda a: self.character + a, base_strings)) | |
if __name__ == '__main__': | |
rway = RWay(argv[1]) | |
print(rway.retrieve_strings()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment