• + 0 comments

    my python approach, aiming for legibility mostly.

    def cavityMap(grid):
        # Write your code here
        
        cavity = grid.copy() # overwriting original grid would cause error
        
        for i in range(1, len(grid)-1): # iterate thru each row ignore first and last
            
            cur = grid[i]
            pre = grid[i-1]
            nex = grid[i+1]
            
            for j in range(1, len(cur)-1): # identify cell and its adjacencies
                
                cell = cur[j]
                left = cur[j-1]
                rght = cur[j+1]
                topp = pre[j]
                botm = nex[j]
                
                if cell > left and cell > rght and cell > topp and cell > botm:
                    cavity[i] = cavity[i][:j] + 'X' + cavity[i][j+1:]
                    
        return cavity