Skip to content

adding dictionary and changing functions to take the group as an input #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,63 @@

# Your code to go here...

my_group =
my_group = {
"Jill":{
"age":26,
"job":"biologist",
"relationships": {
"Zalika":"friend",
"John":"partner",
}
},
"Zalika":{
"age":28,
"job":"artist",
"relationships":{
"Jill":"friend",
}
}, # type: ignore
"John":{
"age":27,
"job":"writer",
"relationships":{
"Jill":"partner",
}
},
"Nash":{
"age":33,
"job":"chef",
"relationships":{
"John":"cousin",
"Zalika":"landlord",
}
}
}
print(my_group)

""" functions to act on the group of people"""

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(group, person1, person2):
"""Remove the connection between two people."""
group[person1]["relationships"].pop(person2, None)
group[person2]["relationships"].pop(person1, None)


def add_person(group, new_person):
"""Add a new person with the given characteristics to the group.
name in dictionary format: name, age, job, relations"""
group[new_person] = new_person

"""testing if all code runs as it should"""

assert len(my_group) == 4, "Group should have 4 members"
assert average_age(my_group) == 28.5, "Average age of the group is incorrect!"
forget(my_group, "Nash", "John")
assert len(my_group["Nash"]["relationships"]) == 1, "Nash should only have one relation"
print("All assertions have passed!")
51 changes: 51 additions & 0 deletions group_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Person:
"""A class to represent an individual and their connections."""

def __init__(self, name, age, job):
"""Create a new Person with the given name, age and job and no connections."""
self.name = name
self.age = age
self.job = job
self.connections = dict()

def add_connection(self, person, relation):
"""Add a new connection to a person"""
if person in self.connections:
raise ValueError(f"I already know about {person.name}")
self.connections[person] = relation

def forget(self, person):
"""Removes any connections to a person"""
self.connections.pop(person, None)


def average_age(group):
"""Compute the average age of the group's members."""
all_ages = [person.age for person in group]
return sum(all_ages) / len(group)

print(__name__)

if __name__ == "__main__":
# ...then create the group members one by one...
jill = Person("Jill", 26, "biologist")
nash = Person("Nash",33, "chef")
zalika= Person("zalika", 28, "artist")
john = Person("John", 27, "writer")
# ...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")
nash.add_connection(john, "cousin")
nash.add_connection(jill, "friend")

print(nash.connections)

# ... then forget Nash and John's connection
nash.forget(john)
# Then create the group
my_group = {jill, zalika, john, nash}

assert len(my_group) == 4, "Group should have 4 members"
assert average_age(my_group) == 28.5, "Average age of the group is incorrect!"
assert len(nash.connections) == 1, "Nash should only have one relation "
print("All assertions have passed!")