Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Radiation Search by libertypi
from itertools import product
def checkio(matrix):
stack = []
groups = dict()
length = len(matrix)
for row, col in product(range(length), range(length)):
stack.append((row, col))
groups[(row, col)] = set()
num = matrix[row][col]
while stack:
currentRow, currentCol = stack.pop()
groups[(row, col)].add((currentRow, currentCol))
for y, x in (
(currentRow - 1, currentCol),
(currentRow + 1, currentCol),
(currentRow, currentCol - 1),
(currentRow, currentCol + 1),
):
if (
0 <= x < length
and 0 <= y < length
and (y, x) not in groups[(row, col)]
and matrix[y][x] == num
):
stack.append((y, x))
return max(
([len(v), matrix[x][y]] for k, v in groups.items() for x, y in v),
key=lambda s: s[0],
)
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([
[1, 2, 3, 4, 5],
[1, 1, 1, 2, 3],
[1, 1, 1, 2, 2],
[1, 2, 2, 2, 1],
[1, 1, 1, 1, 1]
]) == [14, 1], "14 of 1"
assert checkio([
[2, 1, 2, 2, 2, 4],
[2, 5, 2, 2, 2, 2],
[2, 5, 4, 2, 2, 2],
[2, 5, 2, 2, 4, 2],
[2, 4, 2, 2, 2, 2],
[2, 2, 4, 4, 2, 2]
]) == [19, 2], '19 of 2'
July 13, 2020