We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defcavityMap(grid):# Write your code hereforiinrange(1,len(grid)-1):forjinrange(1,len(grid[i])-1):ifgrid[i][j]>max(grid[i-1][j],grid[i][j-1],grid[i+1][j],grid[i][j+1]):grid[i]=grid[i][:j]+'X'+grid[i][j+1:]returngrid
A pythonic solution
defcavityMap(grid):# Write your code hereforiinrange(1,len(grid)-1):forjinrange(1,len(grid[i])-1):ifgrid[i][j-1]<grid[i][j]andgrid[i-1][j]<grid[i][j]andgrid[i][j+1]<grid[i][j]andgrid[i+1][j]<grid[i][j]:grid[i]=''.join(['X'ifa==jelsedfora,dinenumerate(grid[i])])returngrid
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cavity Map
You are viewing a single comment's thread. Return to all comments →
Update: This is nice too
A pythonic solution