From 5b760fbb29c61dff236da97a26170a63097283e8 Mon Sep 17 00:00:00 2001 From: Rebecca Phelan Date: Sat, 30 Nov 2024 12:33:41 +0000 Subject: [PATCH 1/3] Add dictionary as argument to functions --- refactoring/initial_global.py | 50 ++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/refactoring/initial_global.py b/refactoring/initial_global.py index 21f1323..e434345 100644 --- a/refactoring/initial_global.py +++ b/refactoring/initial_global.py @@ -1,16 +1,18 @@ -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.""" + assert person1 in group, f"{person1} is not in the group!" + assert person2 in group, f"{person2} is not in the group!" 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, @@ -50,12 +52,48 @@ 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__": + friend_group = { + "Jess": { + "age": 26, + "job": "biologist", + "relations": { + "Zoey": "friend", + "Joeff": "partner" + } + }, + "Zoey": { + "age": 28, + "job": "artist", + "relations": { + "Jess": "friend", + } + }, + "Joeff": { + "age": 27, + "job": "writer", + "relations": { + "Jess": "partner" + } + } + } 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!") + + # Test the functions with the friend_group + print('Average age of the group:', average_age(friend_group)) + print('Number of members in the group:', len(friend_group)) + gregg_relations = { + "Jess": "friend", + "Zoey": "friend", + "Joeff": "friend" + } + add_person(friend_group, "Gregg", 34, "chef", gregg_relations) + print('Number of members in the group:', len(friend_group)) + print('Average age of the group:', average_age(friend_group)) From ce0509b126c487a04d3b0f2b49cda7625475be87 Mon Sep 17 00:00:00 2001 From: Rebecca Phelan Date: Sat, 30 Nov 2024 13:18:32 +0000 Subject: [PATCH 2/3] Created Person Class --- refactoring/initial_person_class.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/refactoring/initial_person_class.py b/refactoring/initial_person_class.py index 569d26d..13a5e6c 100644 --- a/refactoring/initial_person_class.py +++ b/refactoring/initial_person_class.py @@ -16,7 +16,9 @@ def add_connection(self, person, relation): def forget(self, person): """Removes any connections to a person""" - pass + if person not in self.connections: + raise ValueError(f"I don't know {person.name}") + self.connections.pop(person) def average_age(group): @@ -26,12 +28,21 @@ def average_age(group): if __name__ == "__main__": - # ...then create the group members one by one... + # Create each person jill = Person("Jill", 26, "biologist") + zalika = Person("Zalika", 28, "artist") + john = Person("John", 27, "writer") + nash = Person("Nash", 34, "chef") - # ...then add the connections one by one... - # Note: this will fail from here if the person objects aren't created + # Add connections jill.add_connection(zalika, "friend") + jill.add_connection(john, "partner") + zalika.add_connection(jill, "friend") + john.add_connection(jill, "partner") + john.add_connection(nash, "cousin") + zalika.add_connection(nash, "tenant") + nash.add_connection(john, "cousin") + nash.add_connection(zalika, "landlord") # ... then forget Nash and John's connection nash.forget(john) From 745191a330e8b947f1e374d5ad9f1fe3a7dfb117 Mon Sep 17 00:00:00 2001 From: Rebecca Phelan Date: Sat, 30 Nov 2024 13:28:44 +0000 Subject: [PATCH 3/3] Group Class --- refactoring/initial_two_classes.py | 38 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/refactoring/initial_two_classes.py b/refactoring/initial_two_classes.py index 4054da7..f67838d 100644 --- a/refactoring/initial_two_classes.py +++ b/refactoring/initial_two_classes.py @@ -18,7 +18,9 @@ def __init__(self): def size(self): """Return how many people are in the group.""" - pass + for member in self.members: + assert isinstance(member, Person), "Group members should be of type Person" + return len(self.members) def contains(self, name): """Check whether the group contains a person with the given name. @@ -32,17 +34,36 @@ def add_person(self, name, age, job): def number_of_connections(self, name): """Find the number of connections that a person in the group has""" - pass + if not self.contains(name): + raise ValueError(f"{name} is not in the group!") + return len(self.connections.get(name, {})) def connect(self, name1, name2, relation, reciprocal=True): """Connect two given people in a particular way. Optional reciprocal: If true, will add the relationship from name2 to name 1 as well """ - pass + # Check if both people are in the group + if not self.contains(name1): + raise ValueError(f"{name1} is not in the group!") + if not self.contains(name2): + raise ValueError(f"{name2} is not in the group!") + # Check if the connection already exists + if name2 in self.connections.get(name1, {}): + raise ValueError(f"{name1} is already connected to {name2}!") + # Add the connection + self.connections.setdefault(name1, {})[name2] = relation + # Add the reciprocal connection + if reciprocal: + self.connections.setdefault(name2, {})[name1] = relation def forget(self, name1, name2): """Remove the connection between two people.""" - pass + if not self.contains(name1): + raise ValueError(f"{name1} is not in the group!") + if not self.contains(name2): + raise ValueError(f"{name2} is not in the group!") + self.connections.get(name1, {}).pop(name2, None) + self.connections.get(name2, {}).pop(name1, None) def average_age(self): """Compute the average age of the group's members.""" @@ -55,8 +76,15 @@ def average_age(self): my_group = Group() # ...then add the group members one by one... my_group.add_person("Jill", 26, "biologist") + my_group.add_person("Zalika", 28, "artist") + my_group.add_person("John", 27, "writer") + my_group.add_person("Nash", 34, "chef") # ...then their connections - my_group.connect("Jill", "Zalika", "friend") + my_group.connect("Jill", "Zalika", "friend", reciprocal=True) + my_group.connect("John", "Jill", "partner", reciprocal=True) + my_group.connect("John", "Nash", "cousin", reciprocal=True) + my_group.connect("Zalika", "Nash", "tenant", reciprocal=False) + my_group.connect("Nash", "Zalika", "landlord", reciprocal=False) # ... then forget Nash and John's connection my_group.forget("Nash", "John")