1 / 45

Announcements

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 8 th at 11:59pm. Final. 35 Questions Multiple Choice Same format as midterms

tait
Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. Final – unique questions 3questions complexity 3 questions recursion 2 questions search 2 questions sorting

  4. 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

  5. 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, +=

  6. 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

  7. 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?

  8. 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!)

  9. 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

  10. 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

  11. 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

  12. 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?

  13. 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)

  14. What does this code do? defmyFun(myList): n = len(myList) i= 1 while( i<n): myList[i] = i i= i*2 returnmyList

  15. What does this code do? defmyFun(myList): n = len(myList) i= 1 while( i<n): myList[i] = i i= i*2 returnmyList >>> 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]

  16. What is the complexity? defmyFun(myList): n = len(myList) i= 1 while( i<n): myList[i] = i i= i*2 returnmyList 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

  17. What does this code do? deftrickyReturns(list): k = 0 forw in range(len(list)): if(list[w] == 1001): returnw else: k=k+1 return k

  18. What does this code do? deftrickyReturns(list): k = 0 forw in range(len(list)): if(list[w] == 1001): returnw 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

  19. What is the complexity? deftrickyReturns(list): k = 0 forw in range(len(list)): if(list[w] == 1001): returnw else: k=k+1 return k 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

  20. 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

  21. What does this code do? def mystery(x): if x == 1: return 1 else: return x * mystery(x-1)

  22. What does this do? def mystery(x): return x + mystery(x-1)

  23. How do we fix it? def mystery(x): if x == 0: return 0 else: return x + mystery(x-1)

  24. Tracing the mystery function • mystery(5) • 5 + (mystery(4)) • 5 + (4 + (mystery(3))) • 5 + (4 + (3 + (mystery(2)))) • …. • Why are the parenthesis important?

  25. What if we had this function? def mystery(x): if x == 0: return 0 else: return x - mystery(x-1)

  26. 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 >>>

  27. Fun things to do with Python • Build video games • http://pygame.org/news.html • http://rene.f0o.com/mywiki/PythonGameProgramming

  28. Lego Mindstorms • Program your robots with Python • http://code.google.com/p/nxt-python/

  29. Professional Python Use • Bio Informatics • http://shop.oreilly.com/product/9780596154516.do • Numpy / Scipy • http://numpy.scipy.org/

  30. 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

  31. Book Chapters • Chapters 4-8 • Chapters 11 and 13

  32. 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 2nO(2n) • n! + 2n10 + 2n + 3 n! O(n!)

  33. 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

  34. 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

  35. 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

  36. 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)

  37. 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?

  38. 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?

  39. What does this do? def t4(c): x = {} for iin range(128): x[chr(i)] = i return x[c]

  40. What is the output of the following code? list = ['A',1,'B',2,'C',3,'D',4] myDict= {} foriin range(0,len(list),2): myDict[list[i]] = list[i+1]

  41. What is the output of the following code? list = ['A',1,'B',2,'C',3,'D',4] myDict= {} foriin range(len(list)-1): myDict[list[i]] = list[i+1]

  42. 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)

  43. Conditionals defsel(mylist): for i in range(len(mylist)): if mylist[i] % 2: print(i)

  44. Conditionals def t1(x,y,z): ifx and y or z: x = y ifx and not z: print("goal") else: x = z else: z = y

  45. Homework • Study for the final

More Related