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.

You replace the element with 'X' while iterating, and again whn you are checking the previous element with [j-1] , if it is 'X' it will check if your element is greater than 'X'., so you should check if your [j-1] or [i-1] is 'X' or not. Just add this to your if statement.

## Cavity Map

You are viewing a single comment's thread. Return to all comments →

n = int(input())

a = []

for i in range(n):

for i in range(1,n-1):

for i in range(n):

Can you tell me what the error is?

you can make your code more readable if you stack the if statement like this

I also fixed the error in the code. the for loop in your code had j+1 for both the first and second check.

grid[i][j]>=grid[i-1][j-1] && grid[i][j]>=grid[i-1][j] && grid[i][j]>=grid[i-1][j+1] && grid[i][j]>=grid[i][j-1] && grid[i][j]>=grid[i][j+1] && grid[i][j]>=grid[i+1][j-1] && grid[i][j]>=grid[i+1][j] && grid[i][j]>=grid[i+1][j+1]

how can you compare str values? You first have to convert in into string then you can compare the values

You replace the element with 'X' while iterating, and again whn you are checking the previous element with [j-1] , if it is 'X' it will check if your element is greater than 'X'., so you should check if your [j-1] or [i-1] is 'X' or not. Just add this to your if statement.

how does line 3 and 4 work in python??