Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A python computing about construct a tour that contains all the given locations,

ID: 3883713 • Letter: A

Question

A python computing about construct a tour that contains all the given locations, starting from a specific location.

We do this by adding the closest remaining location to the current end of the tour.

If there are multiple remaining locations that are equally close to the current end of the tour, pick the one that appears first in the given list of locations.

Assumptions:

You can assume that the start is in the list of locations.

You can assume that you can only visit each location once.

We have provided to you a reference implementation of the kclosest function that you can use here. You can use our reference implemenation of the function kclosest(place, k, locations) by adding this code at the beginning of your code:

the kclosest function is that:

You need to write a function buildtour(start, locations)where startis a string corresponding to the name of a location in locations, locations defines like that: locations contain at least kdistinct places (with no duplicate places). For example, [("place1",4.1, 2), ("place2", 4.1, 2), ("place3", 2, 2)] is a valid list of locations (where "place1" and "place2" happen to be at the same coordinates). In contrast, [("place1", 4.1,2), ("place1", 4.1, 2), ("place3", 2, 2)] is not a valid list of locations, as "place1" appears twice.

def kclosest(place, k, locations): trom functools import p def distance_squared(x, y): return x[0] - y[1])**2(x[1] - y[2])**2 place_co_ordinate place locationlist locations y=[] for v in range (0,k): x= min(location-list, key-partial (distance-squared, place-co-ordinate)) location_list. remove(x) y. append(x) place-names [item[0] for item in y] = return place_names

Explanation / Answer

import sys

locations = [("place1",4.1, 2), ("place2", 4.1, 2), ("place3", 2, 2)]
path = []

# return the index of value to be removed.
def indexRemove(place,locations):
for i in locations:
if i[0] == place:
return i

else:
print("This is not a valid place")
sys.exit(1)

# From the above example.
# To remove start from the location at the begining.
start = "place1" # say
index = indexRemove(start,locations)
del locations[index]
path.append(start)

while len(location) > 0:
place = path[-1] # for the last location to find closest path from.
k = len(locations)
nextPlace = kclosest(place,k,locations)
index = indexRemove(nextPlace,locations)
del locations[index]
path.append(nextPlace)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote