So, I'm working on programming problem website where you submit answers to programming problems using a class called solution. You submit your code with a specially named method that the website uses to see if you are able to answer the question.
I submit the following code to the website to solve a problem that involves counting the number of ways to traverse a matrix that includes a number of obstacles:
from functools import cache
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
start = (0,0)
return self.uniquePathsHelper(obstacleGrid,start)
@cache
def uniquePathsHelper(self, matrix, pt):
neighbors = self.get_neighbors(matrix,pt)
if not neighbors:
return 0
elif (len(matrix) - 1, len(matrix[0]) - 1) in neighbors:
return 1
else:
total = 0
for next_pt in neighbors:
total += self.uniquePathsHelper(matrix,next_pt)
return total
def get_neighbors(self,matrix,pt):
neighbors = []
shifts = [(0,1), (1,0)]
max_col = len(matrix[0]) - 1
max_row = len(matrix) - 1
for row_shift, col_shift in shifts:
row, col = row_shift + pt[0], col_shift + pt[1]
if row > max_row or row < 0:
pass
elif col > max_col or col < 0:
pass
elif matrix[row][col] == 1:
pass
else:
neighbors.append((row, col))
return neighbors
I get a very strange error though:
TypeError: unhashable type: 'list'
return self.uniquePathsHelper(obstacleGrid,start)
Line 8 in uniquePathsWithObstacles (Solution.py)
ret = Solution().uniquePathsWithObstacles(param_1)
Line 89 in _driver (Solution.py)
_driver()
Line 100 in <module> (Solution.py)
I understand what an unhashable type error is but I can't see where I am attempting to hash a list in uniquePathsWithObstacles. Even stranger, the program never seems to get to uniquePathsHelper, which is the only function that does anything.
I'm wondering if there is some issue with Python's object system and what is permissible on a method.
Does anyone know what might be wrong here?