SD 212 Spring 2023 / Homeworks


This is the archived website of SD 212 from the Spring 2023 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

hw30: Programming with type hints

  • Due before the beginning of class on Friday, April 21

References

This homework is based on our current unit on typing.

Download and setup

You will need to download two new packages in order to test your code for this HW.

First, make sure you are using the right version of python. Run these commands in a terminal:

mamba activate sd212
python3 --version

You should see a version number that starts with 3.10 or possibly 3.11. If it says something earlier than 3.10, you will need to reinstall your sd212 environment. Don’t worry - it will just take a few minutes. Run the commands listed here if your Python is older than 3.10

Now, once your python is version 3.10 or later, you can install the two new packages we need:

mamba activate sd212
mamba install mypy pandas-stubs

That’s it! You can run mypy --version on the command line to make sure it worked.

The data

This assignment uses some cars data that you can download here: cars.csv

(The original data source that I used is from the folks at SAS.)

Your task

Write a Python program autos.py that has three things:

  • A class Car that represents a single car. It should contain (at least) the following functions:

    • total_weight(self, count): Return the total weight of count many of that car.

    • trip_cost(self, distance, gas_price): Calculate the total cost to buy the car at the MSRP price and drive it the given distance according to the city miles per gallon (MPG), based on the given price in gas per gallon.

  • A function read_cars() that returns a dictionary mapping car model names (in lowercase) to a corresponding instance of your Car class. The data should always come from a file called cars.csv as specified above.

  • Testing code at the end that is exactly this (just copy/paste it in):

    if __name__ == '__main__':
        # YOU MUST NOT CHANGE ANYTHING HERE!
        # Get this to work by writing the Car class and the read_cars() function
        cars = read_cars()
        model = input('Car model: ').lower()
        count = int(input('How many would you like? '))
        dist = float(input('How far will you drive in a year? '))
        mycar: Car = cars[model]
        print('total weight', mycar.total_weight(count))
        print('yearly cost', round(mycar.trip_cost(dist, 3.43), 2))

Crucially, your program must pass “strict” type checking using mypy. This auto-testing will not be run on the submit system automatically, so you have to check it yourself! In particular, you will need to add type annotations for every class variable, instance variable, function argument, and function return type.

You can run mypy to test your code like this:

roche@ubuntu$ mypy autos.py
Success: no issues found in 1 source file

Here are some sample tests runs:

roche@ubuntu$ python3 autos.py
Car model: viper
How many would you like? 10
How far will you drive in a year? 5000
total weight 34100.0
yearly cost 83224.17
roche@ubuntu$ python3 autos.py
Car model: Ion1
How many would you like? 1
How far will you drive in a year? 12000
total weight 2692.0
yearly cost 12578.08

Submit command

To submit files for this homework, run one of these commands:

submit -c=sd212 -p=hw30 autos.py
club -csd212 -phw30 autos.py