Short Exercises #1¶
Due: Sunday, Oct 11 at 3pm CDT
The following short exercises are intended to help you practice some of the programming concepts introduced in weeks 1 and 2. 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.
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 strictly betweenlb
andub
, andFalse
otherwise.
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 by bothp
andq
. For example, supposep
is2
andq
is3
. If we counted how many numbers between 1 and 20 were divisible by both 2 and 3, the result would be 3 (because only 6, 12, and 18 are divisible by both 2 and 3).
Lists¶
Given a list of integers
lst
, write a loop to count the number of negative numbers in the list. For example, list[-1, 2, -3, 4]
has two negative numbers in it.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 pull some instructor files to your repository, and then add your code to one of those files. This process will be very similar in all the exercises and programming assignments, so we have included detailed instructions on how to do this in our Coursework Basics page. Go to that page, and carefully read and follow the instructions in the “Getting the assignment’s files” section. You do not have to read the rest of that page just yet, but you are welcome to skim it.
Once you’ve fetched the instructor-provided files from the “upstream” respository, you should see an se1
directory in your local repository. This contains a README.txt
file that explains what each file is. Make sure you read that file.
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
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 rest of the Coursework Basics page.
Once you’ve completed the exercises, you must submit your work through Gradescope (linked from our Canvas site). In the “Short Exercises #1” assignment, simply upload file se1.py
(do not upload any other file!). Please note:
You are allowed to make as many submissions as you want before the deadline.
There are no extensions for the short exercises. The two free extensions you get for the programming assignments cannot be applied towards the short exercises. Please note that, if you need an extension due to extraordinary circumstances, you should alert us via a private message on Piazza.
Your score on the short exercises is determined solely based on the automated tests, but we may adjust your score if you attempt to pass tests by rote (e.g., by writing code that hard-codes the expected output for each possible test input).
Gradescope will report the test score it obtains when running your code. If there is a discrepancy between the score you get when running our grader script, and the score reported by Gradescope, please let us know so we can take a look at it.
In Lab #0.5, you learned about a tool called chisubmit. That tools is only used on the programming assignments. You do not need to worry about it in these exercises.