EECS 110: Lec 9: Review for the Midterm Exam

Slides:



Advertisements
Similar presentations
Python Mini-Course University of Oklahoma Department of Psychology Day 4 – Lesson 15 Tuples 5/02/09 Python Mini-Course: Day 4 – Lesson 15 1.
Advertisements

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.
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.
The If/Else Statement, Boolean Flags, and Menus Page 180
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.
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
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.'
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 9: Review for the Midterm Exam 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 3: Data Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 8: Lists of Lists Aleksandar Kuzmanovic Northwestern University
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
String and Lists Dr. José M. Reyes Álamo.
Haskell: Syntax in Functions
EECS 110: Lec 17: Review for the Final Exam
EECS 110: Lec 5: List Comprehensions
EECS 110: Lec 5: List Comprehensions
Aleksandar Kuzmanovic Northwestern University
EECS 110: Lec 7: Program Planning
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Functions CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 14 – For Loops
EECS 110: Lec 4: Functions and Recursion
The CS 5 Black Post Penguins Invade Dormitory
CMSC201 Computer Science I for Majors Lecture 15 – For Loops
Introduction to Strings
More Loop Examples Functions and Parameters
EECS 110: Lec 10: Definite Loops and User Input
CHAPTER THREE Sequences.
Data types Numeric types Sequence types float int bool list str
Intro to Computer Science CS1510 Dr. Sarah Diesburg
String and Lists Dr. José M. Reyes Álamo.
EECS 110: Lec 4: Functions and Recursion
Spring 2015.
Introduction to Strings
CS1110 Today: collections.
CISC101 Reminders Assignment 2 due today.
Introduction to Computer Science
Python Review
Types, Truth, and Expressions
EECS 110: Lec 12: Mutable Data
List Comprehensions Problem: given a list of prices, generate a new list that has a 20% discount to each. Formally: input: list of old prices; output:
More 2D Array and Loop Examples Functions and Parameters
Recursion Review Spring 2019 CS 1110
Class code for pythonroom.com cchsp2cs
Recursion Exercises.
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
Introduction to Computer Science
Presentation transcript:

EECS 110: Lec 9: Review for the Midterm Exam Aleksandar Kuzmanovic Northwestern University http://networks.cs.northwestern.edu/EECS110-s17/

General Info Wednesday, April 26, 9:30-11:30am, Pancoe Life Science Pavilion (PLSAUD) 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

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

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

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

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

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

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

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

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 len('42') returns 2 returns the string’s length 'XL' + 'II' returns 'XLII' concatenates strings 'VI'*7 returns 'VIVIVIVIVIVIVI' repeats strings

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

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

s = ’northwestern university' String surgery s = ’northwestern university' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] 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[4] returns 'h' Which index returns 'e'? s[len(s)] returns python != English

s = ’northwestern university' String surgery s = ’northwestern university' 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 s[ ] 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[4] returns 'h' Which index returns 'e'? s[len(s)] returns ERROR python != English

s = ‘northwestern university' Slicing s = ‘northwestern university' 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'

s = ‘northwestern university' Slicing s = ‘northwestern university' 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'

s = ‘northwestern university' Skip-slicing s = ‘northwestern university' 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 What does this return? s[1::6]

s = ‘northwestern university' Skip-slicing s = ‘northwestern university' 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 What does this return? s[1::6]

s = ‘northwestern university' Skip-slicing s = ‘northwestern university' 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 What does this return? s[1::6] 'osus'

Lists ~ Strings of anything 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'

Lists ~ Strings of anything 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'

Lists ~ Strings of anything 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'

Lists ~ Strings of anything 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'

Lists ~ Strings of anything 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]

>>> 3*'i' in 'alien' 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

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

Functioning in Python Some of Python's baggage… # 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 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… What is demo(-4) ? 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… What is demo(-4) ? def demo(x): demo 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… What is demo(-4) ? def demo(x): demo 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… These are different x's ! What is demo(-4) ? 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… What is demo(-4) ? def demo(x): demo 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… What is demo(-4) ? def demo(x): return x + f(x) 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… What is demo(-4) ? def demo(x): demo 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… What is demo(-4) ? def demo(x): return x + f(x) 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… What is demo(-4) ? def demo(x): demo 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… What is demo(-4) ? def demo(x): demo 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 46 What is demo(-4) ?

42 How functions work… What is demo(-4) ? def demo(x): demo 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 What is demo(-4) ? 42

Question 3: Recursion The recursion mantra:

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

How many A’s are in a particular string? 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…

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

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

when there are no letters, there are ZERO A’s numAs How many A’s are in a particular string? antarctica gattaca when there are no letters, there are ZERO A’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 ZERO A’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 ___________

when there are no letters, there are ZERO A’s numAs How many A’s are in a particular string? antarctica gattaca when there are no letters, there are ZERO A’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 1 + the number of A’s in the rest of the string

when there are no letters, there are ZERO A’s 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:]) when there are no letters, there are ZERO A’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 1 + the number of A’s in the rest of the string

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') 1+1+1+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') 1+1+1+numAs('') 1+1+1+0

elif s[0] != 'a': return numAs(s[1:]) 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 Done! 3

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

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

when there are no letters, the answer is the EMPTY STRING '' Taking away… returns a string that is the same as s, but without the first 'a' def removeA( s ) when there are no letters, the answer is the EMPTY STRING '' Base Case: 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 removeA( s ): if len(s) == 0: return ' ' elif s[0] == 'a': return s[1:] elif s[0] != 'a': return s[0]+removeA(s[1:])

You can try these (using recursion) def numBs( 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( s2, s ): Returns the number of characters in s2 that are in the string s.

You can try these (using recursion) def numBs( 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( s2, s ): Returns the number of characters in s2 that are in the string s.

Returns the number of ch characters 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 )

Returns the number of vowels (‘auioe’) in the string s. 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 )

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 = [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]

Question 5: Lists of Lists

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]]

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]]

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]] ERROR

Lists of Lists >> S = [[1,2,3],[4,5,6],[7,8,9]] 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 [ [(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 Tuples use ( ); lists use [ ] [ [(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

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)

"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 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 """ 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 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 """ 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 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] Want more? How would you turn only the sky red?

Question 6: Bugs

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

Good luck with the Midterm Exam!