I'm able to reference the other variables like preReq and taken without issue, but when trying to access visited, I am told I'm referencing the variable before its assignment. Am I missing something with scope that the other variable assignments have that visited does not?
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
preReqs = {}
taken = [0] * numCourses
visited = []
for course in range(numCourses):
preReqs[course] = []
for prereq in prerequisites:
preReqs[prereq[0]].append(prereq[1])
def dfs(course):
print(visited[0])
if course in visited:
return False
else:
visited.append(course)
for preReq in preReqs[course]:
if taken[preReq] == 1:
preReqs[course].remove(preReq)
else:
dfs(preReq)
if preReqs[course] == []:
taken[course] = 1
visited = []
for each in range(numCourses):
dfs(each)
for each in taken:
if each == 0:
return False
return True