970 likes | 987 Views
Review for the EECS 110 Python Midterm Exam including Python syntax, functions, lists, and string manipulations. Prepare with practice questions and exam guidelines.
E N D
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 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.
Midterm Exam • 6 questions: • What Python would say? • Functions • Recursion • List comprehensions • Lists of lists • Bugs
Question 1: What Python Would Say? >> x = 41 >> y = x + 1 >> x ??
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
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
% 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
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
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
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
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
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
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'
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'
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?
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?
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?
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'
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'
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'
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'
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]
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
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:
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#
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) ?
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) ?
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) ?
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) ?
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
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
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) ?
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
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) ?
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) ?
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) ?
Question 3: Recursion The recursion mantra:
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!
Example 1: Designing numAs How many A’s are in a particular string? antarctica gattaca Base Case: Recursive Step:
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…
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:
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:
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:
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 ___________