Review
Winter 2024
Reminders/Exam info
Major Topics
Major Topics
Let’s start
T/F?
n2 + log n10 = O (n2 ) -> T/F
n! = O(n^n)
5^n = O(n^n)
Complexity?
Complexity?
Complexity?
O(1)
Complexity?
O(1)
O (n log n)
One more
def complexity3(lst):
t = 0
for i in range(lst):
t = t + 1
print(t)
repr and str
repr and str
Try to answer:
2) What is the difference between str and repr
3) Can they give different outputs?
4) Can they give the same outputs?
5) Can repr or str give you an error (given syntax is correct?)
class UC(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "UC('" + self.name + "')"
def __str__(self):
return 'UC ' + self.name
ucsd = UC("San Diego")
print(repr(ucsd))
A: UC San Diego
B: "UC('San Diego')"
C: UC('San Diego')
class UC(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "UC('" + self.name + "')"
def __str__(self):
return 'UC ' + self.name
ucsd = UC("San Diego")
print(str(ucsd))
A: UC San Diego
B: 'UC('San Diego')'
C: UC('San Diego')
class UC(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "UC('" + self.name + "')"
def __str__(self):
return 'UC ' + self.name
ucsd = UC("San Diego")
print(ucsd.__repr__())
A: UC San Diego
B: 'UC('San Diego')'
C: UC('San Diego')
class UC(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "UC('" + self.name + "')"
def __str__(self):
return 'UC ' + self.name
ucsd = UC("San Diego")
print(ucsd.__str__())
A: UC San Diego
B: 'UC('San Diego')'
C: UC('San Diego')
class UC(object):
def __init__(self, name):
self.name = name
def __repr__(self):
return "UC('" + self.name + "')"
def __str__(self):
return 'UC ' + self.name
ucsd = UC("San Diego")
repr(str(ucsd)) == repr(ucsd.__str__())
A: True
B: False
print(a)
repr(a)
a.str()
a.repr()
str(a)
a.__str__()
a.__repr__()
print(A())
repr(A())
A().__str__()
A().__repr__()
A.__str__(a)
A.__repr__(a)
https://docs.google.com/presentation/d/1fOwxZXp7VJCxi8sBsFJ5N2nZtbK70EbM/edit#slide=id.p53
Lambda function
>>> j = lambda: lambda x: x*2
What is the proper way to call it?
A: j
B: j()
C: j(3)
D: j()(3)
E: j(3)()
F: j(3)(3)
Dictionaries
Write a function accept_login(users, username, password). The function should return True if the user exists and the password is correct and False otherwise.
users = { "user1" : "password1",
"user2" : "password2",
"user3" : "password3"
}
if accept_login(users, "wronguser", "wrongpassword"):
print("login successful!")
else :
print("login failed...")
Dictionaries
users = { "user1" : "password1",
"user2" : "password2",
"user3" : "password3"
}
if accept_login(users, "wronguser", "wrongpassword"):
print("login successful!")
else :
print("login failed...")
def accept_login(users, username, password):
if users.get(username) == None:
return False
if users.get(username) != password:
print("login failed...")
else:
print("login successful!")
Check point
with open("check_point.txt", "r") as f:
read_data = f.read()
read_data = read_data.replace(",", "!")
with open("check_point.txt", "a") as f:
f.write(read_data)
f.write("Last Line")
D: Error
A:
B:
E: Other
C:
Hello, Ifra, best of luck on your exams!
Inheritance
class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()
A) 0 1
B) 0 0
C) Error because class Derived_Test inherits Test but variable x isn’t inherited
D) Error because when object is created, argument must be passed like Derived_Test(1)
class Test:
def __init__(self):
self.x = 0
class Derived_Test(Test):
def __init__(self):
Test.__init__(self)
self.y = 1
def main():
b = Derived_Test()
print(b.x,b.y)
main()
A) Error because class Derived_Test inherits Test but variable x isn’t inherited
B) 0 0
C) 0 1
D) Error, the syntax of the invoking method is wrong
does every user defined class need an explicit constructor?
class Review:
message = "Good Luck!"
r = Review()
print(r.message)
Will it work?
Recursion
def pick_ared(input):
lst = list(input.keys()) # list if keys [“M.L.”, “A.B.”, “C.D.”]
def helper(lst):
if len(lst) == 0:
return ""
if input[“C.D.”][:3] == '858' or input[lst[0]][:3] == '619':
return str(lst[0]) + ':' + str(input[lst[0]]) + " " + helper(lst[1:])
else:
return helper(lst[1:])
rtn = helper(lst)
return rtn
Objects (if time permits)
Instance attributes are found before class attributes; class attributes are inherited
Objects
Instance attributes are found before class attributes; class attributes are inherited
Objects
Instance attributes are found before class attributes; class attributes are inherited
Objects
Instance attributes are found before class attributes; class attributes are inherited
Objects
Instance attributes are found before class attributes; class attributes are inherited
Land Owners
Instance attributes are found before class attributes; class attributes are inherited
Objects
Instance attributes are found before class attributes; class attributes are inherited
Objects
Instance attributes are found before class attributes; class attributes are inherited
Objects
What is the output?
>>> Worker().work()
Objects
What is the output?
>>> jack
Objects
What is the output?
>>> jack.work()
Objects
What is the output?
>>> john.work()
Objects
What is the output?
>>> john.elf.work(john)
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects
Objects