From 71c93b51b1778a1cdb15fe50d81e8d80c27438e9 Mon Sep 17 00:00:00 2001 From: L-Amos Date: Sun, 1 Dec 2024 19:20:18 +0000 Subject: [PATCH 1/6] Refactoring to make group dict an argument instead of a global variable. --- refactoring/initial_global.py | 72 +++++++++++++++++------------------ 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/refactoring/initial_global.py b/refactoring/initial_global.py index 21f1323..de02bed 100644 --- a/refactoring/initial_global.py +++ b/refactoring/initial_global.py @@ -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, @@ -20,42 +20,42 @@ def add_person(name, age, job, relations): group[name] = new_person -group = { - "Jill": { - "age": 26, - "job": "biologist", - "relations": { - "Zalika": "friend", - "John": "partner" - } - }, - "Zalika": { - "age": 28, - "job": "artist", - "relations": { - "Jill": "friend", - } - }, - "John": { - "age": 27, - "job": "writer", - "relations": { - "Jill": "partner" +if __name__ == "__main__": + # Create Example Group + group = { + "Jill": { + "age": 26, + "job": "biologist", + "relations": { + "Zalika": "friend", + "John": "partner" + } + }, + "Zalika": { + "age": 28, + "job": "artist", + "relations": { + "Jill": "friend", + } + }, + "John": { + "age": 27, + "job": "writer", + "relations": { + "Jill": "partner" + } } } -} - -nash_relations = { - "John": "cousin", - "Zalika": "landlord" -} -add_person("Nash", 34, "chef", nash_relations) - -forget("Nash", "John") - -if __name__ == "__main__": + nash_relations = { + "John": "cousin", + "Zalika": "landlord" + } + # Modify Example Group + add_person(group, "Nash", 34, "chef", nash_relations) + forget(group, "Nash", "John") + # Check Group is Modified Correctly 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!") From 2a8bf77cf0547674be733b887124a0b99b4365b0 Mon Sep 17 00:00:00 2001 From: L-Amos Date: Sun, 1 Dec 2024 19:38:58 +0000 Subject: [PATCH 2/6] Add forget method of Person class. --- refactoring/initial_person_class.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/refactoring/initial_person_class.py b/refactoring/initial_person_class.py index 569d26d..ad1ebcf 100644 --- a/refactoring/initial_person_class.py +++ b/refactoring/initial_person_class.py @@ -16,7 +16,7 @@ def add_connection(self, person, relation): def forget(self, person): """Removes any connections to a person""" - pass + self.connections.pop(person, None) def average_age(group): From 371d1e009c12feb5a8beaa57fbadf33297085e9e Mon Sep 17 00:00:00 2001 From: L-Amos Date: Sun, 1 Dec 2024 19:39:54 +0000 Subject: [PATCH 3/6] Create each person + add their connections. --- refactoring/initial_person_class.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/refactoring/initial_person_class.py b/refactoring/initial_person_class.py index ad1ebcf..969ab6f 100644 --- a/refactoring/initial_person_class.py +++ b/refactoring/initial_person_class.py @@ -28,11 +28,17 @@ def average_age(group): if __name__ == "__main__": # ...then create the group members one by one... 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 jill.add_connection(zalika, "friend") - + jill.add_connection(john, "partner") + zalika.add_connection(jill, "friend") + john.add_connection(jill, "partner") + nash.add_connection(john, "cousin") + nash.add_connection(zalika, "landlord") # ... then forget Nash and John's connection nash.forget(john) # Then create the group From 4c857943277bcf7d660cd2df594eac90a7ab6dc4 Mon Sep 17 00:00:00 2001 From: L-Amos Date: Sun, 1 Dec 2024 19:43:24 +0000 Subject: [PATCH 4/6] Implement size and number_of_connections methods of Group class. --- refactoring/initial_two_classes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/refactoring/initial_two_classes.py b/refactoring/initial_two_classes.py index 4054da7..be4d324 100644 --- a/refactoring/initial_two_classes.py +++ b/refactoring/initial_two_classes.py @@ -18,7 +18,7 @@ def __init__(self): def size(self): """Return how many people are in the group.""" - pass + return len(self.members) def contains(self, name): """Check whether the group contains a person with the given name. @@ -32,7 +32,7 @@ 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 + return(len(self.connections[name])) def connect(self, name1, name2, relation, reciprocal=True): """Connect two given people in a particular way. From 89f7b1cb664deb1a27c3cf6934d637598e2e1f8c Mon Sep 17 00:00:00 2001 From: L-Amos Date: Sun, 1 Dec 2024 20:04:42 +0000 Subject: [PATCH 5/6] Implement connect and forget methods of Group class. --- refactoring/initial_two_classes.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/refactoring/initial_two_classes.py b/refactoring/initial_two_classes.py index be4d324..421d7ea 100644 --- a/refactoring/initial_two_classes.py +++ b/refactoring/initial_two_classes.py @@ -38,11 +38,13 @@ 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 + self.connections[name1] = {name2: relation} + if reciprocal: + self.connections[name2] = {name1: relation} def forget(self, name1, name2): """Remove the connection between two people.""" - pass + self.connections[name1].pop(name2, None) def average_age(self): """Compute the average age of the group's members.""" From b4e1ceef861b20123956ac7af34a242045073e4a Mon Sep 17 00:00:00 2001 From: L-Amos Date: Sun, 1 Dec 2024 20:07:10 +0000 Subject: [PATCH 6/6] Adding people + connections to empty group. --- refactoring/initial_two_classes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/refactoring/initial_two_classes.py b/refactoring/initial_two_classes.py index 421d7ea..631f108 100644 --- a/refactoring/initial_two_classes.py +++ b/refactoring/initial_two_classes.py @@ -57,8 +57,14 @@ 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", "John", "partner") + my_group.connect("Nash", "John", "cousin") + my_group.connect("Nash", "Zalika", "landlord", reciprocal=False) # ... then forget Nash and John's connection my_group.forget("Nash", "John")