In the below code I am getting NameError: name 'checkNeighbour' is not defined. I am not able to understand when to use self keyword so I wanted to know the following:
1) How to call one function inside another. Usually, in Java you would directly call the function, here also I tried calling it directly as checkNeighbour(image,sr,sc,newColor, visited) instead of self.checkNeighbour(image,sr,sc,newColor, visited) but it gave the same error.
2) When recursing, will I have to use self.checkNeighbour(image, x,y,newColor,visited) or checkNeighbour(image, x,y,newColor,visited)
3) How to use the global variable movement inside the checkNeighbour() function, i.e with or without self
class Solution:
movement = [0,-1,0,1,0]
def checkNeighbour(self,image : List[List[int]],sr:int,sc:int,newColor:int, visited:List[List[int]]) -> None:
previousColor = image[sr][sc]
image[sr][sc] = newColor
visited[sr][sc] = -1
for i in range(4):
x = sr + self.movement[i]
y = sc + self.movement[i+1]
if(x < 0 or y < 0 or x >= len(image) or y >= len(image[0])):
continue
if(image[x][y] == previousColor and visited[x][y] != -1):
checkNeighbour(image, x,y,newColor,visited) //recurse the function
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
visited = [[0 for i in range(len(image[0]))] for j in range(len(image))]
self.checkNeighbour(image,sr,sc,newColor, visited)
return(image)