Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions refactoring/initial_global.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
def average_age():
def average_age(group):
"""Compute the average age of the group's members."""
all_ages = [person["age"] for person in group.values()]
return sum(all_ages) / len(group)


def forget(person1, person2):
def forget(person1, person2, group):
"""Remove the connection between two people."""
group[person1]["relations"].pop(person2, None)
group[person2]["relations"].pop(person1, None)


def add_person(name, age, job, relations):
def add_person(name, age, job, relations, group):
"""Add a new person with the given characteristics to the group."""
new_person = {
"age": age,
Expand All @@ -19,8 +19,9 @@ def add_person(name, age, job, relations):
}
group[name] = new_person

if __name__ == "__main__":

group = {
group = {
"Jill": {
"age": 26,
"job": "biologist",
Expand All @@ -43,19 +44,19 @@ def add_person(name, age, job, relations):
"Jill": "partner"
}
}
}

nash_relations = {
"John": "cousin",
"Zalika": "landlord"
}
}

add_person("Nash", 34, "chef", nash_relations)
nash_relations = {
"John": "cousin",
"Zalika": "landlord"
}

forget("Nash", "John")
add_person("Nash", 34, "chef", nash_relations, group)
forget("Nash", "John", group)

if __name__ == "__main__":
assert len(group) == 4, "Group should have 4 members"
assert average_age() == 28.75, "Average age of the group is incorrect!"
assert average_age(group) == 28.75, "Average age of the group is incorrect!"
assert len(group["Nash"]["relations"]) == 1, "Nash should only have one relation"
print("All assertions have passed!")