Introduction to Computer Programming: Lab 4

Time for Lists

Instructions


Exercises

  1. Warmup. Consider these examples. Experiment with lists by typing these examples into IDLE's shell:

    >>> seasons = ['Winter', 'Spring', 'Summer', 'Fall']   # a list literal
    >>> seasons[3] = 'Autumn'   # lists are mutable
    >>> seasons
    
    >>> cs_fac = []           # building a list manually with append
    >>> cs_fac.append('Jim')
    >>> cs_fac.append('Mike')
    >>> cs_fac
    
    >>> ','.join(seasons)
    
    >>> 'the quick brown fox'.split()
    
    >>> ns = [2, 11, 3, 7, 5]
    >>> max(ns)
    >>> sum(ns)
    
    >>> import time
    >>> time.perf_counter()
    >>> start_time = time.perf_counter()
    >>> start_time
    >>> s = 0
    >>> for i in range(10**7):
            s += i
    >>> end_time = time.perf_counter()
    >>> end_time - start_time

  2. 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:

    >>> bxs = [True, False, True, False, False]
    >>> flip_list(bxs)
    >>> bxs
    [False, True, False, True, True]
  3. 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]

  4. 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.


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

    >>> its = []
    >>> prepend(its, 'hello')
    ['hello']
    >>> a = [3, 5, 7, 11]
    >>> prepend(a, 2)
    >>> a
    [2, 3, 5, 7, 11]
  6. Complete rotate(items, n) so that, assuming the list is not empty, rotates the last n items from the end of the list onto the front of the list. Use pop and prepend. (Side-effecting.) Example:

    >>> words = 'four score and seven years ago'.split()
    >>> rotate(words, 4)
    >>> words
    ['and', 'seven', 'years', 'ago', 'four', 'score']
  7. 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.


  8. 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:

    >>> count_last_digit([36, 24, 528, 26, 7, 60, 2017, 4, 127, 311])
    [1, 1, 0, 0, 2, 0, 2, 3, 1, 0]
    >>> count_last_digit([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024])
    [0, 1, 3, 0, 3, 0, 2, 0, 2, 0]
  9. 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:

    >>> b = [False]
    >>> ripple(b)
    >>> b
    [True]
    >>> ripple(b)
    >>> b
    [False, True]
    >>> bb = [True, True, True]
    >>> ripple(bb)
    >>> bb
    [False, False, False, True]
    >>> bbb = [True, True, False, True, False, True]
    >>> ripple(bbb)
    [False, False, True, True, False, True]