AnnouncementsCourse evaluation
Your opinion matters!Attendance grades
Will be posted prior to the finalProject 5 grades
Will be posted prior to the finalOne day extension for Project 5, now due Dec 8th
at 11:59pm
Final35 Questions Multiple ChoiceSame format as midtermsDo not be surprised to find questions similar to
the most frequently missed questions on your two midterms
Final – unique questions3 questions complexity3 questions recursion2 questions search2 questions sorting
Final – some questions may combine multiple
topics!2 questions functions/returns2 questions matrices2 questions dictionaries2 questions I/O files2 questions string manipulation2 questions trees2 questions loops2 questions conditionals2 question binary
Other Stuff that will (or may) appear
Auxiliary functions and operators: len, range, ord, chr, %, [ : ], not, or, and
Useful string and list manipulation functionsstrip, split, rfind, find, append, etc.
Python short handelif, a,b = b,a, +=
Other Stuff that will (or may) appear
Modulesrandom
True and FalseWhat values are consider to be True and what
values are considered to be False
Do a self assessment2 practice midterms2 midtermsFinal Practice questionsRead through solutions to project 1-4
Is there code you do not understand?Read through lab solutions
Is there code you do not understand?
Key things to go overRecursion examples – recitation + lecture slidesKnow the complexity of your searching and
sorting algorithms (memorize!)You do NOT need to memorize the code for the
algorithms themselvesYou should know the intuition behind why they
workKnow the complexity classes (least complex to
most complex) (memorize!)
Key things to go overBe able to identify the growth term
(hint: think about the relation to complexity classes)
Example code in prelabs!Review slides from both midtermsReview slides for the final
Things you do NOT need to review
Project 5 Solution It will be posted too close to the exam
Chapter 9 and the recitation slides that go along with itNaming of loops:
Sentinel, Interactive, File, Nested You just need to know what the loop is doing, not what it is
calledLab 15 solutionOS Module, urllib ModuleGraphics library
What should you know about bucket sort?
Consider a special (simpler) case of bucket sortAssume we know something about the list of
numbers we are sortingAll numbers are integersWe know the maximum numberWe know the minimum number
How should we sort such a list?
Create one bucket for each integer including and between min and max
Traverse the list we want to sortPlace integer in corresponding bucketNote: we do not need to sort the buckets as each
number in them is the same!What is the complexity of this?
Afterwards combine all the bucketsWhat is the complexity of this?
Complexity of Bucket SortOne final assumption: size of list that we want
to sort > (max- min)How can we find the complexity?Traversing the list and placing an integer into a
bucket is O(n)We do one “piece” of work for each item in the list
Inserting into a list without worrying about order is O(1)
Combining buckets is no more complex than O(n)
What does this code do?
def myFun(myList): n = len(myList) i = 1 while ( i<n):
myList[i] = i i = i*2
return myList
What does this code do?def myFun(myList):
n = len(myList) i = 1 while ( i<n):
myList[i] = i i = i*2
return myList >>> myFun([0]) [0] >>> myFun([0,0,0,0]) [0, 1, 2, 0]>>> myFun([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])[0, 1, 2, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 16, 0]
What is the complexity?
A: O(n) B: O(n2)C: O(1)D: O(log n)
def myFun(myList): n = len(myList) i = 1 while ( i<n):
myList[i] = i i = i*2
return myList
NOTE: you will NOT need to be able to do a problemlike this on the final
What does this code do?def trickyReturns(list): k = 0 for w in range(len(list)): if(list[w] == 1001): return w else: k=k+1 return k
What does this code do?def trickyReturns(list): k = 0 for w in range(len(list)): if(list[w] == 1001): return w else: k=k+1 return k
>>> trickyReturns([0,1,3,4,5])1>>> trickyReturns([1001,1,3,4,5])0>>> trickyReturns([0,1,1001,3,4,5])1
What is the complexity?def trickyReturns(list): k = 0 for w in range(len(list)): if(list[w] == 1001): return w else: k=k+1 return kA: O(n)
B: O(n2)C: O(1)D: O(log n) NOTE: you will NOT need to be able to do a problem
like this on the final
How to think about recursion
First identify the terminating conditionThen think about the problem in terms of a
supply chainWhat is each entity in the supply chain doing?
Try tracing on a small input
What does this code do?def mystery(x): if x == 1: return 1 else: return x * mystery(x-1)
What does this do?def mystery(x): return x + mystery(x-1)
How do we fix it?def mystery(x): if x == 0: return 0 else: return x + mystery(x-1)
Tracing the mystery function
mystery(5)5 + (mystery(4))5 + (4 + (mystery(3)))5 + (4 + (3 + (mystery(2))))….Why are the parenthesis important?
What if we had this function?
def mystery(x): if x == 0: return 0 else: return x - mystery(x-1)
Tracing the mystery function
mystery(5)5 - (mystery(4))5 - (4 - (mystery(3)))5 - (4 - (3 - (mystery(2))))….
>>> mystery(3)2>>> mystery(4)2>>> mystery(5)3>>>
Fun things to do with Python
Build video gameshttp://pygame.org/news.htmlhttp://rene.f0o.com/mywiki/
PythonGameProgramming
Lego MindstormsProgram your robots with Python
http://code.google.com/p/nxt-python/
Professional Python UseBio Informatics
http://shop.oreilly.com/product/9780596154516.doNumpy / Scipy
http://numpy.scipy.org/
Final35 Questions Multiple ChoiceSame format as midtermsDo not be surprised to find questions similar to
the most frequently missed questions on your two midterms
Book ChaptersChapters 4-8Chapters 11 and 13
Identify the term that has the largest growth rate
Num of steps growth term complexity6n + 3 6n O(n)2n2 + 6n + 3 2n2 O(n2)2n3 + 6n + 3 2n3 O(n3)2n10 + 2n + 3 2n O(2n)n! + 2n10 + 2n + 3 n! O(n!)
Comparison of complexities:
fastest to slowestO(1) – constant timeO(log n) – logarithmic timeO(n) – linear timeO(n log n) – log linear timeO(n2) – quadratic timeO(2n) – exponential timeO(n!) – factorial time
SearchingLinear Search
Complexity O(n)Why: scan each element
Binary SearchComplexity O(log n)Why: break list into two pieces, discard one piece
SortingBubble sort
Complexity O(n^2) Why: Inner loop compares every pair of elements in the
list, there are n-1 pairs of elements Why: outer loop runs n times, the last time there will be
no swapsMerge sort
Complexity O(n log n) Why: there are log n “level” – ie it takes us log(n) splits to
get to lists of length one Why: it takes us O(n) work to merge all lists on each level
SortingSimplified bucket sort:
Complexity O(n)Why: can each element and place it in a bucket,
then glue all buckets togetherAssumptions: know the range of elements in the
list, assume all elements are integers (finite distribution), distribution is dense (ie the number of elements in the list is equal to or greater than max-min)
Observation: in the worst case we have one element per bucket, merging is still O(n)
What is the terminating condition?
def mystery(x): if x == 1: return 1 else: return x * mystery(x-1)
What if we call mystery with a negative number?
Now what is the terminating condition?
def mystery(x): if x <=1: return 1 else: return x * mystery(x-1)
What if we call mystery with a negative number?
What does this do?
def t4(c): x = {} for i in range(128): x[chr(i)] = i return x[c]
What is the output of the following code?
list = ['A',1,'B',2,'C',3,'D',4]myDict = {} for i in range(0,len(list),2):
myDict[list[i]] = list[i+1]
What is the output of the following code?
list = ['A',1,'B',2,'C',3,'D',4]myDict = {} for i in range(len(list)-1):
myDict[list[i]] = list[i+1]
How can we characterize the difference between the two
pieces of code?In the first example we looked at each non
overlapping par in the listIn the second example we looked at all pairs in
the list (similar to what you did / are doing in project 5)
Conditionals
def sel(mylist): for i in range(len(mylist)): if mylist[i] % 2: print(i)
Conditionalsdef t1(x,y,z):
if x and y or z: x = y if x and not z:
print("goal")else: x = z
else: z = y
HomeworkStudy for the final
Recommended