return - Purdue CS Wiki/Application server

advertisement
Announcements
 Course evaluation
 Your opinion matters!
 Attendance grades
 Will be posted prior to the final
 Project 5 grades
 Will be posted prior to the final
 One day extension for Project 5, now due Dec 8th at
11:59pm
Final
 35 Questions
 Multiple Choice
 Same format as midterms
 Do not be surprised to find questions similar to the
most frequently missed questions on your two
midterms
Final – unique questions
3 questions complexity
3 questions recursion
2 questions search
2 questions sorting
Final – some questions may
combine multiple topics!
2 questions functions/returns
2 questions matrices
2 questions dictionaries
2 questions I/O files
2 questions string manipulation
2 questions trees
2 questions loops
2 questions conditionals
2 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 functions
 strip, split, rfind, find, append, etc.
 Python short hand
 elif,
a,b = b,a, +=
Other Stuff that will (or may)
appear
 Modules
 random
 True and False
 What values are consider to be True and what values are
considered to be False
Do a self assessment
 2 practice midterms
 2 midterms
 Final Practice questions
 Read 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 over
 Recursion examples – recitation + lecture slides
 Know the complexity of your searching and sorting
algorithms (memorize!)
 You do NOT need to memorize the code for the
algorithms themselves
 You should know the intuition behind why they work
 Know the complexity classes (least complex to most
complex) (memorize!)
Key things to go over
 Be able to identify the growth term
 (hint: think about the relation to complexity classes)
 Example code in prelabs!
 Review slides from both midterms
 Review 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 it
 Naming of loops:
 Sentinel, Interactive, File, Nested
 You just need to know what the loop is doing, not what it is
called
 Lab 15 solution
 OS Module, urllib Module
 Graphics library
What should you know about
bucket sort?
 Consider a special (simpler) case of bucket sort
 Assume we know something about the list of numbers
we are sorting
 All numbers are integers
 We know the maximum number
 We 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 sort
 Place integer in corresponding bucket
 Note: 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 buckets
 What is the complexity of this?
Complexity of Bucket Sort
 One 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?
def myFun(myList):
n = len(myList)
i=1
while ( i<n):
myList[i] = i
i = i*2
return myList
A: 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
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
>>> trickyReturns([0,1,3,4,5])
return k
1
>>> trickyReturns([1001,1,3,4,5])
0
>>> trickyReturns([0,1,1001,3,4,5])
1
What is the complexity?
A: O(n)
B: O(n2)
C: O(1)
D: O(log n)
def trickyReturns(list):
k=0
for w in range(len(list)):
if(list[w] == 1001):
return w
else:
k=k+1
return k
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 condition
 Then think about the problem in terms of a supply chain
 What 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 games
 http://pygame.org/news.html
 http://rene.f0o.com/mywiki/PythonGameProgramming
Lego Mindstorms
 Program your robots with Python
 http://code.google.com/p/nxt-python/
Professional Python Use
 Bio Informatics
 http://shop.oreilly.com/product/9780596154516.do
 Numpy / Scipy
 http://numpy.scipy.org/
Final
 35 Questions
 Multiple Choice
 Same format as midterms
 Do not be surprised to find questions similar to the
most frequently missed questions on your two
midterms
Book Chapters
 Chapters 4-8
 Chapters 11 and 13
Identify the term that has the
largest growth rate
Num of steps
growth term
complexity
 6n + 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 slowest
 O(1) – constant time
 O(log n) – logarithmic time
 O(n) – linear time
 O(n log n) – log linear time
 O(n2) – quadratic time
 O(2n) – exponential time
 O(n!) – factorial time
Searching
 Linear Search
 Complexity O(n)
 Why: scan each element
 Binary Search
 Complexity O(log n)
 Why: break list into two pieces, discard one piece
Sorting
 Bubble 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
swaps
 Merge 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
Sorting
 Simplified bucket sort:
 Complexity O(n)
 Why: can each element and place it in a bucket, then glue
all buckets together
 Assumptions: 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 list
 In 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)
Conditionals
def 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
Homework
 Study for the final
Download