Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for Calculate Islands by vikulin
from itertools import product
def checkio(land_map):
# a fast copy of land_map, because we will destruct it
m = [x[:] for x in land_map]
H, W = len(m), len(m[0])
neighbours = set(product([-1, 0, 1], repeat=2)) - set(((0, 0), ))
def island(m, r, c):
m[r][c] = 0
return 1 + sum(island(m, r+i, c+j)
for (i, j) in neighbours
if 0 <= r+i < H and 0 <= c+j < W and m[r+i][c+j] == 1)
return sorted(island(m, r, c)
for c in range(W)
for r in range(H)
if m[r][c] == 1)
Oct. 24, 2015