Skip to content
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
61 changes: 58 additions & 3 deletions group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
"""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"
}
},
"NewGuyNoFriends": {
"age": 150,
"job": "student",
"relations": {
}
Comment on lines +24 to +28
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

poor guy 😢

},
"Nash": {
"age": 34,
"job": "chef",
"relations": {
"John": "cousin",
"Zalika": "landlord"
}
}
}

# the maximum age of people in the group
max_age = max([group.get(personName).get("age") for personName in group.keys()])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could simplify the base of your list comprehension a little bit by using the values directly

Suggested change
max_age = max([group.get(personName).get("age") for personName in group.keys()])
max_age = max(person.get("age") for person in group.values())

print("oldest person is", max_age)

# the average (mean) number of relations among members of the group
import numpy as np
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some style feedback: you should have all import statements at the start of your script

relationships_dict_list = [group.get(personName).get("relations") for personName in group.keys()]
mean_relations = np.mean([len(personalRelations.keys()) for personalRelations in relationships_dict_list])
print("mean number of relations is", mean_relations)

# the maximum age of people in the group that have at least one relation
max_non_relationless = max([group.get(personName).get("age") for personName in group.keys() if len(group.get(personName).get("relations")) != 0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also use the inherent "falsyness" of an empty dictionary

Suggested change
max_non_relationless = max([group.get(personName).get("age") for personName in group.keys() if len(group.get(personName).get("relations")) != 0])
max_non_relationless = max([group.get(personName).get("age") for personName in group.keys() if group.get(personName).get("relations")])

print("oldest person with at least one relation of any type is", max_non_relationless, "years old")

# [more advanced] the maximum age of people in the group that have at least one friend
max_nonfriendless = max([group.get(personName).get("age") for personName in group.keys() if "friend" in list(group.get(personName).get("relations").values())])
print("oldest person with a relation of type friend is", max_nonfriendless, "years old")



# Your code to go here...

my_group =