:param words: A words Trie to search for words in :param clue: The clue to search for the solution in :returns: A generator of possible solutions ''' stack = [('', clue)] while stack: word, hint = stack.pop() path = words.get_path(word) if path.has_a_word(): yield word else: for i in range(len(hint) - 1, -1, -1): if path.has_path(hint[i]): stack.append((word + hint[i], hint[i + 1:])) # ------------------------------------------------------------ # constants # ------------------------------------------------------------ WORDS = set(Words.get_word_list('celebrities')) HEADLINE1 = "celeb singer did pornos" HEADLINE2 = "stardom mythical for designer" if __name__ == "__main__": clue = prepare_clue(HEADLINE2) words = Trie() words.add_words(WORDS) for solution in find_hidden_words(words, clue): print_solution(clue, solution)
while stack: worda, wordb, search = stack.pop() patha = words.get_path(worda) pathb = words.get_path(wordb) if patha.has_a_word() and pathb.has_a_word(): return (hidden, worda, wordb) else: for i in range(0, len(search)): if patha.has_path(search[i]): stack.append((worda + search[i], wordb, search[i + 1:])) if pathb.has_path(search[i]): stack.append((worda, wordb + search[i], search[i + 1:])) # ------------------------------------------------------------ # constants # ------------------------------------------------------------ HIDDEN = [ 'thiecbeigachgiell', 'betfworilesigunshett', 'tcrafafrisc', 'tlawbyirsintther' ] WORDS = set(Words.get_word_list('movies')) if __name__ == "__main__": words = Trie() words.add_words(WORDS) for hidden in HIDDEN: solution = find_solutions(words, hidden) print_solution(solution)
return [] def print_solution(solution): ''' Given an original word and the possible solutions, print them out. :param solutions: The final solutions for the problem ''' for original, letter, solution in solution[0]: print "%s + %s → %s" % (original, letter, solutions) print '= %s' % solution[1] # ------------------------------------------------------------ # constants # ------------------------------------------------------------ WORDS = set(Words.get_word_list()) M_LOOKUP = Words.generate_missing_lookup(WORDS) W_LOOKUP = Words.generate_anagram_lookup(WORDS) MISSING = [ 'sarong', 'gown', 'shirt', 'raincoat', 'apron', 'sweater', 'cape' ] if __name__ == "__main__": import pickle anagram_trie = Trie()