Place these files in a lab4 folder. Before leaving lab, zip up the lab4 folder and hand in the zip file.
Creating a list containing a range of elements
list(range(0, 10)) (not needed today, but note that [elem] * N replicates a single value)Linear search function to find a list element equal to the key. Note that the same structure can be used to find a list element having some other property (e.g., is even, odd, positive, negative, etc.).
def search(list, key): for i in range(0, len(list)): if list[i] == key: return i return NoneTiming linear search for 700 in list(range(100,1000000)) by using time()
import time def timer(): start = time.time() """ insert the code to be timed here, e.g. search(list, key) Make sure to remove the quote marks! """ end = time.time() return end - start
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.
Time how long it takes the search function above to find each of the following in big_list:
How long does each take?
If you repeat the same searches, does it take exactly the same amount of time? Why do you think this is?
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.
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
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:
[ 't', 'u', 'v', 'w', 'x', 'y', 'z' ]the search must examine 't', 'v', 'x', and 'z'. But if the gap value is 3 the search must examine 't', 'w', and 'z'; if the gap is 4 it must examine 't' and 'x'; and if the gap is greater than 6 only 't' must be examined.
Use the following algorithm:
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 >>>
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.