Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Radiation Search by fuzzyone
def checkio(data):
def collect(i, j):
num = data[i][j]
res = [1, num]
data[i][j] = 0
for (k, l) in [(i-1,j), (i+1,j), (i,j-1), (i,j+1)]:
if k < 0 or l < 0 or k >= len(data) or l >= len(data[k]):
continue
try:
if data[k][l] == num:
res[0] += collect(k,l)[0]
except IndexError:
pass
return res
result = []
for row in range(len(data)):
for i in range(len(data[row])):
if data[row][i]:
result.append(collect(row, i))
return max(result, key=lambda res: res[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'
Oct. 12, 2015