Skip to content

Commit

Permalink
finished task 168
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidScobie committed Dec 9, 2020
1 parent 3a79929 commit 500562a
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions week09/refactoring/initial_two_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -32,17 +32,21 @@ 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 == True:
self.connections[name1] = {name2 : relation}
self.connections[name2] = {name1 : relation}
else:
self.connections[name1] = {name2 : relation}

def forget(self, name1, name2):
"""Remove the connection between two people."""
pass
self.connections.pop(name2,None)

def average_age(self):
"""Compute the average age of the group's members."""
Expand All @@ -55,11 +59,19 @@ 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("zalika", "Jill", "friend")
my_group.connect("John", "Jill", "partner")
my_group.connect("Nash", "John", "cousin")
my_group.connect("Nash", "Zalika", "landlord")
# ... then forget Nash and John's connection
my_group.forget("Nash", "John")

assert my_group.size() == 4, "Group should have 4 members"
assert my_group.average_age() == 28.75, "Average age of the group is incorrect!"
assert my_group.number_of_connections("Nash") == 1, "Nash should only have one relation"
Expand Down

0 comments on commit 500562a

Please sign in to comment.