Introduction to Computer Programming: Lab 4

Time for Lists

Instructions

Unless otherwise indicated, use only the Python operations and commands we have described thus far in class.

Show your work for a given problem to the instructor or one of the lab assistants before moving on to the next problem, but do not get too bogged down on any one problem.

In the common case, you will not complete the exercises during our scheduled lab. It is then up to you to complete the lab on your own time.

You are welcome to download a runnable solution module called lab4x. If you place that file in the same folder as the file you are working in, then from the IDLE prompt (>>>) you can import the solution module to see it in action. For example:

Complete flip_list(bools) so that it changes every element of the bools (a list of Booleans) to be its logical opposite. Your function should be side-effecting: it should modify (mutate) items, but not return anything. Use a for loop with enumerate. Example:

Complete make_random_list(n, start, stop) so that it returns a list consisting of n random integers each at least as large as start but less than stop. Example:

>>> make_random_list(5, 100, 200)
[141, 101, 135, 170, 123]

Examine the functions tup_sub, mid_list_updates_per_sec, mid_tup_updates_per_sec and compare_mid_updates. Experiment using compare_mid_updates to try and determine which is more efficient (and by how much) between creating a new tuple with its middle element replaced; and modifying the middle element of a list.

Complete prepend(items, x) so that it inserts x at the front of items. (Side-effecting.) Examples:

Following the models of mid_list_updates_per_sec, mid_tup_updates_per_sec and compare_mid_updates and append_pop_per_sec, complete the two functions prepend_pop_per_sec(a) and compare_pend(start, stop). Then use compare_pend as you did with compare_mid_updates to determine which is more efficient (and by how much) between prepending and appending an item onto a list.

Complete count_last_digit(ns) so that it returns a 10-element array of integers reflecting how many numbers in ns end in 0, 1, ... up to 9. Example:

Complete ripple(bools) so that it modifies bools (assumed to be a list of Booleans values with at least one item on it) so that if the front (position 0) element is False it becomes True and the process stops; if the front element is True it changes to False and the process "ripples" back toward the end of the list. If the last element is True, then the ripple changes that to False and appends a new True element to the back of the list. (Side-effecting.) Examples: