1 / 97

EECS 110: Lec 9: Review for the Midterm Exam

EECS 110: Lec 9: Review for the Midterm Exam. Aleksandar Kuzmanovic Northwestern University. http://cs.northwestern.edu/~akuzma/classes/EECS110-s10/. General Info. Wednesday, April 28, 9-10:30am, Tech Auditorium (Tech L165, Ryan Family Auditorium) To be done individually Closed book

marthaadams
Télécharger la présentation

EECS 110: Lec 9: Review for the Midterm Exam

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. EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University http://cs.northwestern.edu/~akuzma/classes/EECS110-s10/

  2. General Info • Wednesday, April 28, 9-10:30am, Tech Auditorium (Tech L165, Ryan Family Auditorium) To be done individually Closed book One 8.5” by 11” sheet of paper permitted • Please do not discuss the exam with others until everyone has taken it. • There are six questions. Each question is worth 20 points. • Hence, you can acquire 120 points in total. • Those who get > 90 points will get an A. • Those who get >80 points and <90 points will get a B, etc.  

  3. Midterm Exam • 6 questions: • What Python would say? • Functions • Recursion • List comprehensions • Lists of lists • Bugs

  4. Question 1: What Python Would Say? >> x = 41 >> y = x + 1 >> x ??

  5. What Python Would Say? >> x = 41 >> y = x + 1 >> x 41

  6. What Python Would Say? >> x = 41 >> y = x + 1 >> x 41 >> y ??

  7. What Python Would Say? >> x = 41 >> y = x + 1 >> x 41 >> y 42

  8. What Python Would Say? >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ??

  9. What Python Would Say? >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x 83

  10. What Python Would Say? >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x 83 >> y ??

  11. What Python Would Say? >> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x 83 >> y 42

  12. Python (numeric) data types What will these results be? Dominant 1.0 / 5 float long 10**100 - 10**100 1 / 5 int bool 41 + True Recessive

  13. % the “mod” operator x%y returns the remainder when x is divided by y 7 % 3 8 % 3 9 % 3 16 % 7 x%2 == 0 For what values of x are these True? x%2 == 1 x%4 == 0

  14. string functions str(42)returns'42' converts input to a string str len + * len('42')returns2 returns the string’s length 'XL' + 'II'returns 'XLII' concatenates strings 'VI'*7returns 'VIVIVIVIVIVIVI' repeats strings

  15. String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns S[ ]returns'h' Which index returns 'e'? s[len(s)]returns python!=English

  16. String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns ' ' S[ ]returns'h' Which index returns 'e'? s[len(s)]returns python!=English

  17. String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns ' ' S[4]returns'h' Which index returns 'e'? s[len(s)]returns python!=English

  18. String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] indexesinto the string, returning a one-character string index s[0]returns 'n' Read "s-of-zero" or "s-zero" s[12]returns ' ' S[4]returns'h' Which index returns 'e'? s[len(s)]returns ERROR python!=English

  19. Slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : ] slices the string, returning a substring What's going on here? s[0:5]returns'north' s[5:9]returns'west' s[17:]returns'ersity' s[:]returns'northwestern university'

  20. Slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : ] slices the string, returning a substring the first index is the first character of the slice the second index is ONE AFTER the last character a missing index means the end of the string s[0:5]returns'north' s[5:9]returns'west' s[17:]returns'ersity' s[:]returns'northwestern university'

  21. Skip-slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2]returns'nrhe' 'ruv' What skip-slice returns s[1::6] What does this return?

  22. Skip-slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2]returns'nrhe' 'ruv' s[10:17:3] What skip-slice returns s[1::6] What does this return?

  23. Skip-slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2]returns'nrhe' 'ruv' s[10:17:3] What skip-slice returns s[1::6] 'osus' What does this return?

  24. Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) L[0] Indexing: could return a different type Slicing: always returns the same type L[0:1] How could you extract from L 'hi'

  25. Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] Indexing: could return a different type Slicing: always returns the same type L[0:1] How could you extract from L 'hi'

  26. Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] 3.14 Indexing: could return a different type Slicing: always returns the same type L[0:1] How could you extract from L 'hi'

  27. Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] 3.14 Indexing: could return a different type Slicing: always returns the same type L[0:1] [3.14] How could you extract from L 'hi'

  28. Commas separate elements. Lists ~ Strings of anything L = [ 3.14, [2,40], 'third', 42 ] Square brackets tell python you want a list. len(L) 4 L[0] 3.14 Indexing: could return a different type Slicing: always returns the same type L[0:1] [3.14] How could you extract from L 'hi' L[2][1:3]

  29. The in thing >>> 3*'i' in 'alien' False >>> 'i' in 'team' False >>> 'cs' in 'physics' True >>> ‘sleep' not in ‘EECS 110' True >>> 42 in [41,42,43] True a little bit different for lists… >>> 42 in [ [42], '42' ] False

  30. Question 2: Functioning in Python Some basic, built-in functions: bool float int long list str abs max min sum range round absolute value of lists these change data from one type to another creates lists only as accurately as it can! help dir The most important:

  31. Functioning in Python # my own function! def dbl( x ): """ returns double its input, x """ return 2*x keywords Some of Python's baggage… defstarts the function returnstops it immediately and sends back the return value Docstrings They become part of python's built-in help system! With each function be sure to include one that Comments describes overall what the function does, and explains what the inputs mean/are They begin with#

  32. How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x What is demo(-4) ?

  33. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) def g(x): return -1 * x What is demo(-4) ?

  34. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x/2) What is demo(-4) ?

  35. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(x) + g(x/2) These are different x's ! What is demo(-4) ?

  36. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11*g(-4) + g(-4/2) g x = -4 What is demo(-4) ? return -1.0 * x

  37. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11* 4 + g(-4/2) g x = -4 What is demo(-4) ? return -1 * -4 4

  38. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x return 11* 4 + g(-4/2) What is demo(-4) ?

  39. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1 * x return 11* 4 + g(-4/2) g x = -2 What is demo(-4) ? return -1 * -2 2

  40. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x return 11* 4 + 2 What is demo(-4) ?

  41. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + f(-4) def f(x): return 11*g(x) + g(x/2) f x = -4 def g(x): return -1.0 * x 46 return 11* 4 + 2 What is demo(-4) ?

  42. How functions work… def demo(x): return x + f(x) demo x = -4 return -4 + 46 def f(x): return 11*g(x) + g(x/2) def g(x): return -1.0 * x 42 What is demo(-4) ?

  43. Question 3: Recursion The recursion mantra:

  44. Recursion The recursion mantra: deffac(N): if N <= 1: return 1 else: return N * fac(N-1) You handle the base case – the easiest possible case to think of! Recursion does almost all of the rest of the problem!

  45. Example 1: Designing numAs How many A’s are in a particular string? antarctica gattaca Base Case: Recursive Step:

  46. numAs How many A’s are in a particular string? antarctica gattaca Think about the SIMPLEST POSSIBLE case! Base Case: Recursive Step: Do ONLY ONE STEP, and let recursion do the rest…

  47. numAs How many A’s are in a particular string? antarctica gattaca when there are no letters, there are ZEROA’s Base Case: Recursive Step:

  48. numAs How many A’s are in a particular string? antarctica gattaca when there are no letters, there are ZEROA’s Base Case: if the first letter is NOT an A, the answer is just ___________ Recursive Step:

  49. numAs How many A’s are in a particular string? antarctica gattaca when there are no letters, there are ZEROA’s Base Case: if the first letter is NOT an A, the answer is just the number of A’s in the rest of the string Recursive Step:

  50. numAs How many A’s are in a particular string? antarctica gattaca when there are no letters, there are ZEROA’s Base Case: if the first letter is NOT an A, the answer is just the number of A’s in the rest of the string Recursive Step: if the first letter IS an A, the answer is just ___________

More Related