Section 9
Final Review
CSE 160
CSE 160: Final Review
Logistics
CSE 160: Final Review
Debugging & Testing
Consider the following function:
def funky_times(lst):
"""
Given a list of numbers, returns the product of the�first three elements. If the list has fewer than three�elements, return None.
"""
# Implementation not shown
What behaviors might you want to test for this function? Write a test case�for each behavior.
CSE 160: Final Review
Debugging & Testing
Possible behaviors:
(This is not an exhaustive list!)
Behavior | Example test |
List with 0 elements | assert funky_times([]) is None |
List with 1 element | assert funky_times([1]) is None |
List with 2 elements | assert funky_times([1, 2]) is None |
List with 3 integer elements | assert funky_times([3, 1, 4]) == 12 |
List with 3 float elements | assert math.isclose(funky_times([3.0, 1.0, 2.0]), 6.0) |
List with 3 elements (mixed float/integer) | assert math.isclose(funky_times([1.0, 2, 3]), 6.0) |
List with more than 3 elements | assert funky_times([1, 2, 3, 4]) == 6 |
List where all elements are the same | assert funky_times([2, 2, 2, 2]) == 8 |
CSE 160: Final Review
Final Review
CSE 160
CSE 160: Final Review
Final Review Problems
CSE 160: Final Review
Nested Structures (part a)
What data structure would you use if you wanted to keep track of animal species (dog, cat, fish, etc), animal name (John, Paul, Ringo, etc), and animal age (4, 2, 8, etc). It would be best for each animal in the shelter to have its own entry. Additionally, we should be able to easily get each of the three attributes for that animal.
CSE 160: Final Review
Nested Structures (part a)
List of Dictionaries (other answers may be acceptable)
CSE 160: Final Review
Nested Structures (part b)
Given these animals, represent them in the data structure you chose.
animal,name,age
Dog,Connor,5
Cat,Miko,8
Fish,Smithy,5
Dog,Flower,3
Cat,Bjorki,2
CSE 160: Final Review
Nested Structures (part b)
[{"animal": "Dog", "name": "Connor", "age": 5}.
{"animal": "Cat", "name": “Miko”, "age": 8},
{"animal": "Fish", "name": "Smithy", "age": 5},
{"animal": "Dog", "name": "Flower", "age": 3},
{"animal": "Cat", "name": "Bjorki", "age": 2}]
CSE 160: Final Review
Classes
2. What will the following code print?
class Apartment:
def __init__(self, unit,
rent, occupied):
self.unit = unit
self.rent = rent
self.occupied = occupied
def toggle_occupancy(self):
self.occupied = not self.occupied
def get_status(self):
if self.occupied == True:
return "Occupied"
else:
return "Vacant"
apt = Apartment("1A", 1200, False)
print(apt.get_status())
apt.toggle_occupancy()
print(apt.get_status())
CSE 160: Final Review
Classes
Vacant
Occupied
CSE 160: Final Review
CSV DictReader
agreeableness,openness,extraversion,conscientiousness,neuroticism
36,41,72,45,26
73,5,35,53,66
84,16,16,82,68
Write a function called analyze_personality(filename) that takes in a .csv filename and returns a list of dictionaries. Each dictionary contains the following information for each row:
"mean": Mean of all of the numbers for a row as a float
"personality": The name of the personality trait with the highest number as a string.
CSE 160: Final Review
CSV DictReader
def analyze_personality(filename):
output = []
with open(filename) as f:
reader = csv.DictReader(f)
for row in reader:
avg = 0
highest = ("", 0)
for key, val in row.items():
val = float(val)
avg += val
if val > highest[1]:
highest = (key, val)
output.append({"mean": avg / len(row), "personality": highest[0]})
return output
CSE 160: Final Review
What do you want
to review?
Were there any confusing lectures or topics that would be helpful to revisit?
CSE 160: Final Review
Course Evaluations
(Section, Lecture)
Please take 5-10 mins to fill out the course and section evaluations
CSE 160: Final Review