Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Calculate Islands solution in Creative category for Calculate Islands by capback250
# migrated from python 2.7
from functools import reduce
COORDS = ((-1,-1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1))
def checkio(mtx):
numerator = 2
icelands = []
for i1, k1 in enumerate(mtx):
for i2, k2 in enumerate(k1):
if k2 == 1:
neibors(mtx, i1, i2, numerator)
numerator += 1
flat = reduce(list.__add__, [x for x in mtx])
for x in range(2, max(flat)+1):
icelands.append(str(flat).count(str(x)))
return sorted(icelands)
def neibors(mtx, i1, i2, numerator):
if mtx[i1][i2] != 1:
return
mtx[i1][i2] = numerator
for c in COORDS:
x, y = i1 + c[0], i2 + c[1]
if not (x < 0 or y < 0) and x < len(mtx) and y < len(mtx[0]):
neibors(mtx, x, y, numerator)
Jan. 17, 2016