Lab 9
Lab 9
Lab # 9
DICTIONARIES
1
Starting Lab 9
• Open a browser and log into Brightspace.
• On the left hand side under Labs tab, find lab6 material
contained in lab9-students.zip file.
2
Before starting, always make sure you
are running Python 3
This slide is applicable to all labs, exercises, assignments … etc
That is, when you click on IDLE (or start python any other way)
look at the first line that the Python shell displays. It should say
Python 3 (and then some extra digits)
3
Do all the exercises labeled as
Task in your head i.e. on a
paper
Prog ex Dic 2:
Dictionaries
Prog ex Dic 3:
Dictionaries
Prog ex Dic 3 (from the textbook):
9
Task 1:
For each of the following 7 functions answer the question about
how many operations the functions does, roughly, as n grows. For
example the answer is roughly linear in n for foo3, i.e O(n)
Task 2: answer the 4 1. What does the program on
the left print?
questions
1. Write one sentence that
describes abstractly (in
plain English) what the
function annona does?
(something a person with
no programming knowledge
can understand)
>>> L=[1,20,-1,10,-5,10]
>>> L.index(10)
3
>>> A=['d', 'a', 'b', 'a']
>>> A.index('a')
1
>>> A.index(‘c')
-1
Both in and .index methods perform linear search and thus take linear number
of operations in the size of the list
Study: overview of Linear Search
Linear search starts at index 0 and looks at each item one by one.
At each index, we ask this question: Is the value we are looking for
at the current index?
Prog ex 1:
•All three versions of linear search in linear_search-3-versions.py
start at index 0. Rewrite all three to search from the end of the list
instead of from the beginning. Make sure you test them.
Task 4:
•For the new versions of linear search: if you are looking for value v
and it appears in the list more than once, which position will your
modified linear searches find?
Programming ex 2: min or max and Task 5
Prog Ex 2:
•Write a function named min_or_max_index that has two
parameters: one of type list and another type bool. If the
Boolean parameter refers to True, the function returns a tuple
containing the minimum and its index; and if it refers to False, it
returns a tuple containing the maximum and its index.
(do not use python’s min and max functions – roll your own
implementation)
Task 5:
•On a list of size n, what is roughly the number of operations
your program does in the worst case? (constant, linear in n,
quadratic in n, log n ….?)
Study: overview of Binary Search and Task
6
IMPORTANT:
Binary search is used to find an item in an SORTED list!
It does, roughly, log2 n of operations (in the worst case, i.e. O(log2 n)),
where n the size of the list.
Task 6:
Open the file called binary_search.py. It contains the binary search
version we developed in class and study again how it works.
Question: Binary search is significantly faster than the linear search but requires
that the list is sorted. As you know, the number of operations for the best sorting
algorithm is on the order of n log2 n, where n is the size of the list. If we search a lot
of times on the same list of data, it makes sense to sort it once before doing the
searching. Roughly how many times do we need to search in order to make sorting
and then searching as fast or faster than linear search?
Programming exercise 3 (a bit of a
challenge):
1. Write a function called first_one(L) that takes as input a list where each
element of L is either a number 0 or 1. In addition all 0s appear before all
1s. Function first_one(L) should return the position in L of the first 1. If
there is no 1s in the list, return -1.
Unless the list is very small (less than 3 elements) your solution should not
access all the elements in the list. In particular if the list has 1000 elements
you solution should access roughly 10 of them, if it has 100,000 elements it
should access not more than 20. Roll your own implementation (only use
loops and if statements). Basically it should run in O(log n) operations.
>>> first_one( [0,0,0,0,0,0,1,1] )
6
>>> first_one( [1,1] )
0
>>> first_one( [0,0,0] )
-1
2. Repeat the exercise except this time write a function called last_zero(L) that
returns the position of the last 0. If helpful, you can make a call to your own
function first_one(L)
Study Refined Binary Search
The version of binary search we developed in class returns SOME
POSITION where value v is found in the given list L (and -1 if v is
not in the list).
Open file applications.py and program (and test) the 6 functions described
there. All your solutions should perform
at most O(n log n) operations.
The only Python function you can use is “.sort” or “sorted” (since you know
something about the number of operations they take and how they work
roughly). You can also use .append and assume one append call takes constant
number of operations.
DO NOT USE DICTIONARIES. DICTIONARIES are black boxes, for now. Their
running time analysis you will only study in the 2nd year.
BONUS TASK 8: CHALLENGE ANALYSIS:
The function below sorts the given list L. Can you figure out how
many operations it takes in the worst case? In other words is it
better of worse or the same as as selection sort (selec. sort does
O(n2) operations). What about merge sort? Think of L being a list where
elements are ordered from biggest to smallest. That is the worst case
for the below algorithm. You can assume that .append and .pop(0) take
constant number of operations. min takes linear on a list of size n.
You can find rough running times of Python’s operations here:
https://wiki.python.org/moin/TimeComplexity
AFTERWARDS, AT HOME:
--- Study the insertion sort from the 2nd recommended textbook
(Practical Programming). It is yet another algorithm that takes
quadratic number of operations, i.e. O(n2), to sort. Try to figure
out why in the worst case it takes quadratic number of operations?