Example #1
0
 def test_not_equals(self):
     board = Board([['a']])
     board._board = [
         [1, 2, 3, 4],
         [5, 6, 7, 8],
         [9, 10, 11, 13],
     ]
     self.assertNotEqual(self.reference_board, board)
Example #2
0
 def test_construct_other_iterator(self):
     board = (
         (1, 2, 3, 4),
         (5, 6, 7, 8),
         (9, 10, 11, 12),
     )
     self.assertEqual(self.reference_board, Board(board))
Example #3
0
 def test_construct_uneven_rows(self):
     bad_board = [
         [1, 2, 3, 4],
         [5, 6, 7, 8],
         [9, 10, 11, 12, 13],
     ]
     self.assertRaises(ValueError, lambda: Board(bad_board))
Example #4
0
def main():
    args = parse_args()

    if args.wordsearch:
        # Parse the specified wordsearch file
        board = Board([list(line.strip()) for line in args.wordsearch])
    else:
        # Use a random board
        board = random_board(args.width, args.height)

    # Pickle trie for performance
    # The cache tracks the last modified time of the dictionary.
    # If there exists a pickle cache for the dictionary's name and the file has not been
    # changed since the last cache was made, load from the pickle cache instead.
    cache_name = args.dictionary.name + '.pickle'
    cache_valid = False  #In case the cache file doesn't exist
    if os.path.exists(cache_name):
        lastmtime, rootnode = pickle.load(open(cache_name, 'rb'))
        cache_valid = lastmtime >= os.path.getmtime(args.dictionary.name)

    if not cache_valid:
        rootnode = TrieNode()
        for word in args.dictionary:
            rootnode.index(word.strip())

        pickle.dump((os.path.getmtime(args.dictionary.name), rootnode),
                    open(cache_name, 'wb'))

    for word in search_board(board, rootnode):
        if len(word) >= args.min_word_length:
            print(word)
Example #5
0
 def test_construct(self):
     # Now that we have confidence about __eq__, use it to test __init__
     board = Board([
         [1, 2, 3, 4],
         [5, 6, 7, 8],
         [9, 10, 11, 12],
     ])
     self.assertEqual(self.reference_board, board)
Example #6
0
 def setUp(self):
     #Build a board then switch its internal list out so we're not accidentally
     #testing __init__ in setUp
     self.reference_board = Board([['a']])
     self.reference_board._board = [
         [1, 2, 3, 4],
         [5, 6, 7, 8],
         [9, 10, 11, 12],
     ]
Example #7
0
def random_board(width, height):
  """
  Returns a Board of random lowercase letters
  
  Args:
    width: width of the board
    height: height of the board
  """
  return Board([[random.choice(string.ascii_lowercase) for _ in range(width)] for _ in range(height)])
Example #8
0
    def test_search(self):
        root = TrieNode(words=['amp', 'ack', 'bus', 'bar'])
        board = Board([
            ['z', 'a', 'm', 'x'],
            ['s', 'a', 'u', 'b'],
            ['u', 'm', 'c', 'a'],
            ['b', 'p', 'a', 'k'],
        ])

        self.assertEqual(set(main.search_board(board, root)),
                         {'amp', 'ack', 'bus'})
Example #9
0
 def setUp(self):
     self.board = Board([
         [1, 2, 3],
         [4, 5, 6],
         [7, 8, 9],
     ])
Example #10
0
 def test_construct_zero_width_height(self):
     self.assertRaises(ValueError, lambda: Board([[]]))
     self.assertRaises(ValueError, lambda: Board([]))