Question: Please help with HW in python # I need to access the counter variable to the function but I should not pass it because I
Please help with HW in python
# I need to access the counter variable to the function but I should not pass it because I am using recursion to solve it.
so the output should be at the end [1,1,1,1,1,1]
Previous output with passing in variable as a parameter: current code below is not passing the variable so output is error [1, 0, 0, 0, 0, 0] B [1, 1, 0, 0, 0, 0] D [1, 1, 1, 0, 0, 0] E [1, 1, 1, 0, 0, 0] F [1, 1, 1, 1, 0, 0] C [1, 1, 1, 1, 0, 0]
#code blow
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}
visited = set() # Set to keep track of visited nodes.
len_order=len(graph)
counter = 0 # Help here I need to pass this into the function but I am receiving this error
order=[0 * i for i in range(len_order) ] # LIST OF 0'S UNTIL THE LENGTH OF GRAPH WHOCH IS 6
def dfs(visited, graph, node, counter):
if node not in visited:
print (node)
visited.add(node)
order[counter]= 1
print(order)
counter = counter + 1
for neighbour in graph[node]:
dfs(visited, graph, neighbour)
# Driver Code
dfs(visited, graph, 'A')
thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
