Short Exercises #2

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

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

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 se2 directory.

Exercises

Basic Functions

  1. Complete the function eisenstein_triple, which takes three integers a, b, and c, returns True if a, b, and c form an Eisenstein triple, and False otherwise. The integers a, b, and c form an Eisenstein triple if \(a^2 - ab + b^2 = c^2\). For example, the call eisenstein_triple(3, 8, 7) should return True.

List parameters

  1. Complete the function last_negative, which takes a list and returns the location and value of the last negative in the list as a tuple. For example, given the list [-2, 3, -1, 5, 6], last_negative should return the tuple (2, -1), the index and value of the negative number closest to the end of the list. You can assume the input list contains at least one negative.

String parameters and returning lists

  1. Complete the function expand, which takes a string in the form "a-b" where a and b are integers and returns a list of all the integers from a to b, inclusive. For example, expand("8-13") should return the list [8, 9, 10, 11, 12, 13]. You may assume that a < b. Hint: You may make use of the string method split in this problem.

Modifying lists in place

  1. Complete the function clip_in_range, which takes a list, a lower bound, and an upper bound and modifies the list in-place so that all of the values in the list are between the lower bound and upper bound, inclusive. For example, consider the list [3, -2, 8, 4, 5, 9], a lower bound of 0, and an upper bound of 5. After a call to clip_in_range, the list should contain the values [3, 0, 5, 4, 5, 5]. The value -2, smaller than the lower bound, was clipped to 0, and the values 8 and 9, larger than the upper bound, were clipped to 5. The rest of the values were not modified.

Lists of lists

  1. Complete the function flatten_rows, which takes a list of lists and flattens the values of the input list row-wise into a single list. For example, given the input [[1, 2], [3, 4]], flatten_rows should return the list [1, 2, 3, 4]. That is, the output list contains all of the values from the first row from left to right, followed by the values from the second row, and so on. You may assume that the input list has at least one row, but you should not assume that the number of rows and the number of columns are the same. You may also assume that all rows have the same length.

  2. Complete the function flatten_columns, which takes a list of lists and flattens the values of the input list column-wise into a single list. For example, given the input [[1, 2], [3, 4]], flatten_columns should return the list [1, 3, 2, 4]. That is, the output list contains all of the values from the first column from top to bottom, followed by the values from the second column, and so on. You may make the same assumptions as in #5.

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. Load your code into IPython to do some manual testing, and 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 se2

Once you’ve written and tested your code, you will want to commit it and push it to the git server.

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 #2”. 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 60%

Unsatisfactory

less than 60%

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. 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.