Solution to problem on slide 4:
Iterate along the grid, check if each filled in cell's neighbor is filled in. If not then there exists an edge there. You always have an edge at the edge of the grid
class Solution(object): def islandPerimeter(self, grid): n,m = len(grid),len(grid[0]) ans=0 for i in range(n): for j in range(m): if grid[i][j]==1: if i-1<0 or grid[max(0,i-1)][j]==0: ans+=1 if i+1>=n or grid[min(n-1,i+1)][j]==0: ans+=1 if j-1<0 or grid[i][max(0,j-1)]==0: ans+=1 if j+1>=m or grid[i][min(m-1,j+1)]==0: ans+=1 return ans Solution to problem on slide 5 (HW):
Iterate through the 2d array. For existing cells in the 3x3 convolution count the sum and total number of them then divide to fill the cell. class Solution(object): def imageSmoother(self, img): res = [ [0]*len(img[0]) for _ in range(len(img)) ] n = len(img) m = len(img[0]) for i in range(n): for j in range(m): value, count = 0,0 for ti in range(max(0,i-1),min(n,i+2)): for tj in range(max(0,j-1),min(m,j+2)): value+=img[ti][tj] count+=1 res[i][j] = value // count return res