Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Added the requested changes to the file group.py #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
85 changes: 82 additions & 3 deletions group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
"""An example of how to represent a group of acquaintances in Python."""
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": {
"age": 34,
"job": "chef",
"relations": {
"John": "cousin",
"Zalika": "landlord"
}
}
}


#the maximum age of people in the group
import numpy as np

ages=[]

for person in group:
ages.append(group[person]["age"])
print('maximum age of people in the group is', np.max(ages))



#the average (mean) number of relations among members of the group
relation = []
for person in group:
#print(group[person]["relations"])
relation.append(len(group[person]["relations"].keys()))
print('average number of relations among members of the group is', np.mean(relation))


#the maximum age of people in the group that have at least one relation
age_n = []
for person in group:
if len(group[person]["relations"].keys()) >= 1:
age_n.append(group[person]["age"])
print('maximum age of people in the group that have at least one relation is', np.max(age_n))



#[more advanced] the maximum age of people in the group that have at least one friend
Ages = []
for person in group:
if "friend" in group[person]["relations"].values():
Ages.append(group[person]["age"])

print('The maximum age of people in the group that have at least one friend is', np.max(Ages))




import json

#write file
with open('group_file.json', 'w') as json_file:
json.dumps(group, json_file, indent=4)

#read file
with open('group_file.json', 'r') as json_file:
group_data = json_file.read()

# Your code to go here...

my_group =