MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / find_repeat

Function find_repeat

matrix/matrix_based_game.py:128–161  ·  view source on GitHub ↗

Finds all connected elements of the same type from a given position. >>> find_repeat([['A', 'B', 'A'], ['A', 'B', 'A'], ['A', 'A', 'A']], 0, 0, 3) {(1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (2, 2), (1, 0)} >>> find_repeat([['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']], 1, 1, 3)

(
    matrix_g: list[list[str]], row: int, column: int, size: int
)

Source from the content-addressed store, hash-verified

126
127
128def find_repeat(
129 matrix_g: list[list[str]], row: int, column: int, size: int
130) -> set[tuple[int, int]]:
131 """
132 Finds all connected elements of the same type from a given position.
133
134 >>> find_repeat([['A', 'B', 'A'], ['A', 'B', 'A'], ['A', 'A', 'A']], 0, 0, 3)
135 {(1, 2), (2, 1), (0, 0), (2, 0), (0, 2), (2, 2), (1, 0)}
136 >>> find_repeat([['-', '-', '-'], ['-', '-', '-'], ['-', '-', '-']], 1, 1, 3)
137 set()
138 """
139
140 column = size - 1 - column
141 visited = set()
142 repeated = set()
143
144 if (color := matrix_g[column][row]) != "-":
145
146 def dfs(row_n: int, column_n: int) -> None:
147 if row_n < 0 or row_n >= size or column_n < 0 or column_n >= size:
148 return
149 if (row_n, column_n) in visited:
150 return
151 visited.add((row_n, column_n))
152 if matrix_g[row_n][column_n] == color:
153 repeated.add((row_n, column_n))
154 dfs(row_n - 1, column_n)
155 dfs(row_n + 1, column_n)
156 dfs(row_n, column_n - 1)
157 dfs(row_n, column_n + 1)
158
159 dfs(column, row)
160
161 return repeated
162
163
164def increment_score(count: int) -> int:

Callers 1

playFunction · 0.85

Calls 1

dfsFunction · 0.70

Tested by

no test coverage detected