def test_search_tree(): phrase = "appleandlamas" HashProp.set_hash_obj(HashProp("plain", "data/sample-to-find-3.txt")) phrase_dict, phrase_len = utils.phrase_to_dict(phrase) letter_tree, words = LetterBranch.parse_words(phrase_dict, "data/sample") LetterBranch.set_letter_tree(letter_tree) word_tree, word_tree_children = WordBranch.get_word_tree_root( phrase_len, phrase_dict, words) # Check I got the 'apple' word_branch. word = word_tree_children[2] assert str(word.letter_branch) == 'apple'
def test_complex_branching_3(): HashProp.set_hash_obj(HashProp("plain", "data/sample2-to-find-3.txt")) phrase = "andlamasgroves" phrase_dict, phrase_len = utils.phrase_to_dict(phrase) print(phrase_dict) print(phrase_len) letter_tree, words = LetterBranch.parse_words(phrase_dict, "data/sample2") LetterBranch.set_letter_tree(letter_tree) word_tree, word_tree_children = WordBranch.get_word_tree_root( phrase_len, phrase_dict, words) construct_word_tree_start(word_tree, word_tree_children) anagrams = search_solved_anagrams_start(word_tree, word_tree_children) for anagram in anagrams: print(anagram) assert len(anagrams) == 7
def test_anagram_solutions_4(): HashProp.set_hash_obj(HashProp("plain", "data/sample-to-find-4.txt")) phrase = "andapplegroveslamas" phrase_dict, phrase_len = utils.phrase_to_dict(phrase) print(phrase_dict) print(phrase_len) letter_tree, words = LetterBranch.parse_words(phrase_dict, "data/sample") LetterBranch.set_letter_tree(letter_tree) word_tree, word_tree_children = WordBranch.get_word_tree_root( phrase_len, phrase_dict, words) construct_word_tree_start(word_tree, word_tree_children) ''' Should produce 24 solutions: ''' anagrams = search_solved_anagrams_start(word_tree, word_tree_children) for anagram in anagrams: print(anagram) assert len(anagrams) == 24
def search_solved_anagrams(anagram_str, word_branch, max_level, level=2): '''One level DFS in word tree. Args anagram_str (string) The current build string. word_branch (WordBranch) The branch we're looking at in DFS. max_level (int) The max depth to search in the tree. level (int) The level in the DFS / tree we're in. Returns ([string]) Array of strings that matched the hash object. (int) State of program {1: continue, 2: continue with increased max_level, 3: done all hashes found} ''' anagrams = [] state = 1 if word_branch.valid_children == None: return [], state if level > max_level: return [], 2 for word_branch in word_branch.valid_children: new_anagram_str = anagram_str + ' ' + str(word_branch.letter_branch) if word_branch.remain_char == 0: if HashProp.valid_candidate(new_anagram_str): print(HashProp.get_hash_str(new_anagram_str), " --> ", new_anagram_str) anagrams.append(new_anagram_str) hash_obj = HashProp.get_hash_obj() if hash_obj.count == 0: return anagrams, 3 else: new_anagrams, new_state = search_solved_anagrams( new_anagram_str, word_branch, max_level, level + 1) anagrams = anagrams + new_anagrams if new_state > state: state = new_state if state == 3: # Terminate search return anagrams, state return anagrams, state
return anagrams, state '''Run''' if __name__ == "__main__": args = sys.argv if len(args) == 5: start_time = timeit.default_timer() # Time phrase = args[1] wordlist_filename = args[2] hash_algo = args[3] hash_filename = args[4] phrase_dict, phrase_len = utils.phrase_to_dict(phrase) hash_obj = HashProp(hash_algo, hash_filename) HashProp.set_hash_obj(hash_obj) letter_tree, words = LetterBranch.parse_words(phrase_dict, wordlist_filename) LetterBranch.set_letter_tree(letter_tree) word_tree, word_tree_children = WordBranch.get_word_tree_root( phrase_len, phrase_dict, words) construct_word_tree_start(word_tree, word_tree_children) anagrams = search_solved_anagrams_start(word_tree, word_tree_children) elapsed = timeit.default_timer() - start_time # Time print('Time elapsed --> ', elapsed, 'seconds') else: