Esempio n. 1
0
def construct_matrix(input_csv):
  letter_array = []
  try:
    with open(input_csv) as csv_file:
      reader = csv.reader(csv_file)
      for row in reader:
        letter_array.append(row)
    return LetterMatrix(letter_array)
  except Exception as e:
    print("Encountered error when constructing matrix: {}".format(e))
    sys.exit(1)
Esempio n. 2
0
    def test_find_words_with_prefix(self):
        row_matrix = LetterMatrix(
            [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'A', 'I', 'J']])

        assert_equal(row_matrix.find_words(1, 'Z'), [])
        assert_equal(row_matrix.find_words(1, 'AB'), [])
        assert_equal(row_matrix.find_words(1, 'B'), ['B'])
        assert_equal(row_matrix.find_words(2, 'C'), ['CB', 'CD'])
        assert_equal(row_matrix.find_words(3, 'A'), ['ABC', 'AHG', 'AIJ'])
        assert_equal(row_matrix.find_words(3, 'ai'), ['AIJ'])
Esempio n. 3
0
 def test_find_words_no_prefix(self):
     small_matrix = LetterMatrix([['A', 'B'], ['', 'C']])
     assert_equal(small_matrix.find_words(0), [])
     assert_equal(small_matrix.find_words(1), ['A', 'B', 'C'])
     assert_equal(small_matrix.find_words(2),
                  ['AB', 'AC', 'BA', 'BC', 'CA', 'CB'])
     assert_equal(small_matrix.find_words(3),
                  ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA'])
     assert_equal(small_matrix.find_words(4), [])
Esempio n. 4
0
class TestLetterMatrix():

    fixture = LetterMatrix([
        ['', 'A', 'B', 'C', 'D'],
        ['E', 'F', '', 'G', ''],
        ['H', 'I', 'J', 'K', 'L'],
        ['', '', '', '', ''],
        ['M', '', 'N', '', 'O'],
        ['P', 'Q', 'R', 'S', ''],
    ])

    def test_validate_row_length(self):
        cases = [
            (True, []),
            (True, [[]]),
            (True, [['']]),
            (True, [
                ['', '', ''],
                ['', '', ''],
                ['', '', ''],
            ]),
            (False, [[''], []]),
            (False, [[''], [''], ['', '']]),
        ]
        for (passes, matrix) in cases:
            yield self.letter_matrix_validation, matrix, passes

    def test_validate_contents(self):
        cases = [
            (True, []),
            (True, [['A', 'a']]),
            (True, [['A', 'a'], ['', ''], ['B', 'Z']]),
            (False, [[' ']]),
            (False, [['9']]),
            (False, [['.']]),
            (False, [['ABC']]),
            (False, ['ABC']),
        ]
        for (passes, matrix) in cases:
            yield self.letter_matrix_validation, matrix, passes

    def letter_matrix_validation(self, matrix, passes):
        if passes:
            assert LetterMatrix.validate(matrix)
        else:
            assert_raises(InvalidLetterMatrixException, LetterMatrix.validate,
                          matrix)

    def test_element(self):
        assert_equal(self.fixture.element(1, 2), '')
        assert_equal(self.fixture.element(1, 3), 'G')
        assert_equal(self.fixture.element(5, 0), 'P')
        assert_raises(IndexError, self.fixture.element, 6, 0)
        assert_raises(IndexError, self.fixture.element, 0, 5)

    def test_word_at(self):
        assert_equal(self.fixture.word_at([(0, 0)]), '')
        assert_equal(self.fixture.word_at([(1, 1), (1, 2), (1, 3)]), 'FG')

    def test_exists(self):
        assert self.fixture.exists(0, 0)
        assert self.fixture.exists(5, 4)
        assert not self.fixture.exists(6, 4)
        assert not self.fixture.exists(5, 5)
        assert not self.fixture.exists(-1, 0)
        assert not self.fixture.exists(0, -1)

    def test_neighbors(self):
        assert_equal(self.fixture.neighbors(0, 0), [(0, 1), (1, 0), (1, 1)])
        assert_equal(self.fixture.neighbors(2, 1), [(1, 0), (1, 1), (2, 0),
                                                    (2, 2)])

    def test_rec_find_word_coords_no_prefix(self):
        # If no words of the given length can be made, the empty list
        # is returned
        assert_equal(
            sorted(
                list(
                    self.fixture._rec_find_word_coords(100, 4, 0, {(4, 0)},
                                                       ""))), [])
        assert_equal(
            sorted(
                list(self.fixture._rec_find_word_coords(0, 4, 0, {(4, 0)},
                                                        ""))), [])
        assert_equal(
            sorted(
                list(self.fixture._rec_find_word_coords(
                    -6, 4, 0, {(4, 0)}, ""))), [])
        assert_equal(
            sorted(
                list(self.fixture._rec_find_word_coords(1, 4, 0, {(4, 0)},
                                                        ""))), [[(4, 0)]])
        assert_equal(
            sorted(
                list(self.fixture._rec_find_word_coords(2, 4, 0, {(4, 0)},
                                                        ""))),
            [[(4, 0), (5, 0)], [(4, 0), (5, 1)]])
        assert_equal(
            sorted(
                list(self.fixture._rec_find_word_coords(3, 4, 0, {(4, 0)},
                                                        ""))), [
                                                            [(4, 0), (5, 0),
                                                             (5, 1)],
                                                            [(4, 0), (5, 1),
                                                             (4, 2)],
                                                            [(4, 0), (5, 1),
                                                             (5, 0)],
                                                            [(4, 0), (5, 1),
                                                             (5, 2)],
                                                        ])

    def test_rec_find_word_coords_with_prefix(self):
        assert_equal(
            sorted(
                list(
                    self.fixture._rec_find_word_coords(1, 4, 0, {(4, 0)},
                                                       "MP"))), [])
        assert_equal(
            sorted(
                list(self.fixture._rec_find_word_coords(
                    1, 4, 0, {(4, 0)}, "K"))), [])
        assert_equal(
            sorted(
                list(self.fixture._rec_find_word_coords(
                    1, 4, 0, {(4, 0)}, "M"))), [[(4, 0)]])
        assert_equal(
            sorted(
                list(
                    self.fixture._rec_find_word_coords(3, 4, 0, {(4, 0)},
                                                       "MQ"))), [
                                                           [(4, 0), (5, 1),
                                                            (4, 2)],
                                                           [(4, 0), (5, 1),
                                                            (5, 0)],
                                                           [(4, 0), (5, 1),
                                                            (5, 2)],
                                                       ])
        assert_equal(
            sorted(
                list(
                    self.fixture._rec_find_word_coords(3, 4, 0, {(4, 0)},
                                                       "MQR"))), [[(4, 0),
                                                                   (5, 1),
                                                                   (5, 2)]])

    def test_find_words_no_prefix(self):
        small_matrix = LetterMatrix([['A', 'B'], ['', 'C']])
        assert_equal(small_matrix.find_words(0), [])
        assert_equal(small_matrix.find_words(1), ['A', 'B', 'C'])
        assert_equal(small_matrix.find_words(2),
                     ['AB', 'AC', 'BA', 'BC', 'CA', 'CB'])
        assert_equal(small_matrix.find_words(3),
                     ['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA'])
        assert_equal(small_matrix.find_words(4), [])

    def test_find_words_with_prefix(self):
        row_matrix = LetterMatrix(
            [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'A', 'I', 'J']])

        assert_equal(row_matrix.find_words(1, 'Z'), [])
        assert_equal(row_matrix.find_words(1, 'AB'), [])
        assert_equal(row_matrix.find_words(1, 'B'), ['B'])
        assert_equal(row_matrix.find_words(2, 'C'), ['CB', 'CD'])
        assert_equal(row_matrix.find_words(3, 'A'), ['ABC', 'AHG', 'AIJ'])
        assert_equal(row_matrix.find_words(3, 'ai'), ['AIJ'])

    def test_next_letter_above(self):
        assert_equal(self.fixture._next_letter_above(4, 4), None)
        assert_equal(self.fixture._next_letter_above(5, 1), None)
        assert_equal(self.fixture._next_letter_above(0, 0), (1, 0))
        assert_equal(self.fixture._next_letter_above(2, 3), (5, 3))

    def test_collapse_columns(self):
        matrix = copy.deepcopy(self.fixture)
        matrix.collapse_columns()
        assert_equal(matrix.matrix, [
            ['E', 'A', 'B', 'C', 'D'],
            ['H', 'F', 'J', 'G', 'L'],
            ['M', 'I', 'N', 'K', 'O'],
            ['P', 'Q', 'R', 'S', ''],
            ['', '', '', '', ''],
            ['', '', '', '', ''],
        ])
Esempio n. 5
0
 def letter_matrix_validation(self, matrix, passes):
     if passes:
         assert LetterMatrix.validate(matrix)
     else:
         assert_raises(InvalidLetterMatrixException, LetterMatrix.validate,
                       matrix)
 def test_empty(self):
   matrix = LetterMatrix([[]])
   solutions = wordfinder.solve_word_game(matrix, [5], None)
   assert_equal([], solutions)
  def test_two_by_two(self):
    matrix = LetterMatrix([['A','B'],['C', 'D']])
    assert_equal(
      [
        ['AB', 'CD'],
        ['AB', 'DC'],
        ['AC', 'BD'],
        ['AC', 'DB'],
        ['AD', 'BC'],
        ['AD', 'CB'],
        ['BA', 'CD'],
        ['BA', 'DC'],
        ['BC', 'AD'],
        ['BC', 'DA'],
        ['BD', 'AC'],
        ['BD', 'CA'],
        ['CA', 'BD'],
        ['CA', 'DB'],
        ['CB', 'AD'],
        ['CB', 'DA'],
        ['CD', 'AB'],
        ['CD', 'BA'],
        ['DA', 'BC'],
        ['DA', 'CB'],
        ['DB', 'AC'],
        ['DB', 'CA'],
        ['DC', 'AB'],
        ['DC', 'BA'],
      ], 
      sorted(wordfinder.solve_word_game(matrix, [2, 2], None))
    )

    assert_equal(
      [
        ['A', 'BC'],
        ['A', 'BD'],
        ['A', 'CB'],
        ['A', 'CD'],
        ['A', 'DB'],
        ['A', 'DC'],
        ['B', 'AC'],
        ['B', 'AD'],
        ['B', 'CA'],
        ['B', 'CD'],
        ['B', 'DA'],
        ['B', 'DC'],
        ['C', 'AB'],
        ['C', 'AD'],
        ['C', 'BA'],
        ['C', 'BD'],
        ['C', 'DA'],
        ['C', 'DB'],
        ['D', 'AB'],
        ['D', 'AC'],
        ['D', 'BA'],
        ['D', 'BC'],
        ['D', 'CA'],
        ['D', 'CB'],
      ], 
      sorted(wordfinder.solve_word_game(matrix, [1, 2], None))
    )
 def test_single(self):
   matrix = LetterMatrix([['A','B','C']])
   solutions = sorted(wordfinder.solve_word_game(matrix, [3], None))
   assert_equal([['ABC'], ['CBA']], solutions)