Question: Python Write a function sort cars() that takes a list of Car objects and sorts the list of cars by their total repair costs, placing

Python

Write a function sort cars() that takes a list of Car objects and sorts the list of cars by their total repair costs, placing the cars into descending order by total repair cost. In other words, after sorting is done, the first car in the list will be the car with the greatest total repair cost, the second car will be the one with the second-greatest total repair cost, and so on, with the last car being the one with the least total repair cost. If a Car object has never been repaired, then treat its total repair cost as being 0. If two cars c1 and c2 have the same total repair costs, then their order in the list can be given as c1,c2 or c2,c1.

The lesser-known basic sorting algorithm called gnome sort will almost get the job done. The algorithms pseudocode, given below, is already pretty close to real Python code. However, this pseudocode will sort a list into ascending order, which is the opposite of what we want. First study the algorithm to make sure you understand the pseudocode, then alter it so that you can use it to sort the the list of cars by total repair cost.

Note that this pseudocode contains no return statement, so your Python implementation shouldnt either.

procedure gnome_sort(a[]): pos = 0 
 while pos = a[pos-1]) then 
 increment pos by 1 otherwise 
 swap a[pos] and a[pos-1] decrement pos by 1 

Examples:

The output generated by the sample test cases in the provided lab8.py will generate more output than is indi- cated in table below. Only the VINs are given in the table in the interest of saving space.

Hint: you may wish to augment the __repr() __ function in the Car class to include the total repair costs for a car.

 Python Write a function sort cars() that takes a list of

class Car: def __init__(self, vin, brand, model, year): self._vin = vin self._brand = brand self._model = model self._year = year self._repairs = [] def add_repair(self, repair_desc, repair_cost): self._repairs += [Repair(repair_desc, repair_cost)] def __repr__(self): return 'VIN: ' + self._vin + ' ' + \ ' Brand: ' + self._brand + ' ' + \ ' Model: ' + self._model + ' ' + \ ' Year: ' + str(self._year) + ' '  class Repair: def __init__(self, desc, cost): self._desc = desc self._cost = cost def __repr__(self): return self._desc + ': $' + str(self._cost)  def record_repair(cars, vin, repair_desc, repair_cost): vin_values = [] for i in range(len(cars)): vin_values.append(cars[i]._vin) if vin in vin_values: repairs = cars[vin_values.index(vin)]._repairs if len(repairs) != 0: temp_cost = 0 for i in range(len(repairs)): temp_cost += repairs[i]._cost return temp_cost + repair_cost else: return repair_cost else: return 0  def sort_cars(cars): pass # delete this line and then start coding  

if __name__ == '__main__': 
 def reset_car_database(): c01 = Car('XYZ123X', 'Toyota', 'Camry', 2012) c02 = Car('HSY113Y', 'Honda', 'Civic', 2016) c03 = Car('MZJ291E', 'Ford', 'Escape', 2009) c04 = Car('KJD922P', 'Jeep', 'Wrangler', 2011) c05 = Car('TRQ235K', 'Hyundai', 'Sonata', 2017) c06 = Car('JNH47GB', 'Toyota', 'Camry', 2011) c07 = Car('K83JDE3', 'Honda', 'Pilot', 2009) c08 = Car('MCJD83J', 'Hyundai', 'Elantra', 2013) c09 = Car('9EM2JSK', 'Toyota', 'Camry', 2002) c10 = Car('JF83JKS', 'Honda', 'Civic', 2012) c01.add_repair('Broken axle', 2900) c01.add_repair('Punctured tire', 40) c02.add_repair('Cracked windshield', 1000) c04.add_repair('Oil change', 45) c04.add_repair('New clearcoat', 550) c05.add_repair('Punctured tire', 30) c05.add_repair('Cracked windshield', 1000) c06.add_repair('Popped dents', 75) c06.add_repair('Broken headlight', 80) c06.add_repair('Broken taillight', 95) c07.add_repair('Rebuilt engine', 4880) c09.add_repair('Broken headlight', 80) c09.add_repair('Punctured tire', 80) c10.add_repair('Replaced windshield wipers', 125) car_list1 = [c02, c03, c05, c06, c07, c08, c09] car_list2 = [c01, c03, c04, c05, c06, c08, c09, c10] car_list3 = [c03, c04, c08] return car_list1, car_list2, car_list3 
 cars1, cars2, cars3 = reset_car_database() print('Testing sort_cars() with cars1. cars1 after sorting:') sort_cars(cars1) print(cars1) print('Testing sort_cars() with cars2. cars2 after sorting:') sort_cars(cars2) print(cars2) print('Testing sort_cars() with cars3. cars3 after sorting:') sort_cars(cars3) print(cars3) 

Function Call Sorted List Would Look Like This When Printed sort-cars (cars1) I"K83JDE3", "TRQ235K", "HSY 113Y" "JNH47GB", 9EM2JSK "MZJ2 91E", "MCJD83J" sort-cars (cars2) "XYZ123X TR0235 KJD 922P JNH 47GB 9EM2 JSK "JF 83JKS MZJ2 91E MCJD 83 J sort cars (cars3) "KJD922P", "MZJ291E", "MCJD83J

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!