Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University

Similar presentations


Presentation on theme: "EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University"— Presentation transcript:

1 EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University http://cs.northwestern.edu/~akuzma/classes/EECS110-s09/

2 General Info Wednesday, April 29, 10-11:30am, Annenberg Hall G15 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. 2

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

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

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

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

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

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

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

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

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

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

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

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

15 s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns 0123456789 101112131415161718 S[ ] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

16 s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns ' ' 0123456789 101112131415161718 S[ ] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

17 s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns ' ' 0123456789 101112131415161718 S[4] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

18 s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns ' ' 0123456789 101112131415161718 S[4] returns 'h' Which index returns 'e'? python != English s[len(s)] returnsERROR Read "s-of-zero" or "s-zero" index String surgery 19 2021 22

19 s[ : ] slices the string, returning a substring s[5:9] returns 'west' s[0:5] returns 'north' What's going on here? s[17:] returns 'ersity' s[:] returns 'northwestern university' Slicing s = ‘northwestern university' 0123456789 10111213141516171819 2021 22

20 s[ : ] slices the string, returning a substring s[5:9] returns 'west' s[0:5] returns 'north' s[17:] returns 'ersity' s[:] returns 'northwestern university' Slicing s = ‘northwestern university' 0123456789 10111213141516171819 2021 22 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

21 Skip-slicing s = ‘northwestern university' 0123456789 10111213141516171819 2021 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2] returns 'nrhe' What skip-slice returns What does this return? 'ruv' s[1::6]

22 Skip-slicing s = ‘northwestern university' 0123456789 10111213141516171819 2021 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2] returns 'nrhe' What skip-slice returns What does this return? 'ruv' s[10:17:3] s[1::6]

23 Skip-slicing s = ‘northwestern university' 0123456789 10111213141516171819 2021 22 s[ : : ] skip-slices, returning a subsequence the third index is the "stride" length it defaults to 1 s[0:8:2] returns 'nrhe' What skip-slice returns What does this return? 'ruv' s[10:17:3] s[1::6] 'osus'

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

43 Question 3: Recursion The recursion mantra:

44 Recursion The recursion mantra: def fac(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? Base Case: Recursive Step: antarctica gattaca

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

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

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

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

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

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

52 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) Base Case: Recursive Step: when there are no letters, there are ZERO A’s if the first letter is NOT an A, the answer is just the number of A’s in the rest of the string if the first letter IS an A, the answer is just 1 + the number of A’s in the rest of the string numAs

53 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) Base Case: Recursive Step: when there are no letters, there are ZERO A’s if the first letter is NOT an A, the answer is just the number of A’s in the rest of the string if the first letter IS an A, the answer is just 1 + the number of A’s in the rest of the string numAs

54 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca')

55 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca')

56 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca')

57 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca') 1+numAs('taca')

58 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca') 1+numAs('taca') 1+numAs('aca')

59 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca') 1+numAs('taca') 1+numAs('aca') 1+1+numAs('ca')

60 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca') 1+numAs('taca') 1+numAs('aca') 1+1+numAs('ca') 1+1+numAs('a')

61 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca') 1+numAs('taca') 1+numAs('aca') 1+1+numAs('ca') 1+1+numAs('a') 1+1+1+numAs('')

62 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca') 1+numAs('taca') 1+numAs('aca') 1+1+numAs('ca') 1+1+numAs('a') 1+1+1+numAs('') 1+1+1+0

63 def numAs( s ): if len(s) == 0: return 0 elif s[0] != 'a': return numAs(s[1:]) elif s[0] == 'a': return 1+numAs(s[1:]) numAs('gattaca') numAs('attaca') 1+numAs('ttaca') 1+numAs('taca') 1+numAs('aca') 1+1+numAs('ca') 1+1+numAs('a') 1+1+1+numAs('') 1+1+1+0 3 Done!

64 Example 2: Taking away a letter def removeA( s ) returns a string that is the same as s, but without the first 'a' removeA('chatham') returns removeAs('gattaca') returns removeAs('laura') returns removeAs('debbie') returns Examples: chtham gttaca lura debbie

65 Base Case: Recursive Step: when there are no letters, the answer is _____________ if the first letter IS an A, the answer is ______________________ if the first letter IS an A, the answer is __________________ Taking away… def removeA( s ) returns a string that is the same as s, but without the first 'a'

66 Base Case: Recursive Step: when there are no letters, the answer is the EMPTY STRING '' if the first letter IS an A, the answer is THE REST OF THE STRING s[1:] if the first letter is NOT an A, the answer is s[0] + WHAT YOU GET WHEN YOU TAKE As OUT OF s[1:] Taking away… def removeA( s ) returns a string that is the same as s, but without the first 'a'

67 67 def removeA( s ): if len(s) == 0: return ' ' elif s[0] == 'a': return s[1:] elif s[0] != 'a': return s[0]+removeA(s[1:])

68 def numBs( s ): def num( ch, s ): You can try these (using recursion) Returns the number of 'b' characters in the string s. Returns the number of ch characters in the string s. def match( s2, s ): Returns the number of characters in s2 that are in the string s.

69 def numBs( s ): def num( ch, s ): You can try these (using recursion) Returns the number of 'b' characters in the string s. Returns the number of ch characters in the string s. def match( s2, s ): Returns the number of characters in s2 that are in the string s.

70 Question 4: List Comprehensions (1) def num( ch, s ): Returns the number of ch characters in the string s.

71 List Comprehensions (Ex. 1) def num( ch, s ): LC = [c in ch for c in s] return sum( LC )

72 List Comprehensions (Ex. 2) def sajak( s ): Returns the number of vowels (‘auioe’) in the string s.

73 List Comprehensions (Ex. 2) def sajak( s ): LC = [c in 'auioe' for c in s] return sum( LC )

74 >> S = [x**2 for x in range(5)] >> V = [2**i for i in range(6)] >> M = [x for x in S if x%2 == 0] >> print S; ?? List Comprehensions (Ex. 3)

75 >> S = [x**2 for x in range(5)] >> V = [2**i for i in range(6)] >> M = [x for x in S if x%2 == 0] >> print S; [0, 1, 4, 9, 16] List Comprehensions (Ex. 3)

76 >> S = [x**2 for x in range(5)] >> V = [2**i for i in range(6)] >> M = [x for x in S if x%2 == 0] >> print S; print V; [0, 1, 4, 9, 16] ?? List Comprehensions (Ex. 3)

77 >> S = [x**2 for x in range(5)] >> V = [2**i for i in range(6)] >> M = [x for x in S if x%2 == 0] >> print S; print V; [0, 1, 4, 9, 16] [1, 2, 4, 8, 16, 32] List Comprehensions (Ex. 3)

78 >> S = [x**2 for x in range(5)] >> V = [2**i for i in range(6)] >> M = [x for x in S if x%2 == 0] >> print S; print V; print M [0, 1, 4, 9, 16] [1, 2, 4, 8, 16, 32] ?? List Comprehensions (Ex. 3)

79 >> S = [x**2 for x in range(5)] >> V = [2**i for i in range(6)] >> M = [x for x in S if x%2 == 0] >> print S; print V; print M [0, 1, 4, 9, 16] [1, 2, 4, 8, 16, 32] [0, 4, 16] List Comprehensions (Ex. 3)

80 Question 5: Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]]

81 Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] ??

82 Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1

83 Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] ??

84 Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] 9

85 Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] 9 >>>S[3][3] ??

86 Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] 9 >>>S[3][3] ERROR

87 Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] 9 >>>S[3][3] ERROR

88 Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels) def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D list containing RGB information in the pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval)

89 Representing the Pixels in a Picture pixels: [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] Width: len(pixels[0]) Height: len(pixels) 2x2 pixel image

90 Tuples vs. Lists [ [(3, 10, 100), (3, 11, 110)], [(3, 10, 200), (10, 110, 290)] ] Tuples use ( ); lists use [ ] But otherwise, they are the same… (for now, almost) >>> t = (1, 2, 3) >>> t[1] 2 >>> t[1:] (2, 3) >>> (x, y, z) = t >>> x 1 >>> y 2

91 def modify(pic): """ modify modifies an image to make it brighter """ pixels = getPixels(pic) if len(pixels) == 0: return newPixels = [ [setNewPixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] setPixels(pic, newPixels) def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW imanges (row, col) (r,g,b) value input pixels: a 2D list containing RGB information in the pixels in a picture input row: the row of the pixel in question input col: the column of the pixel in question """ rval= min(pixels[row][col][0]+30, 255) gval = min(pixels[row][col][1]+30, 255) bval = min(pixels[row][col][2]+30, 255) return (rval, gval, bval) Brightening a Picture

92 "Quiz“ Want more? How would you turn only the sky red? Name(s): It's all clear to me now! Write a function that tints the top half of the picture red (how red is up to you): def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """ Write a function that copies the top half of an image to the bottom half. def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """

93 "Quiz“ Want more? How would you turn only the sky red? Name(s): It's all clear to me now! Write a function that tints the top half of the picture red (how red is up to you): def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """ if row <= len(pixels)/2: rval = min(pixels[row][col][0]+75,255) else: rval = pixels[row][col][0] return (rval, pixels[row][col][1], pixels[row][col][2]) Write a function that copies the top half of an image to the bottom half. def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """

94 "Quiz“ Want more? How would you turn only the sky red? Name(s): It's all clear to me now! Write a function that tints the top half of the picture red (how red is up to you): def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """ if row <= len(pixels)/2: rval = min(pixels[row][col][0]+75,255) else: rval = pixels[row][col][0] return (rval, pixels[row][col][1], pixels[row][col][2]) Write a function that copies the top half of an image to the bottom half. def setNewPixel( pixels, row, col ): """ setNewPixel returns the NEW image's (row, col) (r,g,b) value """ if row > len(pixels)/2: return pixels[row-len(pixels)/2][col] else: return pixels[row][col]

95 Question 6: Bugs

96 return vs. print >>> answer = dbl(21) def dbl(x): """ doubles x """ return 2*x def dblPR(x): """ doubles x """ print 2*x >>> answer = dbl(21) return provides the function call's value … print just prints

97 Good luck with the Midterm Exam!


Download ppt "EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University"

Similar presentations


Ads by Google