Reputation: 41
I am trying to find the longest sequence of a character in a matrix. I am realively new to python, and I think the problem is that python's recursive methods are not the same as C/C++/Java etc. here is my code, now... Do you know another way to do this, if reccursion is not a thing in Python, or can you fix my code to work in pythonic recusion? ( the problem is that the lenght and the visited matrix is not updated during recursion ).
def get_current_score(self, i, j, char, viz, leng):
leng = 0
self.viz[i][j] = True
if (self.out_of_span(i - 1, j) == False and self.viz[i-1][j] == False and self.matrix[i - 1][j] == char):
self.viz[i - 1][j] = True
return leng + self.get_current_score(i - 1, j, char, self.viz, leng)
if (self.out_of_span(i - 1, j +1) == False and self.viz[i-1][j+1] == False and self.matrix[i - 1][j + 1] == char):
self.viz[i - 1][j + 1] = True
return leng + self.get_current_score(i - 1, j + 1, char, self.viz, leng)
if (self.out_of_span(i - 1, j - 1) == False and self.viz[i-1][j-1] == False and self.matrix[i - 1][j - 1] == char):
self.viz[i - 1][j - 1] = True
return leng + self.get_current_score(i - 1, j - 1, char, self.viz, leng)
if (self.out_of_span(i, j - 1) == False and self.viz[i][j-1] == False and self.matrix[i][j - 1] == char):
self.viz[i][j - 1] = True
return leng + self.get_current_score(i, j - 1, char, self.viz, leng)
if ( self.out_of_span(i, j + 1) == False and self.viz[i][j+1] == False and self.matrix[i][j + 1] == char):
self.viz[i][j + 1] = True
return leng + self.get_current_score(i, j + 1, char, self.viz, leng)
if ( self.out_of_span(i + 1, j) == False and self.viz[i+1][j] == False and self.matrix[i + 1][j] == char):
self.viz[i + 1][j] = True
return leng + self.get_current_score(i + 1, j, char, self.viz, leng)
if (self.out_of_span(i + 1, j - 1) == False and self.viz[i+1][j-1] == False and self.matrix[i + 1][j - 1] == char):
self.viz[i + 1][j - 1] = True
return leng + self.get_current_score(i + 1, j - 1, char, self.viz, leng)
if (self.out_of_span(i + 1, j + 1) == False and self.viz[i+1][j+1] == False and self.matrix[i + 1][j + 1] == char):
self.viz[i + 1][j + 1] = True
return leng + self.get_current_score(i + 1, j + 1, char, self.viz, leng)
return 1 + leng
def add(self, index):
[...]
# scor
print('\n --------------\n')
for i in range(self.maxh, self.nr):
for j in range(self.span[0], self.span[1]+1):
if(self.player1 == False and self.matrix[i][j] == 'X'):
self.score1 = max(self.score1, self.get_current_score(i, j, 'X', self.viz, self.score1))
self.viz[i][j] = True
#self.score1 -= 1
else:
if(self.player1 == True and self.matrix[i][j] == 'O'):
self.score2 = max(self.score2, self.get_current_score(i, j, 'O', self.viz, self.score1))
self.viz[i][j] = True
self.reset_viz()
self.print_matrix()
Upvotes: 2
Views: 70
Reputation: 1281
i think this will do
def get_current_score(self, i, j, char, leng):
# leng = 0 <- don't
self.viz[i][j] = True # you set it twice, let's keep that one
def test_cell(a, b): # keep it DRY
return self.out_of_span(a, b) == False \
and self.viz[a][b] == False \
and self.matrix[a][b] == char
cells = [(i - 1, j), (i - 1, j + 1), (i - 1, j - 1), (i, j - 1),
(i, j + 1), (i + 1, j), (i + 1, j - 1), (i + 1, j + 1)]
for a, b in cells:
if test_cell(a, b):
# you don't need to set that cell to true since that's the
# first thing you do in the function
# self.viz[a][b] = True
return leng + self.get_current_score(a, b, char, leng)
return 1 + leng
def add(self, index):
[...]
# scor
print('\n --------------\n')
for i in range(self.maxh, self.nr):
for j in range(self.span[0], self.span[1]+1):
if(self.player1 == False and self.matrix[i][j] == 'X'):
# no need to send self.viz here. same for score2
# if you need to set self.score1 to 0 do it here. not in the recursion
self.score1 = max(self.score1, self.get_current_score(i, j, 'X', self.score1))
self.viz[i][j] = True
#self.score1 -= 1
else:
if(self.player1 == True and self.matrix[i][j] == 'O'):
self.score2 = max(self.score2, self.get_current_score(i, j, 'O', self.score1))
self.viz[i][j] = True
self.reset_viz()
self.print_matrix()
Upvotes: 1