Google | Tech Screen | SDE 1
Anonymous User
665

PYTHON

Implement a function that puts land 1 into the given cell and returns a resulting number of islands in the ocean. Adjacent 1s form a single island.

matrix = [
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
]

def put_land(row, col):

print(put_land(0,0)) # returns 1,new island, bounds checking
print(put_land(1,1)) # returns 2, also a new island
print(put_land(1,0)) # returns 1,two previous now form a single
print(put_land(4,3)) # returns 2, new island, bounds checking
print(put_land(3,4)) # returns 3, new island, bounds checking

Comments (6)