Beispiel #1
0
def test_to_number_grid():
    assert Parser.from_lines(["123", "456"]).to_number_grid() == \
           Grid.from_values([[1, 2, 3], [4, 5, 6]])
    assert Parser.from_lines(["1 2 3", "4 5 6"]).to_number_grid(separator=None) == \
           Grid.from_values([[1, 2, 3], [4, 5, 6]])
    assert Parser.from_lines(["1, 2, 3", "4, 5, 6"]).to_number_grid(separator=",") == \
           Grid.from_values([[1, 2, 3], [4, 5, 6]])
Beispiel #2
0
def test_data_class_features(test_grid: Grid[int]):
    # Grids should be equal if their data is equal
    assert test_grid == Grid.from_values([[1, 2, 3], [4, 5, 6]])

    # Normal Grid cannot be hashed, since it's mutable
    with pytest.raises(TypeError):
        hash(test_grid)
    # But FrozenGrid can be hashed (still mutable, but at your own risk)
    hash(test_grid.freeze())
Beispiel #3
0
def test_to_directed_weighted_graph(test_grid: Grid[int]):
    graph_not_diagonal = test_grid.to_directed_weighted_graph(include_diagonal=False)
    assert sorted(graph_not_diagonal.get_nodes()) == [0, 1, 2, 3, 4, 5]
    assert graph_not_diagonal.get_edges() == [
        ((0, 1), 2), ((0, 3), 4), ((1, 0), 1), ((1, 2), 3), ((1, 4), 5),
        ((2, 1), 2), ((2, 5), 6), ((3, 0), 1), ((3, 4), 5),
        ((4, 1), 2), ((4, 3), 4), ((4, 5), 6), ((5, 2), 3), ((5, 4), 5)
    ]

    graph_diagonal = test_grid.to_directed_weighted_graph(include_diagonal=True)
    assert sorted(graph_diagonal.get_nodes()) == [0, 1, 2, 3, 4, 5]
    assert graph_diagonal.get_edges() == [
        ((0, 1), 2), ((0, 3), 4), ((0, 4), 5), ((1, 0), 1), ((1, 2), 3), ((1, 3), 4), ((1, 4), 5), ((1, 5), 6),
        ((2, 1), 2), ((2, 4), 5), ((2, 5), 6), ((3, 0), 1), ((3, 1), 2), ((3, 4), 5),
        ((4, 0), 1), ((4, 1), 2), ((4, 2), 3), ((4, 3), 4), ((4, 5), 6), ((5, 1), 2), ((5, 2), 3), ((5, 4), 5)
    ]

    with pytest.raises(ValueError):
        Grid.from_values([["a", "b"]]).to_directed_weighted_graph(include_diagonal=False)
Beispiel #4
0
    def to_number_grid(self, separator: Optional[str] = "") -> Grid[int]:
        def split_line(line: str) -> List[str]:
            if separator == "":
                # Empty string means: no separator, take character by character
                return [char for char in line]
            else:
                # None means: default of split, which is any whitespace
                # Any other value means: use this specific separator to split.
                return line.split(separator)

        return Grid.from_values(
            [[int(word.strip()) for word in split_line(line)]
             for line in self.lines])
Beispiel #5
0
    def __init__(self, problem: Problem, heuristic_type: HeuristicType = HeuristicType.Exhaustive, enable_sorting=False,
                 enable_matchingID=False):
        """
        Create a problem that uses matching ID, only makes sense for Exhaustive matching.
        :param problem: The problem to solve.
        :param heuristic_type: The heuristic type.
        """
        self.grid = Grid(problem.grid, problem.width, problem.height, problem.starts, problem.goals, heuristic_type)
        self.heuristic_type = heuristic_type
        self.enable_sorting = enable_sorting
        self.enable_matchingID = enable_matchingID

        if enable_matchingID:
            max_team = max(map(lambda x: x.color, self.grid.starts))
            teams = [list() for _ in range(max_team + 1)]
            for i, start in enumerate(self.grid.starts):
                teams[start.color].append(i)
            self.teams = list(map(Group, filter(lambda x: len(x) > 0, teams)))
Beispiel #6
0
def test_to_character_grid():
    assert Parser.from_lines(["..#",
                              "#.#"]).to_character_grid() == Grid.from_values(
                                  [[".", ".", "#"], ["#", ".", "#"]])
Beispiel #7
0
def test_grid() -> Grid[int]:
    return Grid.from_values([[1, 2, 3], [4, 5, 6]])
Beispiel #8
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)]
Beispiel #9
0
def test_fill_grid():
    grid = Grid.fill_grid(2, 3, 0)
    assert grid.height == 2
    assert grid.width == 3
    assert grid.get_all_values() == [0, 0, 0, 0, 0, 0]
Beispiel #10
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)]
    ])
Beispiel #11
0
def bit_test_grid() -> Grid[int]:
    return Grid.from_values([[1, 1, 1, 1], [0, 1, 0, 0], [1, 1, 0, 0]])
Beispiel #12
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")]]
Beispiel #13
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)]
    ])
Beispiel #14
0
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)]
    ])
Beispiel #15
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)]])
Beispiel #16
0
def test_validate():
    with pytest.raises(ValueError):
        Grid.from_values([])
    with pytest.raises(ValueError):
        Grid.from_values([[1, 2], [3]])
Beispiel #17
0
 def to_character_grid(self) -> Grid[str]:
     return Grid.from_values([[character for character in line]
                              for line in self.lines])
Beispiel #18
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)]
    ])
Beispiel #19
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)]
    ])
Beispiel #20
0
def test_count_value(test_grid: Grid[int]):
    grid = Grid.from_values([[1, 1, 0, 1], [1, 0, 1, 0]])
    assert grid.count_value(0) == 3
    assert grid.count_value(1) == 5
    assert grid.count_value(2) == 0