Short Exercises #1

Due: Wednesday, October 5th at 4:30pm CT

The following short exercises are intended to help you practice some of the programming concepts introduced in Module #1. These exercises should not take more than 1-2 hours in total to complete.

We encourage you to start by coming up with possible solutions directly on IPython. Once you’ve done this, you will have to follow a series of steps to get the code that will allow you to test and submit your solutions. In future exercises, you will want to perform those steps first but, for this first set of exercises, it will be better if you start working on the exercises informally, and start asking questions if you get stuck or anything is unclear.

We strongly encourage you to allocate time to work through these instructions well ahead of the deadline, to make sure you are able to properly test and submit your solution before the deadline (and so there is plenty of time for us to help you if you run into issues). If you wait until right before the deadline to start going through these instructions, you may not be able to submit your work on time.

Fetching the instructor files

If this is your first time working through a set of Short Exercises, please work through Short Exercises #0 to set up your Short Exercises repository.

To get the files for this set of short exercises, first set the GITHUB_USERNAME environment variable by running the following command at the Linux command line (replacing replace_me with your GitHub username):

GITHUB_USERNAME=replace_me

(remember you can double-check whether the variable is properly set by running echo $GITHUB_USERNAME)

Then navigate to your Short Exercises repository and pull the new material:

cd ~/capp30121
cd short-exercises-$GITHUB_USERNAME
git pull upstream main

You will find the files you need in the se1 directory.

Exercises

Expressions

  1. Given two integer variables, a and x, write a single expression that adds 1 to a, and then multiplies the resulting value by x.

  2. Given two integer variables, n (a numerator) and d (a denominator), write a single expression that evaluates to the fractional part of n/d. For example, if n is 3 and d is 2, n/d is 1.5 and the fractional part is 0.5.

Control Flow

  1. Given two variables x and y that represent a point in the plane, use conditionals to assign to a variable r the value True if (x, y) is strictly outside of the unit square, and False otherwise. The lower left corner of the unit square is the point (0, 0) and the upper right corner is the point (1, 1).

  2. Given integer variables a and b, write a loop that will count how many positive integers divide both a and b evenly. You may assume that a is less than or equal to b. For example, suppose a is 20 and b is 30. The numbers 1, 2, 5, and 10 divide both 20 and 30, so a and b have 4 common divisors.

  3. Given a list of integers lst, write a loop to count the number of values in the list that are even and odd. Use a conditional statement to assign to the variable s the string "EVENS", "ODDS", or "NEITHER", depending on whether the list has more evens than odds ("EVENS"), more odds than evens ("ODDS"), or the same number of evens and odds ("NEITHER").

  4. Given a list of values lst, write a loop to assign to the variable n the first negative value in the list. For example, suppose lst is the list [3, -5, 4, -6, -1]. Your code should assign the value -5 to n. You may assume there is at least one negative value in lst.

Adding your solutions to se1.py

Next, you will be adding your solutions in the se1.py file. Our testing framework relies on your code being inside a series of functions, which we have not yet covered in class. Don’t worry: we’ve specified exactly where in the file you need to add your code, so you don’t have to worry about how functions work just yet. For example, this is where you would place your code for the first exercise:

def add_one_and_multiply(a, x):
    """ Add 1 to a, and multiply by x"""

    ### EXERCISE 1 -- YOUR CODE GOES HERE
    # Replace "None" with the correct expression
    r = None

    ### DO NOT MODIFY THE FOLLOWING LINE!
    return r

In this case, as instructed in the code, you have to replace None with your expression. For example, let’s say the solution we came up with is a + 1 * x (this is actually not the correct expression; can you see why?). In this case, we would edit the file to look like this:

def add_one_and_multiply(a, x):
    """ Add 1 to a, and multiply by x"""

    ### EXERCISE 1 -- YOUR CODE GOES HERE
    # Replace "None" with the correct expression
    r = a + 1 * x

    ### DO NOT MODIFY THE FOLLOWING LINE!
    return r

Testing and Submitting your Solutions

Testing your solutions

Next, you will want to ensure that your code works as expected. We explain how to do this in the Testing Your Code page. Go to that page now, and read it from start to finish. Please note that it uses the above exercise as an example. To load your code into IPython to do some manual testing, make sure to run the following at the start of your IPython session:

$ ipython3

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: import se1

Once you’ve written and tested your code, you will want to commit it and push it to the git server. Now is a good time to read the Coursework Basics page, which goes over some logistics that you’ll encounter in most coursework in this class. Pay special attention to the section on how to use Git effectively!

Submitting your work

Once you’ve completed the exercises, you must submit your work through Gradescope (linked from our Canvas site). Gradescope will fetch your files directly from your GitHub repository, so it is important that you remember to commit and push your work!

To submit your work, go to the “Gradescope” section on our Canvas site. Then, click on “Short Exercises #1”. Then, under “Repository”, make sure to select your uchicago-CAPP30121-aut-2022/short-exercises-$GITHUB_USERNAME.git repository. Under “Branch”, just select “main”.

Finally, click on “Upload”. An autograder will run, and will report back a score. Please note that this autograder runs the exact same tests (and the exact same grading script) described in Testing Your Code. If there is a discrepancy between the tests when you run them on your computer, and when you submit your code to Gradescope, please let us know.

Your ESNU score on this set of exercises will be determined solely on the basis of these automated tests:

Grade

Percent tests passed

Exemplary

at least 95%

Satisfactory

at least 80%

Needs Improvement

at least 45%

Unsatisfactory

less than 45%

Please remember that you can submit as many times as you want before the deadline. We will only look at your last submission before the deadline, and the number of submissions you make has no bearing on your score.