CE 392C - Transportation Network Analysis

Fall 2023

Homework 1

Due on Canvas at 11:59 PM, Friday, September 8. Submit two files: a PDF containing your solutions to the textbook problems, and a ZIP file containing your code for the Python problems. More details below on how to submit the Python code.

Textbook questions

Make sure you are using version 0.91 of the text.

Chapter 2: Exercises 11, 12, 13

Chapter 3: Exercises 1 and 2

Chapter 4: Exercise 4

Python questions

Note: In Python, spacing and indentation is important and code will not run correctly unless a consistent indentation style is used. I use three spaces to indent. Most text editors and IDEs can be set to use this as a default when you press the <TAB> key.

Question 1: Multiplication

Download and unzip these files to a convenient directory. You will be editing three files in this assignment: multiply.py, studentGrades.py, and utstudent.py. You are welcome to look at the other files too (in particular you should look at student.py for Question 3) but they should not be edited.

At a command prompt, type python grader.py to run the autograding script. (On some Linux installations, you may need to type python3 grader.py to ensure you are running version 3 of Python.) You should see the following output:

Running multiplication test: tests/q1/1-regular.test......not yet attempted
Running multiplication test: tests/q1/2-negative.test......not yet attempted
Running multiplication test: tests/q1/3-zero.test......not yet attempted
Running multiplication test: tests/q1/4-float.test......not yet attempted
Running data structures test: tests/q2/1-3students.test......not yet attempted
Running data structures test: tests/q2/2-6students.test......not yet attempted
Running classes test: tests/q3/1-3students.test......not yet attempted
Running classes test: tests/q3/2-6students.test......not yet attempted

SCORES: 
Q1: Multiplication  0/4
Q2: Data structures 0/6
Q3: Classes         0/2
----------
TOTAL:              0/12

This is an unofficial score, please submit code on Canvas for final scoring.

The score is reported as 0, since you have not yet attempted any of the questions. Open multiply.py and look at the function calculateProduct:

def calculateProduct(factor1, factor2):
#  *** YOUR CODE HERE ***
# Replace the following statement with your code for calculating and returning the product of factor1 and factor2
raise utils.NotYetAttemptedException      

Delete the line raise utils.NotYetAttemptedException and replace it with return factor1 * factor2. Run grader.py again, and you should see the following output:

Running multiplication test: tests/q1/1-regular.test......pass
Running multiplication test: tests/q1/2-negative.test......pass
Running multiplication test: tests/q1/3-zero.test......pass
Running multiplication test: tests/q1/4-float.test......pass
Running data structures test: tests/q2/1-3students.test......not yet attempted
Running data structures test: tests/q2/2-6students.test......not yet attempted
Running classes test: tests/q3/1-3students.test......not yet attempted
Running classes test: tests/q3/2-6students.test......not yet attempted

SCORES: 
Q1: Multiplication  4/4
Q2: Data structures 0/6
Q3: Classes         0/2
----------
TOTAL:              4/12

This is an unofficial score, please submit code on Canvas for final scoring.      

Your code passed all of the tests for the multiplication question, so you have a score of 4/4 for the first question. You can examine the test instances used in the tests/q1 folder. For more complicated questions you might find it useful to look at the test instances as a debugging aid.

Question 2: Built-in data structures

In the online Python tutorial, you should have learned about lists, tuples, and dictionaries. These are very useful built-in data structures in Python that we will be using throughout the course. Question 2 involves lists and tuples, and can be solved only using these. If you are not comfortable with dictionaries, you will find it instructive to solve the problem a second time, converting the tuples into a dictionary and operating with the grade records as a dictionary. If done correctly, both implementations will pass the autograding script with a perfect score.

Open studentGrades.py. For this question, you must implement the calculateAverage and findHighScore functions. Both of these functions are passed a list of tuples; each tuple contains a student's name and their grade. An example of such a tuple is ('Alice', 85.0). The entire list can be read as [('Alice', 85.0), ('Bob', 80.0), ('Carlos', 90.0)].

The calculateAverage function should return the average grade across all of the students, as a floating point number. The findHighScore function should identify the student with the highest score, and return a tuple containing both the student's name, and their score.

Question 3: Python classes

In the online Python tutorial, you should have learned the basics of how Python classes operate. The student.py file contains the Student class from this tutorial.

Open utstudent.py. This file defines the UTStudent class, which is derived from the Student class, but contains an extra field of information (the student's EID). As a derived class, UTStudent has all of the methods from the Student class. Any of these methods can be overridden by writing a new version in the UTStudent class definition (for instance, the constructor is different because an EID must be set).

At the bottom of this file is the findHighestEID function, which you must implement. The argument to this function is a list of UTStudent objects; your function must find the student with the highest average score (recall that the Student class contains two exam scores), and return their EID (and only their EID). Try to re-use available methods to the extent possible.

Submission instructions

Your answers to the textbook questions should be placed in a single PDF file and uploaded to Canvas. Please verify that your file uploaded correctly, that the pages are in the right order, etc.

For the Python questions, submit a single ZIP file and upload to Canvas. Please follow these instructions:

  1. Include a text file named README.txt which contains (at a minimum) the names of you and your partner (if any). Both partners do not need to submit this ZIP file. (But both partners should submit their PDF questions from the tetxbook separately.) You can add in additional comments or explanations about your implementation in this file, if necessary.
  2. Everything in the original ZIP file should be in the ZIP file you submit (do not just submit the files you changed.)
  3. Give your ZIP file the same name as the ZIP file you downloaded (in this case, hw1.zip).
  4. When I unzip your file, I should get a folder called hw1/ with all of your files in it (it should not expand in place.)
To grade your code, I will execute the following commands. (If you have a Linux environment, you can test these steps yourself. Failure to follow the instructions above will not usually result in a grade penalty, but it makes my life less convenient.)
unzip hw1.zip
diff -bur master/ hw1/ # master is the original version; this shows me your changes
cd hw1
cat README.txt
python3 grader.py
cd ..
rm -r hw1 hw1.zip

Return to CE 392C.