EECS 110 Lec 9 Review for the Midterm

  • Slides: 97
Download presentation
EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University http:

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

General Info Wednesday, April 29, 10 -11: 30 am, Annenberg Hall G 15 To

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

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

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

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

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

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

What Python Would Say? >> >> >> 41 >> ? ? x = 41

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

What Python Would Say? >> >> >> 41 >> 42 x = 41 y

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

What Python Would Say? >> >> >> 41 >> 42 >> >> ? ?

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

What Python Would Say? >> >> >> 41 >> 42 >> >> 83 x

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

What Python Would Say? >> >> >> 41 >> 42 >> >> 83 >>

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

What Python Would Say? >> >> >> 41 >> 42 >> >> 83 >>

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

Python (numeric) data types Dominant float long int bool Recessive What will these results

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

% the “mod” operator x%y returns the remainder when x is divided by y

% 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 len + * str(42) returns '42' converts input to a string

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

String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7

String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7 8 9 s[ ] 10 11 12 13 14 15 16 17 18 19 20 21 22 indexes into 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

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

String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7

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

String surgery s = ’northwestern university' 0 1 2 3 4 5 6 7

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

Slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8

Slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 s[ : ] 10 11 12 13 14 15 16 17 18 19 20 21 22 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

Slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 s[ : ] 10 11 12 13 14 15 16 17 18 19 20 21 22 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 s[0: 5] returns 'north' a missing index means the end of the string 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

Skip-slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 s[ : : ] 10 11 12 13 14 15 16 17 18 19 20 21 22 skip-slices, returning a subsequence 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' 0 1 2 3 4 5 6 7 8

Skip-slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 s[ : : ] 10 11 12 13 14 15 16 17 18 19 20 21 22 skip-slices, returning a subsequence 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] s[10: 17: 3]

Skip-slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8

Skip-slicing s = ‘northwestern university' 0 1 2 3 4 5 6 7 8 9 s[ : : ] 10 11 12 13 14 15 16 17 18 19 20 21 22 skip-slices, returning a subsequence 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 Commas separate elements. L = [ 3. 14, [2,

Lists ~ Strings of anything Commas separate elements. 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'

Lists ~ Strings of anything Commas separate elements. L = [ 3. 14, [2,

Lists ~ Strings of anything Commas separate elements. 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'

Lists ~ Strings of anything Commas separate elements. L = [ 3. 14, [2,

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

Lists ~ Strings of anything Commas separate elements. L = [ 3. 14, [2,

Lists ~ Strings of anything Commas separate elements. L = [ 3. 14, [2, 40], 'third', 42 ] Square brackets tell python you want a list. len(L) L[0] 4 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'

Lists ~ Strings of anything Commas separate elements. L = [ 3. 14, [2,

Lists ~ Strings of anything Commas separate elements. L = [ 3. 14, [2, 40], 'third', 42 ] Square brackets tell python you want a list. len(L) L[0] 4 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 >>>

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 >>> 42 in [ [42], '42' ] False a little bit different for lists…

Question 2: Functioning in Python Some basic, built-in functions: abs bool absolute value float

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

Functioning in Python # my own function! def dbl( x ): """ returns double

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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) def f(x): return 11*g(x) +

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) ? demo x = -4 return -4 + f(-4)

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) + g(x/2) demo x = -4 return -4 + f(-4) f def g(x): return -1 * x x = -4 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) def f(x): return 11*g(x) +

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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

How functions work… def demo(x): return x + f(x) def f(x): return 11*g(x) +

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

Question 3: Recursion The recursion mantra:

Question 3: Recursion The recursion mantra:

Recursion The recursion mantra: def fac(N): You handle the base case – the easiest

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

Example 1: Designing num. As How many A’s are in a particular string? antarctica

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

num. As How many A’s are in a particular string? antarctica gattaca Base Case:

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

num. As How many A’s are in a particular string? antarctica gattaca Base Case:

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

num. As How many A’s are in a particular string? antarctica gattaca Base Case:

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

num. As How many A’s are in a particular string? antarctica gattaca Base Case:

num. As How many A’s are in a particular string? antarctica gattaca 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

num. As How many A’s are in a particular string? antarctica gattaca Base Case:

num. As How many A’s are in a particular string? antarctica gattaca 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 ______

num. As How many A’s are in a particular string? antarctica gattaca Base Case:

num. As How many A’s are in a particular string? antarctica gattaca 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

num. As def num. As( s ): if len(s) == 0: return 0 elif

num. As def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(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

num. As def num. As( s ): if len(s) == 0: return 0 elif

num. As def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(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

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca')

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

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

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

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

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca') num. As('attaca') 1+num. As('taca')

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca') num. As('attaca') 1+num. As('taca') 1+num. As('aca')

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca') num. As('attaca') 1+num. As('taca') 1+num. As('aca') 1+1+num. As('ca')

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca') num. As('attaca') 1+num. As('taca') 1+num. As('aca') 1+1+num. As('a')

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca') num. As('attaca') 1+num. As('taca') 1+num. As('aca') 1+1+num. As('a') 1+1+1+num. As('')

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca') num. As('attaca') 1+num. As('taca') 1+num. As('aca') 1+1+num. As('a') 1+1+1+num. As('') 1+1+1+0

def num. As( s ): if len(s) == 0: return 0 elif s[0] !=

def num. As( s ): if len(s) == 0: return 0 elif s[0] != 'a': return num. As(s[1: ]) elif s[0] == 'a': return 1+num. As(s[1: ]) num. As('gattaca') num. As('attaca') 1+num. As('taca') 1+num. As('aca') 1+1+num. As('a') 1+1+1+num. As('') 1+1+1+0 Done! 3

Example 2: Taking away a letter def remove. A( s ) returns a string

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

Taking away… def remove. A( s ) Base Case: returns a string that is

Taking away… def remove. A( s ) Base Case: returns a string that is the same as s, but without the first 'a' when there are no letters, the answer is _______ if the first letter IS an A, the answer is ___________ Recursive Step: if the first letter IS an A, the answer is _________

Taking away… def remove. A( s ) Base Case: returns a string that is

Taking away… def remove. A( s ) Base Case: returns a string that is the same as s, but without the first 'a' 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: ] Recursive Step: if the first letter is NOT an A, the answer is s[0] + WHAT YOU GET WHEN YOU TAKE As OUT OF s[1: ]

def remove. A( s ): if len(s) == 0: return ' ' elif s[0]

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

You can try these (using recursion) def num. Bs( s ): Returns the number

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

You can try these (using recursion) def num. Bs( s ): Returns the number

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

Question 4: List Comprehensions (1) def num( ch, s ): Returns the number of

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

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’)

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

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

List Comprehensions (Ex. 3) >> S = [x**2 for x in range(5)] >> V

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

List Comprehensions (Ex. 3) >> S = [x**2 for x in range(5)] >> V

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

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

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

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

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]

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

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,

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,

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,

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,

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,

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,

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,

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

Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = get. Pixels(pic) if len(pixels) == 0: return new. Pixels = [ [set. New. Pixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] set. Pixels(pic, new. Pixels) def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW imanges (row, col) (r, g, b) value input pixels: a 2 D 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, 100), (3, 110)], [(3, 10,

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

Tuples vs. Lists [ [(3, 100), (3, 110)], [(3, 10, 200), (10, 110, 290)]

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

Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter

Brightening a Picture def modify(pic): """ modify modifies an image to make it brighter """ pixels = get. Pixels(pic) if len(pixels) == 0: return new. Pixels = [ [set. New. Pixel( pixels, row, col ) for col in range(len(pixels[0]))] for row in range(len(pixels))] set. Pixels(pic, new. Pixels) def set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW imanges (row, col) (r, g, b) value input pixels: a 2 D 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)

"Quiz“ It's all clear to me now! Name(s): Write a function that tints the

"Quiz“ It's all clear to me now! Name(s): Write a function that tints the top half of the picture red (how red is up to you): def set. New. Pixel( pixels, row, col ): """ set. New. Pixel 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 set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ Want more? How would you turn only the sky red?

"Quiz“ It's all clear to me now! Name(s): Write a function that tints the

"Quiz“ It's all clear to me now! Name(s): Write a function that tints the top half of the picture red (how red is up to you): def set. New. Pixel( pixels, row, col ): """ set. New. Pixel 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 set. New. Pixel( pixels, row, col ): """ set. New. Pixel returns the NEW image's (row, col) (r, g, b) value """ Want more? How would you turn only the sky red?

"Quiz“ It's all clear to me now! Name(s): Write a function that tints the

"Quiz“ It's all clear to me now! Name(s): Write a function that tints the top half of the picture red (how red is up to you): def set. New. Pixel( pixels, row, col ): """ set. New. Pixel 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 set. New. Pixel( pixels, row, col ): """ set. New. Pixel 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] Want more? How would you turn only the sky red?

Question 6: Bugs

Question 6: Bugs

return vs. print def dbl(x): """ doubles x """ return 2*x def dbl. PR(x):

return vs. print def dbl(x): """ doubles x """ return 2*x def dbl. PR(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!

Good luck with the Midterm Exam!