Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8)...
Transcript of Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8)...
![Page 1: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/1.jpg)
Prelim 2 Review Part 1
Spring 2017
CS 1110
![Page 2: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/2.jpg)
Exam Info
• Prelim 2: 7:30–9:00PM, Tuesday, April 25th
aa200-jjm200 Baker Laboratory 200
jjm201 – sge200 Rockefeller 201
sge201 – zz200 Rockefeller 203
• No Electronics, Calculators, Notes, or Books
• Bring Your Cornell ID
• Name & NetId on Each Page
4/23/17 Prelim 2 Review Part 1 2
![Page 3: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/3.jpg)
What is on the Exam?
• The big topics
Nested Lists & Dictionaries (A3, Lab 8)
Recursion (A4, Lab 9)
Defining classes (Lab 10, Lab 11, A4)
Inheritance and subclasses (Lab 11)
Name Resolution
While Loops & Invariants
4/23/17 Prelim 2 Review Part 1 3
![Page 4: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/4.jpg)
What is on the Exam?
• The big topics
Nested Lists & Dictionaries (A3, Lab 8)
Recursion (A4, Lab 9)
Defining classes (Lab 10, Lab 11, A4)
Inheritance and subclasses (Lab 11)
Name Resolution
While Loops & Invariants
4/23/17 Prelim 2 Review Part 1 4
![Page 5: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/5.jpg)
What is on the Exam?
• The big topics
Nested Lists & Dictionaries (A3, Lab 8)
Recursion (A4, Lab 9)
Defining classes (Lab 10, Lab 11, A4)
Inheritance and subclasses (Lab 11)
Name Resolution
While Loops & Invariants
4/23/17 Prelim 2 Review Part 1 5
![Page 6: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/6.jpg)
4/23/17 Prelim 2 Review Part 1 6
class Customer(_______):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]"""
![Page 7: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/7.jpg)
4/23/17 Prelim 2 Review Part 1 7
class Customer(object):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]"""
def __init__(_______________):
"""Initialize a new Customer with name n, optional email e, and
no purchases or spending
Pre: n is a string, e is a string or None"""
Object = Not
a Subclass
![Page 8: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/8.jpg)
4/23/17 Prelim 2 Review Part 1 8
class Customer(object):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]""“
def __init__(self, n, e=None):
"""Initialize a new Customer with name n, optional email e, and
no purchases or spending
Pre: n is a string, e is a string or None"""
Optional
Attribute e
![Page 9: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/9.jpg)
4/23/17 Prelim 2 Review Part 1 9
class Customer(object):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]""“
def __init__(self, n, e=None):
"""Initialize a new Customer with name n, optional email e, and
no purchases or spending
Pre: n is a string, e is a string or None"""
self.name = n
self.email = e
self.purchases = 0
self.spent = 0.0Note: not everything
you need to initialize
is in the parameters!
![Page 10: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/10.jpg)
4/23/17 Prelim 2 Review Part 1 10
class Customer(object):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]""“
def __str__(_______):
""“Returns String Representation of this customer:
Name (email, if exists)”””
![Page 11: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/11.jpg)
4/23/17 Prelim 2 Review Part 1 11
class Customer(object):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]""“
def __str__(self):
""“Returns String Representation of this customer:
Name (email, if exists)”””
if self.email is None:
return self.name
else:
return self.name +'('+self.email+')'
![Page 12: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/12.jpg)
4/23/17 Prelim 2 Review Part 1 12
class Customer(object):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]"""
def makePurchase(_________):
""“Update customer after making a purchase of c dollars
Pre: c float >= 0.0 ”””
![Page 13: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/13.jpg)
4/23/17 Prelim 2 Review Part 1 13
class Customer(object):
"""Instance is a customer for our company
Attributes:
name: last name [string]
email: e-mail address [string or None if unknown]
purchases: number of items bought, [int >= 0]
spent: money spend at our company [float >= 0.0]"""
def makePurchase(self, c):
""“Update customer after making a purchase of c dollars
Pre: c float >= 0.0 ”””
self.purchases += 1;
self.spent += c;
![Page 14: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/14.jpg)
4/23/17 Prelim 2 Review Part 1 14
class PrefCustomer(_____________):
"""An instance is a 'preferred' customer, a Subclass of Customer.
Mutable attributes (in addition to Customer):
level: level of preference [One of 'bronze', 'silver', 'gold'] """
![Page 15: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/15.jpg)
4/23/17 Prelim 2 Review Part 1 15
class PrefCustomer(Customer):
"""An instance is a 'preferred' customer, a Subclass of Customer.
Mutable attributes (in addition to Customer):
level: level of preference [One of 'bronze', 'silver', 'gold'] """
def __init__(__________________):
"""Initialize a new PrefCustomer with name n, optional email e, and
no purchases or spending, and level l
Pre: n is a string, e is a string or None"""
Superclass in
the Header
We are
“overloading” the
initializer
![Page 16: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/16.jpg)
4/23/17 Prelim 2 Review Part 1 16
class PrefCustomer(Customer):
"""An instance is a 'preferred' customer, a Subclass of Customer.
Mutable attributes (in addition to Customer):
level: level of preference [One of 'bronze', 'silver', 'gold'] """
def __init__(self, n, l, e=None):
"""Initialize a new PrefCustomer with name n, optional email e, and
no purchases or spending, and level l
Pre: n is a string, e is a string or None"""
Customer.__init__(self,n,e=e)
self.level = l
Call the Superclass
initializer explicitly
as a helper!__str__,
makePurchase
“Inherited” from
Parent Class
![Page 17: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/17.jpg)
• What is ‘self’?
Not just a random thing you stick in front of stuff in
Classes!!!
Contains the ID of the object on which the method
was called
• Why is self.method() preferred to
ClassName.method(self) ?
If a class is extended with a subclass, self may refer
to an object of the subclass, and method() may be
overloaded in the subclass.
Notes on ‘self’
4/23/17 Prelim 2 Review Part 1 17
![Page 18: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/18.jpg)
What is on the Exam?
• The big topics
Nested Lists & Dictionaries (A3, Lab 8)
Recursion (A4, Lab 9)
Defining classes (Lab 10, Lab 11, A4)
Inheritance and subclasses (Lab 11)
Name Resolution
While Loops & Invariants
4/23/17 Prelim 2 Review Part 1 18
![Page 19: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/19.jpg)
Name Resolution (from P2 Fall 2013)
4/23/17 Prelim 2 Review Part 1 19
Execute the Following:
1) a.y
2) a.z
3) b.y
4) B.y
![Page 20: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/20.jpg)
Name Resolution (from P2 Fall 2013)
4/23/17 Prelim 2 Review Part 1 20
Execute the Following:
1) a.y 1
2) a.z error
3) b.y 3
4) B.y 4
![Page 21: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/21.jpg)
Name Resolution (from P2 Fall 2013)
4/23/17 Prelim 2 Review Part 1 21
Execute the Following:
1) a.f()
2) b.f()
3) a.f
4) A.g(b)
![Page 22: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/22.jpg)
Name Resolution (from P2 Fall 2013)
4/23/17 Prelim 2 Review Part 1 22
Execute the Following:
1) a.f() 4
2) b.f() 17
3) a.f <method A.f >
4) A.g(b) 10
![Page 23: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/23.jpg)
What is on the Exam?
• The big topics
Nested Lists & Dictionaries (A3, Lab 8)
Recursion (A4, Lab 9)
Defining classes (Lab 10, Lab 11, A4)
Inheritance and subclasses (Lab 11)
Name Resolution
While Loops & Invariants
4/23/17 Prelim 2 Review Part 1 23
![Page 24: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/24.jpg)
• What’s an Invariant?
An assertion (usually a condition) that is supposed to
"always" be true in a piece of code
If temporarily invalidated, must make it true again
• Loop Invariant – An assertion that should be true
before and after every iteration of the loop
• Class Invariant – assertion on value of attribute
E.g. [int, 0…maxValue]
Invariants
4/23/17 Prelim 2 Review Part 1 24
![Page 25: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/25.jpg)
• Initialize: Make Invariant True to start
• Terminate: Figure out where your loop
should stop and translate this into your
while loop condition
• Write the loop body to make progress
toward termination and keep invariant True
• Note: Pay attention to range:
Note: a..b <=> range(a, b+1)
While Loop Development Tips
4/23/17 Prelim 2 Review Part 1 25
![Page 26: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/26.jpg)
While Loop Function
def e_approximate(x, tol):
"""Returns: an integer giving the number of taylor series
terms neccessary for an approximation of e^x that is
between -tol and +tol of the actual value.
You can assume the math module is imported.
A taylor series approximation for e^x is the sum of
x^n / factorial(n), plus 1. That is to say, a two term
approximation is:
e^x ~ 1 + x^1/factorial(1) + x^2/factorial(2)
Pre: x is an int, tol a float. """
4/23/17 Prelim 2 Review Part 1 26
You would be given
more details of any math
concept used on an exam
![Page 27: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/27.jpg)
While Loop Function
def e_approximate(x, tol):
""“Spec"""
target = math.exp(x)
#invariant: approx is the taylor series approximation of e^x
#with n terms
4/23/17 Prelim 2 Review Part 1 27
![Page 28: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/28.jpg)
While Loop Function
def e_approximate(x, tol):
""“Spec"""
target = math.exp(x)
#invariant: approx is the taylor series approximation of e^x
#with n terms
n = 0
approx = 1.0
4/23/17 Prelim 2 Review Part 1 28
![Page 29: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/29.jpg)
While Loop Function
def e_approximate(x, tol):
""“Spec"""
target = math.exp(x)
#invariant: approx is the taylor series approximation of e^x
#with n terms
n = 0
approx = 1.0
while abs(approx - target) > tol:
4/23/17 Prelim 2 Review Part 1 29
![Page 30: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/30.jpg)
While Loop Function
def e_approximate(x, tol):
""“Spec"""
target = math.exp(x)
#invariant: approx is the taylor series approximation of e^x
#with n terms
n = 0
approx = 1.0
while abs(approx - target) > tol:
n += 1
approx += x**n / float(math.factorial(n))
return n
4/23/17 Prelim 2 Review Part 1 30
Note: setting n=1 and
flipping the while loop
statements violates the
invariant!
![Page 31: Prelim 2 Review Part 1 Spring 2017 - Cornell UniversityNested Lists & Dictionaries (A3, Lab 8) Recursion (A4, Lab 9) Defining classes (Lab 10, Lab 11, A4) Inheritance and subclasses](https://reader034.fdocuments.us/reader034/viewer/2022042022/5e795b512c71d003a30fdd3f/html5/thumbnails/31.jpg)
Good Luck!
4/23/17 Prelim 2 Review Part 1 31