Solution 1

``````class Distance(object):
METER_CONVERSIONS = {
"mm": 0.001,
"cm": 0.01,
"m": 1,
"km": 1000,
"in": 0.0254,
"ft": 0.3048,
"yd": 0.9144,
"mi": 1609.344
}

def __init__(self, value, unit="m"):
self.value = value
self.unit = unit

def convert_to_meters(self):
return self.value * self.METER_CONVERSIONS[self.unit]

def __str__(self):
return "{}{}".format(self.value, self.unit)

def __repr__(self):
return "Distance: {}{}".format(self.value, self.unit)

def __eq__(self, other):
if self.convert_to_meters() == other.convert_to_meters():
return True
return False

def __ne__(self, other):
if self.convert_to_meters() != other.convert_to_meters():
return True
return False

def __lt__(self, other):
if self.convert_to_meters() < other.convert_to_meters():
return True
return False

def __le__(self, other):
if self.convert_to_meters() <= other.convert_to_meters():
return True
return False

def __gt__(self, other):
if self.convert_to_meters() > other.convert_to_meters():
return True
return False

def __ge__(self, other):
if self.convert_to_meters() >= other.convert_to_meters():
return True
return False
``````

# Going the Distance

Complete the methods for the class `Distance` so that it has functionality to compare different distances.

For the `convert_to_meters` method, the formula is multiplying the object's value * it's unit conversion (in the conversion dictionary at the top).

Use https://rszalski.github.io/magicmethods/ for magic method guidance, and read the tests if you get stuck.

``````d1 = Distance(4, "ft")    # 4 feet = 1.21 meter
d2 = Distance(1)          # 1 meter (meter by default)
d3 = Distance(1000, "mm") # 1 meter

list_of_distances =[d1, d2]

print(d1) = "4ft"
print(list_of_distances) == '[Distance: 4ft, Distance: 1m]'

d1 == d3 # True
d1 != d2 # True
d1 > d2 # True
d2 < d1 # True
d1 <= d3 # True
d1 >= d3 # False
``````

### Test Cases

test str repr -

``````def test_str_repr():
d1 = Distance(4, "ft")
d2 = Distance(1)

list_of_distances =[d1, d2]

assert str(d1) == "4ft"
assert str(d2) == "1m"
assert str(list_of_distances) == '[Distance: 4ft, Distance: 1m]'``````

test compare between units -

``````def test_compare_between_units():
d1 = Distance(4, "ft")
d2 = Distance(1)
d3 = Distance(1000, "mm")

assert d2 == d3
assert d1 != d2
assert d1 != d3

assert d1 > d2
assert d2 < d1
assert d2 <= d3
assert d3 >= d2
``````

test init -

``````def test_init():
d1 = Distance(4, "ft")

assert d1.value == 4
assert d1.unit == "ft"``````

test convert to meters -

``````def test_convert_to_meters():
d1 = Distance(4, "ft")
d2 = Distance(1)
d3 = Distance(1, 'm')
d4 = Distance(1000, "mm")
d5 = Distance(1.5, "km")

assert d1.convert_to_meters() == 1.2192
assert d2.convert_to_meters() == 1
assert d3.convert_to_meters() == 1
assert d4.convert_to_meters() == 1
assert d5.convert_to_meters() == 1500
``````
# He's going for speed... class Distance(object): METER_CONVERSIONS = { "mm": 0.001, "cm": 0.01, "m": 1, "km": 1000, "in": 0.0254, "ft": 0.3048, "yd": 0.9144, "mi": 1609.344 } def __init__(self, value, unit="m"): pass def convert_to_meters(self): pass def __str__(self): pass def __repr__(self): pass def __eq__(self, other): pass def __ne__(self, other): pass def __lt__(self, other): pass def __le__(self, other): pass def __gt__(self, other): pass def __ge__(self, other): pass