Repairing Python recursion causes wrong results -


i thought had no problems code, when started changing values, ended recursion problem. thought repaired it, when looked @ results, wrong. results when keep recursion though.

i used while loop try , fix recursion problem, instead of calling spread method recursively, return value pass propagate method, , return false if wouldn't pass value. so, long method keeps returning values, should rerun spread method result of previous run.

this code works until breaks recursion limit:

    def spread(self, position):         direction in self._directions:             (x, y) = self.changeposition(position, direction)             if self.caninfectmatrix[x][y] , not self.contactmatrix[x][y]:                 self.contactmatrix[x][y] = true                 self.spread([x,y]) #                 return [x,y] #             return false      def propagate(self):         # initialize caninfectmatrix , contactmatrix         self.contactmatrix = [[false row in range(self.cardinalwidth)] col in range(self.cardinalwidth)]         self.caninfectmatrix = [[false row in range(self.cardinalwidth)] col in range(self.cardinalwidth)]         col in range(self.cardinalwidth):             row in range(self.cardinalwidth):                 self.caninfectmatrix[row][col] = self.getsinfected(self._matrix[col][row])         # spread infection.         x in range(self.cardinalwidth):             y in range(self.cardinalwidth):                 if self._matrix[x][y] == "infected":                     self.spread([x,y]) #                     position = [x,y] #                     while position: #                         position = self.spread(position) 

the following code doesn't work, no errors:

    def spread(self, position):         direction in self._directions:             (x, y) = self.changeposition(position, direction)             if self.caninfectmatrix[x][y] , not self.contactmatrix[x][y]:                 self.contactmatrix[x][y] = true #                self.spread([x,y])                  return [x,y]              return false      def propagate(self):         # initialize caninfectmatrix , contactmatrix         self.contactmatrix = [[false row in range(self.cardinalwidth)] col in range(self.cardinalwidth)]         self.caninfectmatrix = [[false row in range(self.cardinalwidth)] col in range(self.cardinalwidth)]         col in range(self.cardinalwidth):             row in range(self.cardinalwidth):                 self.caninfectmatrix[row][col] = self.getsinfected(self._matrix[col][row])         # spread infection.         x in range(self.cardinalwidth):             y in range(self.cardinalwidth):                 if self._matrix[x][y] == "infected": #                    self.spread([x,y])                      position = [x,y]                      while position:                          position = self.spread(position) 

notice change in comments @ bottom of each method

as far can tell, both of these should accomplish same exact thing, don't. 1 works great until recursion limit error. other doesn't work @ all, no recursion error.

why these return different values?

in second version using return statement in for loop. such return interrupts loop, of course, never resumed.

what want instead have call spread() return list of points, possibly empty. in caller append these new answers list of to-be-processed points. caller work repeatedly popping item off list, calling spread(), , appending new points got list --- , repeating until list empty.


Comments