Short Exercises #2

Due: Wednesday, Oct 13 at 4:30pm CDT

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 see the “Fetching the instructor files” section of Short Exercises #1 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 ~/cmsc12100
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 peep, which takes two digits p and e, returns True if the digits p and e can replace \(p\) and \(e\) so that \(peep\) is equal to \((pp)^e\), and False otherwise. For example, the call peep(1, 3) should return True since \(1331 = (11)^3\). You can assume that the inputs p and e are digits (the numbers 0-9).

Lists parameters

  1. Complete the function has_more, which takes two lists and a target value and returns True if the first list has more of the target value than the second list, and False otherwise. For example, given the lists [1, 1, 2, 3] and [2, 1, 1, 1], and target 1, the function would return False. There are not more 1s in the first list than than the second. This is a good place to use a helper function to count the number of occurrences of a target value in a list. You may not use the built-in count method.

Returning lists

  1. Complete the function make_star_strings, which takes a list of nonnegative integers and returns a list of strings where each string is a string of stars (*). The number of stars in each string is the corresponding number in the input list. For example, a call to make_star_strings with the list [2, 1, 3, 0] would return ["**", "*", "***", ""]. Notice that a zero in the input list produces the empty string (a string with zero stars).

Modifying lists in place

  1. Complete the function replace, which takes a list, a value replacee, and another value replacer, and replaces all instances of replacee with replacer in the list in place. For example, if the input variable lst has the values [2, 1, 4, 1], calling replace(lst, 1, 3) will update the value of lst to be [2, 3, 4, 3].

Lists of lists

  1. Complete the function rows_and_columns_contain, which takes a list of lists and a target value, and returns True if the target value occurs at least once in every row and column of the input, and False otherwise. Here are some sample uses of the function and expected results:

    grid =  [[2, 1, 1, 2],
             [1, 2, 3, 1],
             [3, 3, 1, 2],
             [1, 2, 1, 3]]
    
     rows_and_columns_contain(grid, 1) # expected result: True
    
     rows_and_columns_contain(grid, 2) # expected result: False (no 2 in third column)
    
     rows_and_columns_contain(grid, 3) # expected result: False (no 3 in first row)
    

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

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

Ungradable

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.