A python computing . the kclosest function is that: next construct a tour that c
ID: 3885724 • Letter: A
Question
A python computing .
the kclosest function is that:
next 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:
You need to write a function buildtour(start, locations)where startis a string corresponding to the name of a location in locations, locations defines before.
example like that:
>>> print(buildtour("myhotel", [("ngv", 4, 0), ("town hall", 4, 4),("myhotel", 2, 2), ("fed square", 4, 2)]))
["myhotel", "fed square", "ngv", "town hall"]
>>> print(buildtour("fed square", [("ngv", 4, 0), ("town hall", 4, 4), ("myhotel", 2, 2), ("fed square", 4, 2)]))
["fed square", "ngv", "myhotel", "town hall"]
>>> print(buildtour("fed square", [("myhotel", 2, 2), ("town hall", 4, 4), ("fed square", 4, 2), ("parliament", 8,5.5)]))
['fed square', 'myhotel', 'town hall', 'parliament']
>>> print(buildtour("parliament", [("myhotel", 2, 2), ("town hall", 4, 4), ("fed square", 4, 2), ("parliament", 8,5.5)]))
['parliament', 'town hall', 'fed square', 'myhotel']
Explanation / Answer
python function "kclosest()"code to perform the given operations :
#the function kclosest definition
def kclosest (place, k, locations ):
#import partial
from functools import partial
#internal function to compute the distance between the
#place and locations
def distance_squared(X,Y):
return (X[0] -Y[1]**2 + (X[1] -Y[2])**2
#save the place
place-co-ordinate = place
#assign the locations
location-list = locations
#declare a list to save the results
Y=[]
#the for loop to find the closest pairs
for v in range (0,k):
#find the closest in the list
X= min (location-list, key=partial(distance-squared,place-co-ordinate ))
#remove the closesgt from list
location-list.remove(X)
#save the closest
Y.append(X)
#extract the place names from the list
place-names = [item [0] for item in Y]
#return the place names
return place-names
sample test of the function :
>>>============================RESTART========================
>>>
>>> print (kclosest((4,1),1[("ngv",4,0),("town hall",4,4),("my hotel", 2,2),("parliament",8,5,5),("frd square",4,2)]))
['ngv']
>>> print (kclosest((4,1),2 [("ngv",4,0),("town hall",4,4),("my hotel", 2,2),("parliament",8,5,5),("frd square",4,2)]))
['ngv','fed square']
>>> print (kclosest((4,1),3 [("ngv",4,0),("town hall",4,4),("my hotel", 2,2),("parliament",8,5,5),("frd square",4,2)]))
['ngv','fed square','ngv']
>>> print (kclosest((4,1),4 [("ngv",4,0),("town hall",4,4),("my hotel", 2,2),("parliament",8,5,5),("frd square",4,2)]))
['myhotel','fed squre',;ngv','towel hall']
>>>
raw copyable code:
note: preserve the code identation
#the function kclosest definition
def kclosest(place,k,locations):
#import partial
from functools import partial
#intential function to compute the distance between the
#place and loacations
def distance -squared(X<Y):
return (X[0]-Y[1]**2 + 9X[1]-Y[2]**2
#save the place
place_co_ordinate = place
#assign the loacations
location_list =locations
#declare a list to save the results
Y=[]
#the for loop to find the closest pairs
for v in range (0,k):
#find the closest int the list
X=min9'location_list,key=partial(distance_squared,place_co_ordinate))
#remove the closest from list
location_list.remove(X)
#save the closest
Y.append(X)
#extract the place names from the list
place_names =[item[0] for item in Y]
#print the place names
return place-names