CMPT 120: Lab 7


Write a function named trisect that takes one argument as input. The argument can be either a list or a string. The function should return a list with 3 elements. The first element will contain the first third of the input, the second element the middle third, and the last element the last third. Use slices to split up the string.
>>> trisect([1,2,3,4,5,6])
[[1, 2], [3, 4], [5, 6]]

>>> trisect("Hello")
['H', 'el', 'lo']

>>> trisect([1,2,3,4,5,6,7])
[[1, 2], [3, 4], [5, 6, 7]]

>>> trisect([])
[[], [], []]

For this part of the lab you are going to write two functions that each take a list as their only argument.

The first function, reverse, will return a list with the elements of the inputs reversed. The list used as input should not change.

The second function, reverseInPlace, will not return anything. Instead it will reverse the elements in the input list. After the function has been called you will see the changes in that list.
>>> x=[1,2,3,4]
>>> reverse(x)
[4, 3, 2, 1]
>>> x
[1, 2, 3, 4]
>>> reverseInPlace(x)
>>> x
[4, 3, 2, 1]

Suppose you want to write a program that removes the given items from the list. The program should work with two lists, like these:
targetlist = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
removelist = [2, 5, 7]
The idea is to take the targetlist and delete the elements listed in removelist. So, in the example, we want to remove elements 2, 5, and 7 from targetlist. After this it will be [0, 10, 30, 40, 60, 80, 90].

Your first though about how to do this might not work. You can download a broken item remover.
  1. Figure out why the program linked above doesn't work: it loops over the items in removelist and deletes each one. Why doesn't that remove the correct items?
  2. Fix the code so it removes the correct items.


Chris Schmidt, last updated July 2, 2007