15110 SUMMER SESSION TWO 2014

Lab 4 - Wednesday, July 9

Goals

The purpose of this lab is to get you to work with iteration as a tool for finding a value with a certain property in a list, and to learn to measure the time consumed by executing some Python code. When you are done, you should be able to

Deliverables

  1. timings.txt
  2. is_all_odd.py
  3. gap_search.py

Place these files in a lab4 folder. Before leaving lab, zip up the lab4 folder and hand in the zip file.

CA Demonstration (as necessary)

Activities

  1. Create a list whose elements contain the integers from 100,000 through 300,000, inclusive (be careful! what should the range be?) and set the variable big_list to contain that list.

    1. Time how long it takes the search function above to find each of the following in big_list:

      1. element at front (100000)
      2. element in middle (200000)
      3. element at back (300000)
      4. element not in the list (3)

      How long does each take?

    2. If you repeat the same searches, does it take exactly the same amount of time? Why do you think this is?

    3. How would you describe the relationship between the position of the item you are searching for in the list and how long it takes to find that item? (Is the search time independent of the position, or is there some mathematical relationship between the two quantities?)

    Write your answers in a text file called timings.txt.

  2. In is_all_odd.py, write a Python function is_all_odd(numlist) that takes an integer list as an argument and returns True if all the elements in the list are odd numbers and False otherwise. (Hint: how is this different from the function search(list, key) given above?)

    Example:

    > python3 -i is_all_odd.py
    >>> 4 % 2
    0
    >>> 5 % 2
    1
    >>> is_all_odd([1, 2, 8, 12, 99])
    False
    >>> is_all_odd([1, 3, 5, 7])
    True
    >>> is_all_odd([])
    True
    
  3. In this exercise, you will iterate over a "gapped" list (i.e., by examining every nth element). In gap_search.py, write a Python function gap_search(list, key, gap). This function works like search(list, key) above, but with two important differences:

    Use the following algorithm:

    1. The parameters are a list, a key, and a gap.
    2. Set i to 0
    3. While i is less than the length of the list:
      1. If the element at index i is equal to the key, return i.
      2. Otherwise, increase i by the gap amount.
    4. (The loop is done at this point, which means the key was not found.) Return None.

    Example:

    > python3 -i gap_search.py
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'x', 1)
    4
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'x', 2)
    4
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'x', 3)
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'x', 4)
    4
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'x', 5)
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'x', 6)
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'x', 7)
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'a', 1)
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'z', 1)
    6
    >>> gap_search(['t', 'u', 'v', 'w', 'x', 'y', 'z'], 'z', 3)
    6
    >>> 
    

  4. Suppose you can be sure the list input to gap_search is always sorted in increasing order. Modify the code to return as soon as it detects that the key cannot be in the gapped list because it has encountered an element greater than the key.