Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE CODE IN PYTHON Write a function find worst brand() that searches a list o

ID: 3812657 • Letter: P

Question

PLEASE CODE IN PYTHON

Write a function find worst brand() that searches a list of cars and returns the brand of car that has the worst repair record in terms of total repair costs. Since there might be several cars of the same brand, it will be necessary to sum the repair costs for all cars of each brand, determine which brand had the greatest total repair cost over all cars in the list, and finally, return the name of that brand. If two (or more) brands have the same maximum total repair code, the function can return either brand.

Hint: consider using a dictionary, where the keys are brands and the values are the total costs for the brands.

Another hint: to retrieve a list of the keys in a dictionary, you can use this syntax:

my_dict = { }

...code that stores key/value pairs in my_dict...

key_list = my_dict.keys()

The idea then is to iterate over the list of keys, find the maximum value, and return the key for that maximum value. In the context of the find worst brand() function, finding the key with the largest value is equivalent to finding the brand with the highest total cost.

PROVIDED BELOW ARE THE CLASSES THAT ARE BEING REFERENCED AS WELL AS THE LISTS OF CARS THAT WILL BE USED IN THE PROBLEM. PLEASE CODE ACCORDING TO THE PROVIDED DETAILS

EXAMPLES OF FINAL OUTPUT:

find worst brand(cars1) "Honda"

find worst brand(cars2) "Toyota"

find worst brand(cars3) "Jeep"

Explanation / Answer

# pastebin link for code: https://pastebin.com/yWi4QaAq

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 total_cost(car):
    repairs = car._repairs;
    total_cost = 0
    for repair in repairs:
        total_cost += repair._cost;
    return total_cost

def find_worst_brand(carList):
    brand_dict = {}
    for car in carList:
        brand_dict[car._brand] = 0
  
    for car in carList:
        repair_cost = total_cost(car)
        brand_dict[car._brand] += repair_cost
  
    max_cost = -1
    worst_brand = ""
    for brand in brand_dict:
        if brand_dict[brand] > max_cost:
            max_cost = brand_dict[brand]
            worst_brand = brand
    return worst_brand

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 find_worst_brand() with cars1: ' + str(find_worst_brand(cars1)))
    print('Testing find_worst_brand() with cars2: ' + str(find_worst_brand(cars2)))
    print('Testing find_worst_brand() with cars3: ' + str(find_worst_brand(cars3)))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote