Question
Can you send me the original code which I can copy and paste please. thank you so much Merge Sort Distances In this assignment, you
Can you send me the original code which I can copy and paste please. thank you so much
Merge Sort Distances
In this assignment, you will use merge sort to order a collection of locations by their distance from a given start location. Feel free to reuse any code from class.
The Location object takes three arguments: a name, a latitude, and a longitude. It has one method, distance_to(), which takes another location as an argument and returns the distance to another location (in arbitrary units, disregarding the curvature of the globe, and ignoring wrapping around at the International Date Line).
Expected behavior:
home = Location('home', 37.78, -122.18) cities = [ Location('Seattle', 47.60, -122.33), Location('Boston', 42.36, -71.06), Location('San Francisco', 37.77, -122.41), Location('Baltimore', 39.29, -76.61), Location('London', 51.51, 0.13), Location('Miami', 25.76, -80.19), Location('Houston', 29.76, -95.37), Location('Denver', 39.74, -104.99), Location('Anchorage', 61.22, -149.90), Location('NYC', 40.71, -74.01), ] sort_by_distance(home, cities)
=> [San Francisco at 37.77, -122.41, Seattle at 47.60, -122.33,
Denver at 39.74, -104.99, Houston at 29.76, -95.37,
Anchorage at 61.22, -149.90, Miami at 25.76, -80.19,
Baltimore at 39.29, -76.61, NYC at 40.71, -74.01,
Boston at 42.36, -71.06, London at 51.51, 0.13]
calloway = Location('Calloway Hall', 39.35, -76.58)
sort_by_distance(calloway, cities)
=> [Baltimore at 39.29, -76.61, NYC at 40.71, -74.01,
Boston at 42.36, -71.06, Miami at 25.76, -80.19,
Houston at 29.76, -95.37, Denver at 39.74, -104.99,
San Francisco at 37.77, -122.41, Seattle at 47.60, -122.33,
Anchorage at 61.22, -149.90, London at 51.51, 0.13]
use this same code please
from math import sqrt
class Location: def __init__(self, name, latitude, longitude): self.name = name self.lat = latitude self.long = longitude def distance_to(self, loc): """Return the distance to the given location.""" return sqrt((self.lat - loc.lat)**2 + (self.long - loc.long)**2) def __eq__(self, other): """"Return True if latitudes and longitudes are equal""" return self.lat == other.lat and self.long == other.long def __repr__(self): return f'{self.name} at {self.lat:.2f}, {self.long:.2f}'
def sort_by_distance(start, locations): """Sort a list of locations by their distance to start.""" # Your code here
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