Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Preliminaries For this lab you will be working with the following classes: class Car: To complete this lab you may write any helper methods (inside

Preliminaries For this lab you will be working with the following classes: class Car:

image text in transcribed

To complete this lab you may write any helper methods (inside Car or Repair) and any helper functions you like. Just make sure to include them inside your lab8.py file

Part I: Recording a Repair (3 points) Write a function record repair() that records a repair for a car and returns the total cost of all repairs for that car (including the new repair). The function takes four arguments, in this order: cars: a list of Car objects vin: the VIN for the car that we want to record a repair for repair desc: a description of the repair repair cost: the cost of the repair (an integer) The function must search the list of Car objects for the car with the given vin, call the add repair method of the Car class with the given repair desc and repair cost to record a repair for the car, and then return the new total repair costs for the car. If no Car object is found in the list cars with the given vin, the function should simply return 0.

Examples: While looking at the examples in this document, you should have open then provided lab8.py file so that you can see exactly what are the contents of cars1, cars2 and cars3.

image text in transcribed

Part II: Sorting the Cars (3 points) 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 indicated 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.

image text in transcribed

Part III: Which Brand Has the Worst Repair Record? (3 points) 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.

Examples:

image text in transcribed

class Car: def init (sel 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 'Mn' Brand self. brand An' Model self model An +N Year str (self year) Vn' Total Repairs str (self. total repairs n' class Repair def init (self, desc, cost) self desc desc self cost cost. def repr (self) return self. desc S' str (self cost)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Systems Analysis And Synthesis Bridging Computer Science And Information Technology

Authors: Barry Dwyer

1st Edition

0128054492, 9780128054499

More Books

Students also viewed these Databases questions

Question

How do Excel Pivot Tables handle data from non OLAP databases?

Answered: 1 week ago