EX05 - `list` Utility Functions


In this exercise you will build some list utility functions. 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 with lists (but you can use for..in loops!)
  • Cannot use the list class’s + or == operators (you can still use + with int and str types, though!) nor built-in methods beyond append and pop
    • Note: You can use + and == for individual elements, just not entire lists.

Assignment Outline

  • only_evens – 35 Points Autograded
  • is_equal – 40 Points Autograded
  • sub – 20 Points Autograded
  • Style, Linting, Typing – 20 Points Autograded

0. Skeleton Code

Create a new directory in exercises named ex05.

Inside the exercises/ex05 directory, create a file named utils.py. Add a docstring and establish an __author__ variable to be assigned a string with the digits of your PID. This is where you will implement your function skeletons and implementations below.

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. is_equal – 35 Points

Given two lists of int values, return True if every element at every index is equal in both lists.

>>> is_equal([1, 0, 1], [1, 0, 1])
True
>>> is_equal([1, 1, 0], [1, 0, 1]))
False

Your implementation should not assume the lengths of each List are equal.

This concept is called deep equality. Two separate list objects on the heap may be distinct objects, such that if you changed one the other would remain the same. However, two distinct objects can be deeply equal to one another if what they are made of is equal to each other in essence.

Define a function with the following signature: 1. Name: is_equal 2. Parameters: Two lists of integers. 3. Returns: True if lists are equal, False otherwise.

Implement the is_equal function as described above.

3. sub ON YOUR OWN – 15 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]

Note: Since the sub function is an ON YOUR OWN part of the assignment, you cannot ask questions about it in office hours.

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.

4. 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 - List Utils”. 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 “yy.mm.dd-hh.mm-exercises-ex05.zip”. The “yy”, “mm”, “dd”, and so on, are timestamps with the current year, 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, Kaki Ryan