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
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:
from reference import kclosest
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)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started