CSE 160 Section 10
Final Review 2: Code-writing Practice
TA
CSE 160: Final Review 2
Logistics
CSE 160: Final Review 2
Section Plan
Practice problems!
If requested, we will spend any extra time reviewing material you all would like to focus on.
Topics include (but are not limited to):
CSE 160: Final Review 2
Section Handout Problems
CSE 160: Final Review 2
Problem 1
You are given a .csv file that looks like the following (additional lines not shown)
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:
For example, the above csv would produce the following list of dictionaries
[{'mean': 44.0, 'personality': 'extraversion'},
{'mean': 46.4, 'personality': 'agreeableness'},
{'mean': 53.2, 'personality': 'agreeableness'}]
CSE 160: Final Review 2
Problem 1
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 2
Problem 2
A tutoring center stores check-in data as a list of tuples. Each tuple contains:
(student_name, course, topic, minutes)
Example:
checkins = [
("Suhas", "CSE160", "lists", 30),
("Cici", "CSE160", "files", 45),
("Suhas", "CSE160", "dictionaries", 20),
("Brianna", "MATH126", "regression", 50),
("Cici", "MATH126", "plots", 25),
("Suhas", "CSE160", "lists", 15)
]
Write a function summarize_checkins(checkins) that returns a nested dictionary mapping each student to another dictionary mapping each topic to the total number of minutes that student spent on that topic.
Example:
summarize_checkins(checkins)
returns:
{
"Suhas": {"lists": 45, "dictionaries": 20},
"Cici": {"files": 45, "plots": 25},
"Brianna": {"regression": 50}
}
CSE 160: Final Review 2
Problem 2
def summarize_checkins(checkins):
summary = {}
for student, course, topic, minutes in checkins:
if student not in summary:
summary[student] = {}
if topic not in summary[student]:
summary[student][topic] = 0
summary[student][topic] += minutes
return summary
CSE 160: Final Review 2
Problem 3.1
Write a class Apartment with the following behavior.
Each apartment stores:
Methods:
Adds the given tenant name to the apartment's tenant list.
Changes the apartment from occupied to vacant or vice versa.
Returns the rent divided equally among all tenants.
You may assume there is at least one tenant.
Example:
apt1 = Apartment("1A", 1200, False, [])
apt1.add_tenant("Arona")
apt1.tenants
# ["Arona"]
apt2 = Apartment("1A", 1200, True, ["Asmi", "Katie", "Arpan"])
apt2.monthly_cost()
# 400.0
CSE 160: Final Review 2
Problem 3.1
class Apartment:
def __init__(self, unit, rent, occupied, tenants):
self.unit = unit
self.rent = rent
self.occupied = occupied
self.tenants = tenants
def add_tenant(self, name):
self.tenants.append(name)
def toggle_occupancy(self):
self.occupied = not self.occupied
def monthly_cost(self):
return self.rent / len(self.tenants)
CSE 160: Final Review 2
Problem 3.2
What will the following code print?
apt = Apartment("1A", 1200, False, ["Sara"])
apt.add_tenant("Rosario")
apt.toggle_occupancy()
print(apt.occupied)
print(apt.tenants)
print(apt.monthly_cost())
CSE 160: Final Review 2
Problem 3.2
True
['Sara', 'Rosario']
600.0
CSE 160: Final Review 2
Course Evals!
CSE 160: Final Review 2
Section Code: Blues
CSE 160: Final Review 2