1 of 14

CSE 160 Section 10

Final Review 2: Code-writing Practice

TA

CSE 160: Final Review 2

2 of 14

Logistics

  • Section Assignment 10 due Friday, June 5th @ 11:59 PM
  • Resubmission Cycle 6/2 – 6/7
    • This is the last resubmission cycle!
    • Don’t forget to fill out the resubmission form.
  • Final Exam on Monday, June 8th @ 2:30 – 4:20 PM

CSE 160: Final Review 2

3 of 14

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):

  • Data structures
  • Functions
  • Nested structures
  • File I/O
  • Objects/classes
  • CSVs
  • Comprehensions

CSE 160: Final Review 2

4 of 14

Section Handout Problems

CSE 160: Final Review 2

5 of 14

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:

  • "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.

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

6 of 14

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

7 of 14

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

8 of 14

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

9 of 14

Problem 3.1

Write a class Apartment with the following behavior.

Each apartment stores:

  • unit (string)
  • rent (int)
  • occupied (bool)
  • tenants (list of strings)

Methods:

  1. add_tenant(name)

Adds the given tenant name to the apartment's tenant list.

  • toggle_occupancy()

Changes the apartment from occupied to vacant or vice versa.

  • monthly_cost()

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

10 of 14

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

11 of 14

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

12 of 14

Problem 3.2

True

['Sara', 'Rosario']

600.0

CSE 160: Final Review 2

13 of 14

Course Evals!

CSE 160: Final Review 2

14 of 14

Section Code: Blues

CSE 160: Final Review 2