diff --git a/wordcross.py b/wordcross.py index efb5567..e0df75c 100644 --- a/wordcross.py +++ b/wordcross.py @@ -75,7 +75,7 @@ def get_word(self): return ''.join(reversed(reversed_word)) -def serialize_to_file(node, file_path): +def node_to_file(node, file_path): """Serialize a Node to a file on disk. Args: @@ -88,7 +88,7 @@ def serialize_to_file(node, file_path): with open(file_path, 'wb+') as file: pickle.dump(node, file) -def deserialize_from_file(file_path): +def node_from_file(file_path): """Deserialize a Node from a file on disk. Args: @@ -151,11 +151,9 @@ def split_file(file_name, output_dir): file_name (str): The path on disk to a list of line-delimited English words. output_dir (str): The path to a directory where the deterministic output files will be generated. """ - words = [line.strip() for line in open(file_name)] - # Process the words into a dictionary where the key is the first letter, value is the list of words grouped_words = {} - for word in words: + for word in [line.strip() for line in open(file_name)]: grouped_words.setdefault(word[0].upper(), []).append(word.upper()) # Loop through each letter and write an output file for all the words starting with that letter @@ -177,14 +175,11 @@ def solve(possible_letters): The list is first sorted by word length (shortest first), then alphabetically. """ result = [] - # Load all Trees from disk so we can time the speed of the implementation - trees = [get_tree_for_letter(letter) for letter in set(possible_letters)] - - for tree in trees: + for tree in [get_tree_for_letter(letter) for letter in set(possible_letters)]: get_possible_words(tree, possible_letters, result) return sorted(result, key=lambda r: (len(r), r.upper())) -def get_possible_words(root: Node, possible_letters, word_list=set()): +def get_possible_words(root: Node, possible_letters, word_list): """Gets all possible words from a single tree that can be composed using specified letters. This is a recursive function. Callers should supply a `word_list`. Iterations of this function @@ -200,8 +195,7 @@ def get_possible_words(root: Node, possible_letters, word_list=set()): letters = possible_letters[:] letters.remove(root.character) - valid_children = [c for c in root.children if c.character in letters] - for child in valid_children: + for child in [child for child in root.children if child.character in letters]: get_possible_words(child, letters, word_list) def print_solution(words): @@ -213,7 +207,7 @@ def print_solution(words): curr_len = 0 for word in words: if curr_len != len(word): - print(str(len(word)) + "-Letter Words:") + print(''.join([str(len(word)), "-Letter Words:"])) curr_len = len(word) print(word) @@ -225,7 +219,7 @@ def get_tree_for_letter(starting_letter): Raises: ValueError: If `starting_letter` is not a character from [A-Z]. - FileNotFoundError: If `.\TextFiles\master-list.txt` is not present on disk. This file + FileNotFoundError: If `.\\TextFiles\\master-list.txt` is not present on disk. This file is in the Master branch of this repository and must always be present. """ if ((len(starting_letter) != 1) or (starting_letter.upper() not in string.ascii_uppercase)): @@ -237,14 +231,14 @@ def get_tree_for_letter(starting_letter): expected_data_file_name = "DataFiles/" + str(starting_letter) + "-data.node.bin" expected_data_file_path = os.path.join(base_directory, expected_data_file_name) if os.path.isfile(expected_data_file_path): - return deserialize_from_file(expected_data_file_path) + return node_from_file(expected_data_file_path) # If word list exists for letter, build the data file and return node expected_word_list_name = "TextFiles/" + str(starting_letter) + "-words.txt" expected_word_list_path = os.path.join(base_directory, expected_word_list_name) if os.path.isfile(expected_word_list_path): node = build_tree(expected_word_list_path) - serialize_to_file(node, expected_data_file_path) + node_to_file(node, expected_data_file_path) return node # If the master list does not exist, cannot continue @@ -265,15 +259,10 @@ def get_tree_for_letter(starting_letter): if letter == "": print("Computing possible word compinations...") break - if letter not in string.ascii_uppercase: + if (len(letter) != 1) or (letter not in string.ascii_uppercase): print("Invalid input - must be a letter [A-Z]!") else: letters.append(letter) - - output = "Current letters: " - for letter in letters: - output += letter + " " - print(output) + print("Current Letters: " + " ".join(letters)) - answers = solve(letters) - print_solution(answers) + print_solution(solve(letters))