EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University
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
Midterm Exam 6 questions: 1.What Python would say? 2.Functions 3.Recursion 4.List comprehensions 5.Lists of lists 6.Bugs 3
>> x = 41 >> y = x + 1 >> x ?? Question 1: What Python Would Say?
>> x = 41 >> y = x + 1 >> x 41 What Python Would Say?
>> x = 41 >> y = x + 1 >> x 41 >> y ?? What Python Would Say?
>> x = 41 >> y = x + 1 >> x 41 >> y 42 What Python Would Say?
>> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x ?? What Python Would Say?
>> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x 83 What Python Would Say?
>> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x 83 >> y ?? What Python Would Say?
>> x = 41 >> y = x + 1 >> x 41 >> y 42 >> x = x + y >> x 83 >> y 42 What Python Would Say?
bool Dominant int long float Recessive 41 + True 10** ** / 5 1 / 5 What will these results be? Python (numeric) data types
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
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
s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns S[ ] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery
s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns ' ' S[ ] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery
s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns ' ' S[4] returns 'h' Which index returns 'e'? python != English s[len(s)] returns Read "s-of-zero" or "s-zero" index String surgery
s[ ] indexes into the string, returning a one-character string s = ’northwestern university' s[0] returns 'n' s[12] returns ' ' S[4] returns 'h' Which index returns 'e'? python != English s[len(s)] returnsERROR Read "s-of-zero" or "s-zero" index String surgery
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'
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' 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
Skip-slicing s = ‘northwestern university' 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]
Skip-slicing s = ‘northwestern university' 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]
Skip-slicing s = ‘northwestern university' 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'
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.
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.
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
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
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
>>> 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…
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
# 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
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
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
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
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 !
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
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
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)
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
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
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*
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
Question 3: Recursion The recursion mantra:
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!
Example 1: Designing numAs How many A ’s are in a particular string? Base Case: Recursive Step: antarctica gattaca
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
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
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
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
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
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
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
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
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')
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')
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')
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')
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')
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')
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')
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') numAs('')
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') numAs('')
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') numAs('') Done!
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
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'
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 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:])
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.
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.
Question 4: List Comprehensions (1) def num( ch, s ): Returns the number of ch characters in the string s.
List Comprehensions (Ex. 1) def num( ch, s ): LC = [c in ch for c in s] return sum( LC )
List Comprehensions (Ex. 2) def sajak( s ): Returns the number of vowels (‘auioe’) in the string s.
List Comprehensions (Ex. 2) def sajak( s ): LC = [c in 'auioe' for c in s] return sum( LC )
>> 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)
>> 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)
>> 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)
>> 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)
>> 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)
>> 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)
Question 5: Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]]
Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] ??
Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1
Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] ??
Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] 9
Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] >>> S[0][0] 1 >>>S[2][2] 9 >>>S[3][3] ??
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
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
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)
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
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
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
"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 """
"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 """
"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]
Question 6: Bugs
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
Good luck with the Midterm Exam!