Question: Can you edit my code to show no errors. The code and error output will be listed below. DO NOT COPY AND PASTE ANOTHER ANSWER

Can you edit my code to show no errors. The code and error output will be listed below.

DO NOT COPY AND PASTE ANOTHER ANSWER

Question for code 1

filter candidates algorithm

Create a list called filtered. Iterate through the list of candidates. For each candidate, use the colours and posisitions auxiliary functions to compare it to the given guess. If the number of correct colours and right positions for the candidate match the given col and pos values, add the candidate to the filtered list. Sort the filtered list in ascending order. Return the filtered list.

Q1. Complete the Python code to implement your filter candidates algorithm. Run the test code to test your implementation.

CODE 1

%run -i m269_util

def filter_candidates(guess: tuple, col: int, pos: int, candidates: list) -> list:

filtered = [] for candidate in candidates: if colours(guess, candidate) == col and positions(guess, candidate) == pos: filtered.append(candidate) return sorted(filtered)

# Test code filter_tests = [ # case, guess, col, pos, candidates, filtered space ('random guess', ('R','B','O','G'), 3, 2, candidates, [('B', 'V', 'O', 'G'), ('B', 'Y', 'O', 'G'), ('G', 'B', 'O', 'V'), ('G', 'B', 'O', 'Y'), ('O', 'B', 'V', 'G'), ('O', 'B', 'Y', 'G'), ('R', 'B', 'G', 'V'), ('R', 'B', 'G', 'Y'), ('R', 'B', 'V', 'O'), ('R', 'B', 'Y', 'O'), ('R', 'G', 'O', 'V'), ('R', 'G', 'O', 'Y'), ('R', 'O', 'V', 'G'), ('R', 'O', 'Y', 'G'), ('R', 'V', 'B', 'G'), ('R', 'V', 'O', 'B'), ('R', 'Y', 'B', 'G'), ('R', 'Y', 'O', 'B'), ('V', 'B', 'O', 'R'), ('V', 'B', 'R', 'G'), ('V', 'R', 'O', 'G'), ('Y', 'B', 'O', 'R'), ('Y', 'B', 'R', 'G'), ('Y', 'R', 'O', 'G')]), ('close guess',('O','B','Y','R'), 3, 3, candidates, [('G', 'B', 'Y', 'R'), ('O', 'B', 'G', 'R'), ('O', 'B', 'V', 'R'), ('O', 'B', 'Y', 'G'), ('O', 'B', 'Y', 'V'), ('O', 'G', 'Y', 'R'), ('O', 'V', 'Y', 'R'), ('V', 'B', 'Y', 'R')]), ('correct guess',('O','B','Y','G'), 4, 4, candidates, [('O', 'B', 'Y', 'G')] ) ]

test(filter_candidates, filter_tests)

ERROR FOR CODE 1

--------------------------------------------------------------------------- NameError Traceback (most recent call last) ~/m269_util.py in  13 filter_tests = [ 14 # case, guess, col, pos, candidates, filtered space ---> 15 ('random guess', ('R','B','O','G'), 3, 2, candidates, 16 [('B', 'V', 'O', 'G'), ('B', 'Y', 'O', 'G'), ('G', 'B', 'O', 'V'), ('G', 'B', 'O', 'Y'), ('O', 'B', 'V', 'G'), ('O', 'B', 'Y', 'G'), ('R', 'B', 'G', 'V'), ('R', 'B', 'G', 'Y'), ('R', 'B', 'V', 'O'), ('R', 'B', 'Y', 'O'), ('R', 'G', 'O', 'V'), ('R', 'G', 'O', 'Y'), ('R', 'O', 'V', 'G'), ('R', 'O', 'Y', 'G'), ('R', 'V', 'B', 'G'), ('R', 'V', 'O', 'B'), ('R', 'Y', 'B', 'G'), ('R', 'Y', 'O', 'B'), ('V', 'B', 'O', 'R'), ('V', 'B', 'R', 'G'), ('V', 'R', 'O', 'G'), ('Y', 'B', 'O', 'R'), ('Y', 'B', 'R', 'G'), ('Y', 'R', 'O', 'G')]), 17 ('close guess',('O','B','Y','R'), 3, 3, candidates, NameError: name 'candidates' is not defined

Question for code 2

Use Dijsktra's algorithm to compute a shortest path from despatch to each zone. Draw the resulting tree.

The contents of Q4_warehouse.py are reproduced below for your reference.

warehouse = WeightedDiGraph() warehouse.add_node('despatch') warehouse.add_node('A') warehouse.add_node('D') warehouse.add_node('P') warehouse.add_node('S') warehouse.add_node('U') warehouse.add_node('Y') warehouse.add_node('Z')

warehouse.add_edge('despatch','U',30) warehouse.add_edge('U','despatch',30) warehouse.add_edge('despatch','A',10) warehouse.add_edge('A','despatch',10) warehouse.add_edge('despatch','D',5) warehouse.add_edge('D','despatch',5) warehouse.add_edge('despatch','Z',16) warehouse.add_edge('Z','despatch',16) warehouse.add_edge('A','U',15) warehouse.add_edge('U','A',15) warehouse.add_edge('A','P',10) warehouse.add_edge('P','A',10) warehouse.add_edge('P','S',12) warehouse.add_edge('S','P',12) warehouse.add_edge('D','Z',8) warehouse.add_edge('Z','D',8) warehouse.add_edge('P','D',15) warehouse.add_edge('D','P',15) warehouse.add_edge('P','Y',8) warehouse.add_edge('Y','P',8) warehouse.add_edge('Y','S',12) warehouse.add_edge('S','Y',12) warehouse.add_edge('Y','Z',15) warehouse.add_edge('Z','Y',15)

warehouse.draw() Don't worry about the layout of your tree.

CODE 2

import networkx as nx import matplotlib.pyplot as plt from Q4_warehouse import warehouse

# Compute shortest path from 'despatch' to each zone using Dijkstra's algorithm shortest_paths = nx.shortest_path(warehouse, source='despatch', weight='weight')

# Print shortest paths for zone, path in shortest_paths.items(): print(f'Shortest path from despatch to {zone}: {path}')

# Draw resulting tree pos = nx.spring_layout(warehouse) nx.draw(warehouse, pos, with_labels=True) nx.draw_networkx_edge_labels(warehouse, pos, edge_labels=nx.get_edge_attributes(warehouse, 'weight')) plt.show()

ERROR FOR CODE 2

--------------------------------------------------------------------------- NameError Traceback (most recent call last) ~/m269_util.py in  3 import networkx as nx 4 import matplotlib.pyplot as plt ----> 5 from Q4_warehouse import warehouse 6 7 # Compute shortest path from 'despatch' to each zone using Dijkstra's algorithm ~/Q4_warehouse.py in  ----> 1 warehouse = WeightedDiGraph() 2 warehouse.add_node('despatch') 3 warehouse.add_node('A') 4 warehouse.add_node('D') 5 warehouse.add_node('P') NameError: name 'WeightedDiGraph' is not defined

QUESTION FOR CODE 3

Implement this shortest path between nodes algorithm as a function in Python. Include a docstring. We have provided the reverse_in_place(). Make sure that your function inputs are in the order warehouse, start, end.

Test your implementation by running the test code given below. If any tests fail, then debug your implementation and run the test code again. You can repeat this iterative process as many times as you want. If there are any tests that fail and you are unable to identify the problem, you may add explanatory comments below the test code.

CODE 3

%run -i m269_util # The reverse_in_place code appears in Exercise 4.7.7 answer def reverse_in_place(values: list) -> None: """Reverse the order of the values.

Postconditions: post-values = [pre-values[len(pre-values) - 1], ..., pre-values[1], pre-values[0]] """ left_index = 0 right_index = len(values) - 1 while left_index < right_index: left_item = values[left_index] values[left_index] = values[right_index] values[right_index] = left_item left_index = left_index + 1 right_index = right_index - 1

# Write your function code here

def shortest_path(warehouse: dict, start: str, end: str) -> list: """ Return a list of nodes representing the shortest path from the start node to the end node. The path is computed using Dijkstra's algorithm on the warehouse graph.

Preconditions: - start and end are valid node names in the warehouse dictionary - warehouse is a dictionary representing a graph as described in section 4.6.2 - the weights of the edges are non-negative

Postconditions: - return a list of nodes representing the shortest path from the start node to the end node, including the start and end nodes """ unvisited_nodes = set(warehouse.keys()) shortest_distance = {node: float('inf') for node in unvisited_nodes} shortest_distance[start] = 0 previous_node = {node: None for node in unvisited_nodes}

while unvisited_nodes: current_node = min(unvisited_nodes, key=shortest_distance.get) if shortest_distance[current_node] == float('inf'): break

unvisited_nodes.remove(current_node)

for neighbour, distance in warehouse[current_node].items(): alternative_route = shortest_distance[current_node] + distance if alternative_route < shortest_distance[neighbour]: shortest_distance[neighbour] = alternative_route previous_node[neighbour] = current_node

shortest_path = [end] while previous_node[end]: end = previous_node[end] shortest_path.append(end) reverse_in_place(shortest_path)

return shortest_path

# Test code shortest_path_tests = [ # case, warehouse, start, end, shortest path ('neighbouring zones', warehouse, 'S', 'Y', ['S','Y']), ('zone neighbouring despatch', warehouse, 'despatch', 'D', ['despatch','D']), ('zone neighbouring despatch shortest path not direct', warehouse, 'despatch','Z', ['despatch','D','Z']), ('distant zones', warehouse, 'U', 'D', ['U','A','despatch','D']), ('distant zones opposite direction', warehouse, 'D', 'U', ['D','despatch','A','U']), ('two different shortest paths, choice depends on dijkstra', warehouse, 'Z', 'P', ['Z','D','P']), ] test(shortest_path_between_nodes, shortest_path_tests) # Add any explanatory comments here

ERROR FOR CODE 3

--------------------------------------------------------------------------- NameError Traceback (most recent call last) ~/m269_util.py in  61 shortest_path_tests = [ 62 # case, warehouse, start, end, shortest path ---> 63 ('neighbouring zones', warehouse, 'S', 'Y', ['S','Y']), 64 ('zone neighbouring despatch', warehouse, 'despatch', 'D', ['despatch','D']), 65 ('zone neighbouring despatch shortest path not direct', warehouse, 'despatch','Z', ['despatch','D','Z']), NameError: name 'warehouse' is not defined

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!