Exemple #1
0
 def to_coordinate_list(self,
                        separator: str = _DEFAULT_SEPARATOR
                        ) -> List[Coordinate]:
     return [
         Coordinate.from_point(numbers[0], numbers[1])
         for numbers in self.to_number_lists(separator)
     ]
def test_get_local_area_cells(test_grid: Grid[int]):
    assert test_grid.get_local_area_cells(Coordinate(0, 1),
                                          lambda cell1, cell2: cell1.value < cell2.value,
                                          include_diagonal=False) == [
               Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3),
               Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)
           ]
    assert test_grid.get_local_area_cells(Coordinate(1, 1),
                                          lambda cell1, cell2: abs(cell1.value - cell2.value) == 4,
                                          include_diagonal=True) == [
               Cell(Coordinate(0, 0), 1), Cell(Coordinate(1, 1), 5)
           ]
def test_copy(test_grid: Grid[int]):
    assert test_grid.copy().rows == [
        [Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3)],
        [Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)]
    ]
    assert not test_grid.copy() is test_grid
    assert not test_grid.copy() is test_grid.copy()
Exemple #4
0
def test_neighbors():
    coord = Coordinate.from_point(x=2, y=2)

    assert coord.get_neighbor_left().x == 1
    assert coord.get_neighbor_left().y == 2
    assert coord.get_neighbor_right().x == 3
    assert coord.get_neighbor_right().y == 2
    assert coord.get_neighbor_above().x == 2
    assert coord.get_neighbor_above().y == 1
    assert coord.get_neighbor_below().x == 2
    assert coord.get_neighbor_below().y == 3

    assert coord.get_neighbor_left_above().x == 1
    assert coord.get_neighbor_left_above().y == 1
    assert coord.get_neighbor_right_above().x == 3
    assert coord.get_neighbor_right_above().y == 1
    assert coord.get_neighbor_left_below().x == 1
    assert coord.get_neighbor_left_below().y == 3
    assert coord.get_neighbor_right_below().x == 3
    assert coord.get_neighbor_right_below().y == 3

    non_diagonal_neighbors = [
        coord.get_neighbor_left(),
        coord.get_neighbor_right(),
        coord.get_neighbor_above(),
        coord.get_neighbor_below()
    ]
    assert coord.get_neighbors(
        include_diagonal=False) == non_diagonal_neighbors
    assert coord.get_neighbors(
        include_diagonal=True) == non_diagonal_neighbors + [
            coord.get_neighbor_left_above(),
            coord.get_neighbor_right_above(),
            coord.get_neighbor_left_below(),
            coord.get_neighbor_right_below()
        ]
Exemple #5
0
def move_to_down(c: Coordinate, g: Grid):
    new_row = c.row + 1
    if new_row >= g.height:
        new_row = 0
    return Coordinate(new_row, c.column)
Exemple #6
0
def test_to_coordinate_list():
    assert Parser.from_lines(["1 2", "3 4"]).to_coordinate_list() == \
           [Coordinate.from_point(1, 2), Coordinate.from_point(3, 4)]
    assert Parser.from_lines(["1, 2", "3, 4"]).to_coordinate_list(separator=",") == \
           [Coordinate.from_point(1, 2), Coordinate.from_point(3, 4)]
def test_get_all_cells(test_grid: Grid[int]):
    assert test_grid.get_all_cells() == [
        Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3),
        Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)
    ]
def test_get_value_by_coord(test_grid: Grid[int]):
    assert test_grid.get_value_by_coord(Coordinate(0, 0)) == 1
def test_rotate_left(test_grid: Grid[int]):
    assert test_grid.rotate_left(1) == Grid([
        [Cell(Coordinate(0, 0), 3), Cell(Coordinate(0, 1), 6)],
        [Cell(Coordinate(1, 0), 2), Cell(Coordinate(1, 1), 5)],
        [Cell(Coordinate(2, 0), 1), Cell(Coordinate(2, 1), 4)]
    ])
Exemple #10
0
def test_filter_rows(test_grid: Grid[int]):
    assert test_grid.filter_rows(lambda row: sum(cell.value for cell in row) > 8) == \
           Grid([[Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)]])
Exemple #11
0
def test_find_cells_by_value(test_grid: Grid[int]):
    assert test_grid.find_cells_by_value(1) == [Cell(Coordinate(0, 0), 1)]
    assert test_grid.find_cells_by_value([2, 4]) == [Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 0), 4)]
Exemple #12
0
def test_find_cells_by_predicate_on_value(test_grid: Grid[int]):
    assert test_grid.find_cells_by_predicate_on_value(lambda value: value % 2 == 0) == \
           [Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 2), 6)]
Exemple #13
0
def test_find_cells_by_predicate_on_coord(test_grid: Grid[int]):
    assert test_grid.find_cells_by_predicate_on_coord(lambda coord: coord.row + coord.column == 2) == \
           [Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 1), 5)]
Exemple #14
0
def test_find_cells_by_predicate_on_cell(test_grid: Grid[int]):
    assert test_grid.find_cells_by_predicate_on_cell(lambda cell: cell.value >= 3 and cell.coord.column >= 1) == \
           [Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)]
Exemple #15
0
def test_get_cell_by_coord(test_grid: Grid[int]):
    assert test_grid.get_cell_by_coord(Coordinate(0, 1)) == Cell(Coordinate(0, 1), 2)
Exemple #16
0
def test_flip_vertical(test_grid: Grid[int]):
    assert test_grid.flip_vertical() == Grid([
        [Cell(Coordinate(0, 0), 4), Cell(Coordinate(0, 1), 5), Cell(Coordinate(0, 2), 6)],
        [Cell(Coordinate(1, 0), 1), Cell(Coordinate(1, 1), 2), Cell(Coordinate(1, 2), 3)]
    ])
Exemple #17
0
def test_get_cells_by_coords(test_grid: Grid[int]):
    assert test_grid.get_cells_by_coords([Coordinate(0, 1), Coordinate(1, 2)]) == [
        Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 2), 6)
    ]
Exemple #18
0
def test_from_values():
    assert Grid.from_values([[0, 1]]).rows == [[Cell(Coordinate(0, 0), 0), Cell(Coordinate(0, 1), 1)]]
    assert Grid.from_values([["a", "b"]]).rows == [[Cell(Coordinate(0, 0), "a"), Cell(Coordinate(0, 1), "b")]]
Exemple #19
0
def test_get_values_by_coords(test_grid: Grid[int]):
    assert test_grid.get_values_by_coords([Coordinate(1, 1), Coordinate(0, 2)]) == [5, 3]
Exemple #20
0
def test_map_values_by_function(test_grid: Grid[int]):
    assert test_grid.map_values_by_function(lambda value: value + 1) == Grid([
        [Cell(Coordinate(0, 0), 2), Cell(Coordinate(0, 1), 3), Cell(Coordinate(0, 2), 4)],
        [Cell(Coordinate(1, 0), 5), Cell(Coordinate(1, 1), 6), Cell(Coordinate(1, 2), 7)]
    ])
Exemple #21
0
def test_get_all_coords(test_grid: Grid[int]):
    assert test_grid.get_all_coords() == [
        Coordinate(0, 0), Coordinate(0, 1), Coordinate(0, 2),
        Coordinate(1, 0), Coordinate(1, 1), Coordinate(1, 2)
    ]
Exemple #22
0
def test_map_values_by_dict(test_grid: Grid[int]):
    assert test_grid.map_values_by_dict({1: 11, 3: 33, 4: 44}) == Grid([
        [Cell(Coordinate(0, 0), 11), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 33)],
        [Cell(Coordinate(1, 0), 44), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)]
    ])
Exemple #23
0
 def from_values(cls, values: List[List[T]]) -> Grid[T]:
     Grid.validate(values)
     return Grid([[
         Cell(Coordinate(row_i, column_i), values[row_i][column_i])
         for column_i in range(len(values[0]))
     ] for row_i in range(len(values))])
Exemple #24
0
def test_map_cells_by_function(test_grid: Grid[int]):
    assert test_grid.map_cells_by_function(lambda cell: cell.value + cell.coord.row + cell.coord.column) == Grid([
        [Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 3), Cell(Coordinate(0, 2), 5)],
        [Cell(Coordinate(1, 0), 5), Cell(Coordinate(1, 1), 7), Cell(Coordinate(1, 2), 9)]
    ])
Exemple #25
0
def test_from_point():
    coord = Coordinate.from_point(x=1, y=2)
    assert coord.row == 2
    assert coord.column == 1
    assert coord.y == 2
    assert coord.x == 1
Exemple #26
0
def test_get_item(test_grid: Grid[int]):
    assert Grid.from_values([[0, 1]])[0] == [Cell(Coordinate(0, 0), 0), Cell(Coordinate(0, 1), 1)]
Exemple #27
0
def test_from_grid():
    coord = Coordinate.from_grid(row=1, column=2)
    assert coord.row == 1
    assert coord.column == 2
    assert coord.y == 1
    assert coord.x == 2
Exemple #28
0
def test_get_cell_by_index(test_grid: Grid[int]):
    assert test_grid.get_cell_by_index(1, 2) == Cell(Coordinate(1, 2), 6)
Exemple #29
0
def move_to_right(c: Coordinate, g: Grid):
    new_col = c.column + 1
    if new_col >= g.width:
        new_col = 0
    return Coordinate(c.row, new_col)
Exemple #30
0
def test_get_neighbor_values(test_grid: Grid[int]):
    assert test_grid.get_neighbor_values(Coordinate(0, 1), include_diagonal=False) == [1, 3, 5]