Skip to content

Answers to summary data from group #79

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 9 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ Some things you may wish to consider in your model:
- Does it allow people who have no job?
- Does it allow people with no connections?
- Does it assume that connections are always reciprocal (e.g. if A is B's friend, does B
automatically consider A a friend too?)
automatically consider A a friend too?)
142 changes: 141 additions & 1 deletion group.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,144 @@

# 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",
"Nash": "landlord"
}
},

"John": {
"age": 27,
"job": ["writer"],
"relationships": {
"Jill": "partner",
"Nash": "cousin"
}
},

"Nash": {
"age": 34,
"job": ["chef"],
"relationships": {
"John": "cousin",
"Zalika": "tenant"
}
},

"Nanashi": {
"age": 20,
"job": [],
"relationships": {}
}
}

'''
# checking if the dictionary correctly reflects relations
for names in my_group.keys():
if my_group[names]["relationships"]:
for name2 in my_group[names]["relationships"]:
print (f"{names} knows {name2}")
else:
print (f"{names} doesn't know anyone in my group.")
'''

def forget(person1, person2):
"""
removes connection between 2 people in the group
"""
if person1 in my_group.keys() and person2 in my_group[person1]["relationships"]:
del my_group[person1]["relationships"][person2]
else:
print (f"{person1} doesn't know {person2}, check name.")
if person2 in my_group.keys() and person1 in my_group[person2]["relationships"]:
del my_group[person2]["relationships"][person1]
else:
print (f"{person2} doesn't know {person1}, check name.")

def add_person(name, age, job=[], relations={}):
"""
adds a person to my_group
"""
if not isinstance(name,str):
print ("Name must be a string value.")
if not isinstance(age, int):
print ("Age must be an integer.")
if not isinstance(job, list):
print ("Jobs must be a list.")
if not isinstance(relations, dict):
print ("Relations must be a dictionary.")
my_group[name] = dict([("age", age), ("job", job), ("relationships", relations)])

def get_age():
"""
puts the age of each person in the dictionary into a list
such that it's easier to calculate the average or the maximum
"""
age_list = []
for names in my_group:
age_list.append(my_group[names]["age"])
return age_list

def average_age():
"""
calculates the average age of my_group (without importing libraries)
"""
ages = get_age()
average = sum(ages)/len(ages)
return average

def maximum_age():
"""
identifies the maximum age of people in the group
"""
ages = get_age()
return max(ages)

def get_No_of_relations():
"""
looks up of the number of relations for
each person in the dictionary and summarizes them into a list
such that it's easier to utilize in other functions using No. of relations
"""
no_of_relation_list = []
for names in my_group:
no_of_relation_list.append(len(my_group[names]["relationships"]))
return no_of_relation_list

def average_No_of_relations():
"""
calculates the average No. of relations
among members of my_group (without importing libraries)
"""
no_of_relations = get_No_of_relations()
average_no_of_relations = sum(no_of_relations)/len(no_of_relations)
return average_no_of_relations

def max_age_person_with_1_relation():
max_age = 0
for names in my_group:
if len(my_group[names]["relationships"]) >= 1:
if my_group[names]["age"] > max_age:
max_age = my_group[names]["age"]
return max_age

def max_age_person_with_1_friend():
max_age = 0
for names in my_group:
if "friend" in (my_group[names]["relationships"]).values():
if my_group[names]["age"] > max_age:
max_age = my_group[names]["age"]
return max_age