EX05 - more `list` Utility Functions


In this exercise you will continue building some list utility functions and get some practice writing unit tests for them. Your function implementations may only make use of the built-in len function, and a list object’s methods append and pop.

Specifically off-limits in this exercise are the following. Making use of any of the following will result in no credit for the function you use them in:

  • Cannot use other built-in function besides len - specifically not max, min, slice
  • Cannot use slice notation in conjunction with the subscription operator
  • Cannot use the in operator of Python
  • Cannot use the list class’s + or == operators nor built-in methods beyond append and pop
    • Note: You can use + and == for individual elements, just not entire lists.

Assignment Outline

  • only_evens (Level: Novice) – 30 Points Autograded
    • Unit Tests – 5 Points Autograded
  • sub (Level: Advanced) – 35 Points Autograded
    • Unit Tests – 5 Points Autograded
  • concat (On Your Own) – 15 Points Autograded
    • Unit Tests – 5 Points Autograded
  • Style, Linting, Typing – 20 Points Autograded

Note: Even if your functions are not 100% correct or finished, you can get full credit for the unit tests if you set up a function skeleton and write your tests assuming correct functionality.

0. Pull the skeleton code

You will find the starter files needed by “pulling” from the course workspace repository. Before beginning, be sure to:

  1. Be sure you are in your course workspace. Open the file explorer and you should see your work for the course. If you do not, open your course workspace through File > Open Recent.
  2. Open the Source Control View by clicking the 3-node (circles) graph (connected by lines) icon in your sidebar or opening the command palatte and searching for Source Control.
  3. Click the Ellipses in the Source Control pane and select “Pull” from the drop-down menu. This will begin the pulling process from the course repository. It should silently succeed.
  4. Return to the File Explorer pane and open the exercises directory. You should see it now contains another directory named ex05. If you expand that directory, you should see the starter file for the three functions you’ll be writing.
  5. If you do not see the ex05 directory, try once more but selecting "Pull From" and select upstream in step 2.

1. only_evens – 30 Points

This is the first function you will write in utils.py. The other two functions will also be defined in this file.

Given a list of ints, only_evens should return a list containing only the elements of the input list that were even. Example usage:

>>> only_evens([1, 2, 3])
[2]
>>> only_evens([1, 5, 3])
[]
>>> only_evens([4, 4, 4])
[4, 4, 4]

Continue by defining a skeleton function with the following signature:

  1. Name: only_evens
  2. Arguments: A list of integers.
  3. Returns: A list of integers, containing only the even elements of the input parameter.

2. sub – 35 Points

In this exercise you will write a function named sub. Given a list of ints, a start index, and an end index (not inclusive), sub should generate a List which is a subset of the given list, between the specified start index and the end index - 1. This function should not mutate its input list.

Example usage:

>>> a_list = [10, 20, 30, 40]
>>> sub(a_list, 1, 3)
[20, 30]

Next, define a skeleton function with the following signature in ex05/utils.py:

  1. Name: sub
  2. Parameters: A list and two ints, where the first int serves as a start index and the second int serves as an end index (not inclusive).
  3. Returns: A List which is a subset of the given list, between the specified start index and the end index - 1.

If the start index is negative, start from the beginning of the list. If the end index is greater than the length of the list, end with the end of the list.

If the length of the list is 0, start > len of the list or end <= 0, return the empty list.

3. concat ON YOUR OWN – 15 Points

In this exercise you will write a function named concat. Given two Lists of ints, concat should generate a new List which contains all of the elements of the first list followed by all of the elements of the second list.

Define your function with the following signature.

  1. Name: concat
  2. Parameters: Two lists of ints.
  3. Returns: A List containing all elements of the first list, followed by all elements of the second list.

concat should NOT mutate (modify) either of the arguments passed to it.

4. Unit Tests

For each function from above (only_evens, sub, concat), you are to define at least 3x unit test functions. Remember that a unit test function starts with test_. In order for our grader to execute correctly, import these functions using the complete path. This should look like from exercises.ex05.list_utils import only_evens, sub, concat.

The 3 unit tests should consist of:

  • One edge case
  • Two use cases

Include descriptive function names and docstrings like we saw in class on Tuesday so that it captures what is being tested.

The command to run your tests is python -m pytest exercises/ex05 or you can run them using the beaker tab in VSCode if it is working (it tends to be a bit flaky).

If your screen is large enough, you are encouraged to open these files side-by-side in VSCode by dragging the tab of one to the right side of VSCode so that it changes to a split pane view. Closing your file explorer can help give you additional horizontal space.

5. Make a Backup Checkpoint “Commit”

As you make progress on this exercise, making backups is encouraged.

  1. Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
  2. Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
  3. Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
    • If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
  4. In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Progress on Exercise 3” will suffice.
  5. Press the Check icon to make a Commit (a version) of your work.
  6. Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.

5. Submit to Gradescope for Grading

Login to Gradescope and select the assignment named “EX05 - More Lists.”. You’ll see an area to upload a zip file. To produce a zip file for autograding, return back to Visual Studio Code.

If you do not see a Terminal at the bottom of your screen, open the Command Palette and search for “View: Toggle Integrated Terminal”.

Type the following command (all on a single line):

python -m tools.submission exercises/ex05

In the file explorer pane, look to find the zip file named “21.mm.dd-hh.mm-exercises-ex05.zip”. The “mm”, “dd”, and so on, are timestamps with the current month, day, hour, minute. If you right click on this file and select “Reveal in File Explorer” on Windows or “Reveal in Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.

Autograding will take a few moments to complete. If there are issues reported, you are encouraged to try and resolve them and resubmit. If for any reason you aren’t receiving full credit and aren’t sure what to try next, come give us a visit in office hours!

Contributor(s): Marlee Walls, Kris Jordan