diff --git a/week09/refactoring/initial_global.py b/week09/refactoring/initial_global.py index 21f1323..0b4ea37 100644 --- a/week09/refactoring/initial_global.py +++ b/week09/refactoring/initial_global.py @@ -20,41 +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__": + 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) + nash_relations = { + "John": "cousin", + "Zalika": "landlord" + } -forget("Nash", "John") + add_person("Nash", 34, "chef", nash_relations) -if __name__ == "__main__": + forget("Nash", "John") assert len(group) == 4, "Group should have 4 members" assert average_age() == 28.75, "Average age of the group is incorrect!" assert len(group["Nash"]["relations"]) == 1, "Nash should only have one relation" diff --git a/week09/refactoring/initial_person_class.py b/week09/refactoring/initial_person_class.py index 569d26d..99fdaf3 100644 --- a/week09/refactoring/initial_person_class.py +++ b/week09/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): @@ -28,13 +28,22 @@ 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") + john.add_connection(jill, "partner") + nash.add_connection(john, "cousin") + nash.add_connection(zalika, "landlord") + zalika.add_connection(jill, "friend") # ... then forget Nash and John's connection nash.forget(john) + john.forget(nash) # Then create the group my_group = {jill, zalika, john, nash} diff --git a/week09/refactoring/initial_two_classes.py b/week09/refactoring/initial_two_classes.py index a54d1b3..99e6e67 100644 --- a/week09/refactoring/initial_two_classes.py +++ b/week09/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,17 +32,35 @@ 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. Optional reciprocal: If true, will add the relationship from name2 to name 1 as well """ - pass + if reciprocal: + if name1 in self.connections: + self.connections[name1].update({name2: relation}) + else: + self.connections.update({name1: {name2: relation}}) + + if name2 in self.connections: + self.connections[name2].update({name1: relation}) + else: + self.connections.update({name2: {name1: relation}}) + + else : + if name1 in self.connections: + self.connections[name1].update({name2: relation}) + else: + self.connections.update({name1: {name2: relation}}) def forget(self, name1, name2): """Remove the connection between two people.""" - pass + print(self.connections) + self.connections[name1].pop(name2, None) + self.connections[name2].pop(name1, None) + print(self.connections) def average_age(self): """Compute the average age of the group's members.""" @@ -55,8 +73,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("Nash", 34, "chef") + my_group.add_person("John", 27, "writer") # ...then their connections my_group.connect("Jill", "Zalika", "friend") + my_group.connect("Nash", "Zalika", "landlord") + my_group.connect("Jill", "John", "partner") + my_group.connect("Nash", "John", "cousin") # ... then forget Nash and John's connection my_group.forget("Nash", "John")