You can download the assignment here.

The goal of this assignment is to incentivize learning to write reasonably good python/numpy code. This is so that:

  • You don’t learn it on your own and discover some super useful function at the end of the semester
  • If you’re doing something like calculating eigenvectors wrong, you find out in a low-stakes way
  • You get credit for spending time doing this

If you need an introduction to Python or numpy, you can check out this tutorial.

This assignment is due on Friday, January 24, 11:59pm.

Description

Each assignment requires you to fill in the blank in a function (in tests.py and warmup.py) and return the value described in the comment for the function.

When you open one of these two files, you will see starter code that looks like this:

def sample1(xs):
    """
    Inputs:
    - xs: A list of values

    Returns:
    The first entry of the list
    """
    return None

You should fill in the implementation of the function, like this:

def sample1(xs):
    """
    Inputs:
    - xs: A list of values

    Returns:
    The first entry of the list
    """
    return xs[0]

You can test your implementation by running the test script:

python run.py --test w1          # Check warmup problem w1 from warmups.py
python run.py --allwarmups       # Check all the warmup problems
python run.py --test t1          # Check the test problem t1 from tests.py
python run.py --alltests         # Check all the test problems

# Check all the test problems; if any of them fail, then launch the pdb
# debugger so you can find the difference
python run.py --alltests --pdb

This will show:

python run.py --allwarmups
Running w1
Running w2
...
Running w20
Ran warmup tests
20/20 = 100.0

Warmup Problems

You need to solve all 20 of the warmup problems in warmups.py. These are all solvable with one line of code.

Test problems

You need to solve all 20 problems in tests.py. Many are not solvable in one line. You may not use a loop to solve any of the problems, with the exception of t10 (but this one can also be solved without loops).

Here is one example:

def t4(R, X):
    """
    Inputs:
    - R: A numpy array of shape (3, 3) giving a rotation matrix
    - X: A numpy array of shape (N, 3) giving a set of 3-dimensional vectors

    Returns:
    A numpy array Y of shape (N, 3) where Y[i] is X[i] rotated by R

    Par: 3 lines
    Instructor: 1 line

    Hint:
    1) If v is a vector, then the matrix-vector product Rv rotates the vector
       by the matrix R.
    2) .T gives the transpose of a matrix
    """
    return None

For each problem we provide:

  • Inputs: The arguments that are provided to the function
  • Returns: What you are supposed to return from the function
  • Par: How many lines of code it should take. If it takes more than this, there is probably a better way to solve it. Except for t10, you should not use any explicit loops.
  • Instructor: How many lines our solution takes
  • Hints: Functions and other tips you might find useful for this problem

What to submit

  • On Canvas, submit a .zip file of your code (all files)
    • This should contain a single directory, which is your uniqname, which contains the code for the assignment. For example if I (uniquname justincj) were submitting my code, the zip file should contain files like justincj/run.py, etc. We provide a script that validates this submission format.
  • On Gradescope, submit a .pdf file with:
    • Your code (warmups.py and tests.py)
    • The terminal output when running python run.py --alltests and python run.py --allwarmups (copy-paste)
    • You can create the pdf however you like: notepad, Microsoft Word, LaTeX, etc. We care about the content, not the formatting or typesetting.