Skip to content

Adding summary functions to answer week 3 HW#24

Open
tomholdendye wants to merge 1 commit intoUCL-MPHY0021-21-22:mainfrom
tomholdendye:main
Open

Adding summary functions to answer week 3 HW#24
tomholdendye wants to merge 1 commit intoUCL-MPHY0021-21-22:mainfrom
tomholdendye:main

Conversation

@tomholdendye
Copy link

Copy link
Collaborator

@stefpiatek stefpiatek left a comment

Choose a reason for hiding this comment

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

Looks like it should indeed work, can you do it with comprehension expressions?

Comment on lines +37 to +41
ages = []
for i in range(len(group)):
vals = list(group.values())[i]
ages.append(vals["age"])
return max(ages)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This certainly does work, can you do them all with comprehension expressions?

So something like this

Suggested change
ages = []
for i in range(len(group)):
vals = list(group.values())[i]
ages.append(vals["age"])
return max(ages)
ages = [person["age"] for person in group.values()]
return max(ages)

Comment on lines +69 to +70
for i in range(len(group)):
relations = list(group.values())[i]["relations"].values()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also FYI, when iterating through something, it's a lot less common (and not considered pythonic) to use a range, instead you can just iterate through the items

Suggested change
for i in range(len(group)):
relations = list(group.values())[i]["relations"].values()
for person in group.values()):
relations = list(person["relations"].values())

and so on

return max(ages)

def mean_num_relations(group):
""" Finds the average number of relations amongst each member
Copy link
Collaborator

Choose a reason for hiding this comment

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

🎉 enjoy that you'e added docstrings

Comment on lines +75 to +76
oldest_person_age = max_age(group)
print("The oldest person is in the group is", oldest_person_age,"years old!")
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is a great place for f strings which you can format strings with, even evaluting code inline

Suggested change
oldest_person_age = max_age(group)
print("The oldest person is in the group is", oldest_person_age,"years old!")
oldest_person_age =
print(f"The oldest person is in the group is {max_age(group)} years old!")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants