Skip to content

Commit

Permalink
Remove global variables UCL-RITS#166
Browse files Browse the repository at this point in the history
  • Loading branch information
nuttamas committed Dec 23, 2020
1 parent 36d0bc9 commit 95a7eaf
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions week09/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(group, person1, person2):
"""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(group, name, age, job, relations):
"""Add a new person with the given characteristics to the group."""
new_person = {
"age": age,
Expand Down Expand Up @@ -50,12 +50,12 @@ def add_person(name, age, job, relations):
"Zalika": "landlord"
}

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

forget("Nash", "John")
forget(group, "Nash", "John")

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!")

0 comments on commit 95a7eaf

Please sign in to comment.