An electric-car app knows the positions of charging points as whole-number vectors (usually [x, y] on a street grid, sometimes with a third number for the floor of a car park). For a car at car it shows the nearest charger. When several chargers are equally near, picking one silently would depend on the order of the list, so the app shows all of them and lets the driver choose.
Write nearest_chargers(car, chargers, measure) that returns the sorted list of indices of all chargers at the smallest distance from car. measure is either "straight" (Euclidean distance) or "streets" (Manhattan distance: the sum of the absolute differences). If there are no chargers, return [].
Examples
Input: car = [3, 2], chargers = [[1, 1], [6, 3], [5, 3], [2, 4]], measure = "straight"
Output: [0, 2, 3]
Explanation: chargers 0, 2 and 3 are all at distance sqrt(5) = 2.236; charger 1 is at sqrt(10).
Input: car = [3, 2], chargers = [[1, 1], [6, 3], [5, 3], [2, 4]], measure = "streets"
Output: [0, 2, 3]
Explanation: the street distances are 3, 4, 3 and 3.
Input: car = [0, 0], chargers = [[2, 2], [3, 0], [0, -3]], measure = "streets"
Output: [1, 2]
Explanation: the street distances are 4, 3 and 3. In a straight line [2, 2] (about 2.83)
is the only nearest one.
Constraints
0 <= len(chargers) <= 10**4; all vectors have the same length, between 1 and 5- coordinates are whole numbers between
-10**5and10**5 measureis"straight"or"streets"
Goals
- Find the smallest distance from a query to the rows of a dataset in one pass
- Return every row that ties for the smallest distance instead of an arbitrary one
- Switch between two distances without changing the rest of the code