Short Exercises #1¶
Due: Wednesday, Oct 6 at 4:30pm CDT
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.
That said, because this is your first piece of graded work, you should expect to spend some time familiarizing yourself with the mechanics of testing and submitting your work. Most notably, you will have to complete our Git Tutorial to be able to fetch a series of instructor files, and to submit your work. That said, please note that you will be able to start working on the exercises before you complete that tutorial.
To ensure you don’t have to deal with last-minute issues before the deadline, we strongly encourage you to hit the following informal deadlines:
By the end of Sunday, October 3rd: Make sure you have worked through the Git Tutorial.
By the end of Tuesday, October 5th: Make sure you have made at least one submission. It is ok if you submit an incomplete solution as a way to familiarize yourself with the mechanics of submitting your work (you are allowed to make as many submissions as you want before the deadline; we will only count your last submission)
In the interim, 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.
Exercises¶
Expressions¶
Given two integer variables,
a
andx
, write a single expression that adds1
toa
, and then multiplies the resulting value byx
Given three integer variables,
x
,lb
, andub
, write a single boolean expression that evaluates toTrue
ifx
is outside the range betweenlb
andub
(inclusive), andFalse
otherwise. For example, ifx
is11
,lb
is1
andub
is10
, thenx
is outside the range fromlb
toub
and the result would beTrue
. If, on the other hand,x
is10
,lb
is1
andub
is10
thenx
is not outside the range fromlb
toub
inclusive and the result would beFalse
.
Control Flow¶
Given an integer variable
x
, write a conditional statement that will assign to a variables
the string value"POSITIVE"
,"NEGATIVE"
, or"ZERO"
(depending on whetherx
is positive, negative, or zero, respectively)Given integer variables
lb
,ub
,p
, andq
, write a loop that will count how many numbers betweenlb
andub
(inclusive) are divisible byp
or divisible byq
, but not divisible by bothp
andq
. For example, supposep
is2
andq
is3
. If we counted how many numbers between 1 and 20 were divisible by either 2 or 3 (but not both), the result would be 10 (because seven values–2, 4, 8, 10, 14, 16, 20–are divisible by2
but not3
and three values–3, 9, and 15–are divisible by3
and not by2
for a total of 10 values.
Lists¶
Given a list of integers
lst
and a variableval
, write a loop to count the number of values in the list that are strictly greater thanval
. For example, given the list[-1, -2, -3, -4]
and0
forval
, the result would be zero because there are no values that are strictly greater than zero in the list.Given a list of integers
lst
, create a new list with its values negated. For example, given list[-1, 2, -3, 4]
, the result should be this new list:[1, -2, 3, -4]
Testing and Submitting your Solutions¶
To test and submit your solution, you will need to perform some steps that require completing our Git Tutorial. However, as noted above you do not have to wait to complete the Git Tutorial to work on these short exercises.
That said, we strongly encourage you to allocate some time to work through these instruction 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¶
Using the invitation URL provided on Ed Discussion, create a repository for your Short Exercises files (you will be using this repository not just for this set of short exercises, but also for all subsequent ones this quarter).
Next, make sure you’ve
set the GITHUB_USERNAME
variable by running the following (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
)
And finally, run these commands (if you don’t recall what some of these commands do, they are explained at the end of the Git Tutorial):
cd ~/cmsc12100
mkdir short-exercises-$GITHUB_USERNAME
cd short-exercises-$GITHUB_USERNAME
git init
git remote add origin git@github.com:uchicago-cmsc12100-aut-21/short-exercises-$GITHUB_USERNAME.git
git remote add upstream git@github.com:uchicago-cmsc12100-aut-21/short-exercises.git
git pull upstream main
git branch -M main
git push -u origin main
You will find the files you need for the short exercises in the se1
directory.
This contains a README.txt
file that explains what each file is. Make sure you read that file.
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 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”. Since this is your first submission on Gradescope, you will first need to connect your Gradescope account to GitHub. Click on the “Connect to GitHub” button. Pressing that button will take you to a GitHub page asking you to confirm that you want to authorize Gradescope to access your GitHub repositories. Just click on the green “Authorize gradescope” button.
Then, under “Repository”, make sure to select your uchicago-cmsc12100-aut-21/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% |
Ungradable |
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, and the number of submissions you make has no bearing on your score.