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

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']

Write a function kclosest(place, k, locations) that returns a list of the k closest locations to a given place, ordered by increasing distance. If there are multiple locations at equal distance to the given place, you need to choose between them, return the ones that appear first in the list of the locations Assumptions . You can assume that k >0. You can assume that the locations contain at least kdistinct places (with no duplicate places). For example, [("place1", 4.1, 2), ("place2", 4.1, 2), ("place3", 2, 2)1 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. You should calculate the distance between an observation and the test point using the Euclidean distance between two points (x1,yi) and (22, y2):

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