Run update21, if you haven't already, to create
the cs21/labs/08 directory. Then cd into this directory
to create your program for this lab in the file fuelEconomy.py.
Introduction
The Environmental Protection Agency (EPA) gathers data about the
gas usage of all vehicles sold in the US. We will use searching and
sorting to explore this data allowing us to answer questions such as:
- What are all of the cars made by a particular company?
- What are all of the cars of certain type made by any company?
- Which cars are the most and the least fuel efficient?
- Which cars have the lowest and highest annual fuel costs?
The file in /usr/local/doc/FEguide.txt contains the 2015
EPA data on over one thousand different vehicles. Here are some
sample lines from this file:
General Motors,Chevrolet,K1500 TAHOE 4WD,18,2900,Standard SUV 4WD
General Motors,Chevrolet,TRAVERSE AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,19,2750,Standard SUV 4WD
Chrysler Group LLC,Dodge,Durango AWD,16,3400,Standard SUV 4WD
Ford Motor Company,Ford,Expedition 4WD,17,3100,Standard SUV 4WD
Ford Motor Company,Ford,EXPLORER AWD,19,2750,Standard SUV 4WD
Each line contains data on one particular vehicle including:
- Manufacturer (we won't be using this field in our program)
- Company
- Model
- Combined (city and highway) Miles Per Gallon
- Estimated annual fuel cost
- Type of vehicle
Your program will read in this data and allow the user to explore it
by searching and sorting on different features. You will implement a
basic menu to direct the user through various options. Here is a
sample of how the program should operate.
Requirements
Make sure your program meets the following specifications.
- Read in the data for all of the cars as a list of lists that
looks like this:
[[car1Data], [car2Data], [car3Data], ...]
- You should store the data in the correct format.
The company name, model, and type of vehicle should be strings, while
the miles per gallon and annual fuel cost should be integers. Here's
an example of one of the above inner lists for a particular car:
['Fiat', 'Alfa Romeo', '4C', 28, 2050, 'Two Seaters']
Remember that you can use split(",") to split up a string
into its components that are separated by commas.
- Present the user with a menu of options as shown in the sample
run. Be sure to force the user to provide valid input.
- When you ask for input from the user, such as a company name or
a type of car, allow them to enter the information in upper or
lower case. The case should be ignored when searching.
- When you search, you should consider it to be a match if the
given string is found anywhere in the item. For example "volks" is in
"Volkswagen".
- You should use linear search to handle all search
requests. You will need to modify it so that it will work with a list
of lists. It should take the list of car data, an index to indicate
which feature of the car data you want to search, and a value to
search for. You should return a list of all cars that match the
requested value.
- If a search fails, in other words no matches are found, your
program should report this, and return back to the main menu.
- You should use either bubble sort or insertion
sort to handle all sort requests because they are fast at sorting
nearly sorted lists. You will need to modify the sort so that it will
work with a lists of lists. It should take the list of car data and
an index to indicate which feature of the car data you want to sort
on.
- Note that when you do a sort on MPGs that the worst cars
will be at the front of the list and the best cars will be at the back
of the list. You should just report the top 20 for the best and
bottom 20 for the worst.
- In contrast, when you do a sort on fuel cost, the worst cars
will be at the end of the list (because they have high values) and
best cars will be at the front of the list (because they have low
values). Again just report 20 for each case.
- For every search and sort request, you should show the results
and also report the number of cars in the result as well as their
average MPGs.
Tips
- You should continue to practice top-down design. By approaching the
problem in an organized way you will save time in the long run.
- Start by creating main().
- Make stubs for all of the functions.
- Implement one function at a time.
- This is really important: Test each function thoroughly!
- The first field (the Manufacturer) is not going to be used by your
program. Therefore, you can store and ignore it, or simply don't
store it in your list of car data.
- Remember that you can slice from the beginning or the end of a
list. Doing ls[:20] gives you the first 20 elements of a
list. Similarly, doing ls[-20:] gives you the last 20 elements of
a list.
- Doing ls.reverse() will put the list in reverse order.
It doesn't return anything, instead it permanently alters the list's
order.
- Because some of the data in the EPA file is very long, it is
helpful to use slicing to make the output of the data more readable.
For example, you can show only the first 10 characters of the the
company name rather than the entire name.
Submit
Remember to run handin21 to turn in your completed code.