예제 #1
0
def main():
    words_dictionary = Trie()
    file_name = sys.argv[1]
    lowercase_letters = string.ascii_lowercase
    try:
        with open(file_name, 'r') as file_handle:
            for word in file_handle:
                words_dictionary.insert_word(word.rstrip())
    except FileNotFoundError:
        print("The file name provided doesn't exist. Try again!")
        sys.exit()

    word_search_game = WordSearch(words_dictionary)

    while True:
        cmd = int(input("Enter 1 to play else any other digit to exit! : "))
        if cmd == 1:
            num_rows = int(
                input("Please enter number of rows for the grid : "))
            num_cols = int(
                input("Please enter number of columns for the grid : "))
            start = time.time()
            grid = [[
                random.choice(lowercase_letters) for i in range(num_cols)
            ] for j in range(num_rows)]

            print('\n')
            for row in grid:
                print(row)

            print(word_search_game.search_valid_words(grid))
            print("\nTotal execution time : ", time.time() - start, "\n")

        else:
            sys.exit()
예제 #2
0
    def test_word_search(self):
        words_dictionary = Trie()
        words = ["naruto", "naru", "leemo", "hokage", "leaf", "sakura", \
                "saskent", "kiba", "lee", "noji", "leen"]
        for word in words:
            words_dictionary.insert_word(word)
        word_search_game = WordSearch(words_dictionary)

        grid = [['n', 'j', 'r', 'u'],
                ['o', 'a', 'i', 't'],
                ['l', 'e', 'o', 'p'],
                ['e', 'n', 'm', 'q']]

        valid_words = set(["noji", "naru", "naruto", "lee", "leen", "leemo"])
        ret_words = word_search_game.search_valid_words(grid)
        for word in ret_words:
            if word in valid_words:
                valid_words.remove(word)
            else:
                valid_words.add(word)

        self.assertEqual(len(valid_words), 0)