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

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
EECS 110: Lec 14: Classes and Objects Aleksandar Kuzmanovic Northwestern University
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
EECS 110: Lec 11: Indefinite Loops and Program Design Aleksandar Kuzmanovic Northwestern University
Today’s whether: if, elif, or else!
HW 3: Problems 2&3. HW 3 Prob 2:Encipher encipher( S, n ) takes as input a string S and a non-negative integer n between 0 and 25. This function returns.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
The If/Else Statement, Boolean Flags, and Menus Page 180
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
Haskell Chapter 3, Part I. Pattern Matching  Pattern matching with tuples  Pattern matching with list comprehensions  As-patterns.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
Now String theory To Do: Lists This stuff hurts my brane. when you learn string theory from google images… Goal: Thinking like a machine You should now.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #005 (April somthin, 2015)
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
EECS 110: Lec 5: List Comprehensions Aleksandar Kuzmanovic Northwestern University
The building blocks of functional computing data, sequences conditionals recursion CS 121 today List Comprehensions map and applications.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
The last CS lecture you’ll ever need! On Warner Brothers' insistence, we affirm that this 'C' does not stand for 'Chamber' and 'S' does not stand for 'Secrets.'
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
12/9/2010 Course A201: Introduction to Programming.
Last Week if statement print statement input builtin function strings and methods for loop.
EECS 110: Lec 4: Functions and Recursion Aleksandar Kuzmanovic Northwestern University
IS 313 Tomorrow… IS 313 Today? 9/16/09 - today: recursion and beyond! 9/23/09 - next wk: no meeting (DC) 9/30/09 - following wk: for & while Homework functions.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Comprehending List Comprehensions
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
The last CS lecture you’ll ever need! On Warner Brothers' insistence, we affirm that this 'C' does not stand for 'Chamber' and 'S' does not stand for 'Secrets.'
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 17: Review for the Final Exam Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 7: Program Planning Aleksandar Kuzmanovic Northwestern University
The last CS 5 lecture you’ll ever need! Inducing labor for the machine! == Reducing labor for humans! On Warner Brothers' insistence, we affirm that this.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 3: Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
Python Files and Lists. Files  Chapter 9 actually introduces you to opening up files for reading  Chapter 14 has more on file I/O  Python can read.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Find LCM Least Common Multiple of 3 and 5: List the Multiples of each number, The multiples of 3 are 3, 6, 9, 12, 15, 18,... etc The multiples of 5 are.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
EECS 110: Lec 8: Lists of Lists
EECS 110: Lec 17: Review for the Final Exam
EECS 110: Lec 9: Review for the Midterm Exam
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
Aleksandar Kuzmanovic Northwestern University
Strings Part 1 Taken from notes by Dr. Neil Moore
EECS 110: Lec 4: Functions and Recursion
Introduction to Strings
EECS 110: Lec 10: Definite Loops and User Input
CHAPTER THREE Sequences.
Data types Numeric types Sequence types float int bool list str
String and Lists Dr. José M. Reyes Álamo.
EECS 110: Lec 4: Functions and Recursion
Spring 2015.
Introduction to Strings
CS1110 Today: collections.
Python Review
More 2D Array and Loop Examples Functions and Parameters
Class code for pythonroom.com cchsp2cs
Recursion Exercises.
def-ining a function A function as an execution control structure
Problem to solve Given four numbers, days, hours, minutes, and seconds, compute the total number of seconds that is equivalent to them. For example, 0.
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

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!