Adding summary functions to answer week 3 HW#24
Open
tomholdendye wants to merge 1 commit intoUCL-MPHY0021-21-22:mainfrom
Open
Adding summary functions to answer week 3 HW#24tomholdendye wants to merge 1 commit intoUCL-MPHY0021-21-22:mainfrom
tomholdendye wants to merge 1 commit intoUCL-MPHY0021-21-22:mainfrom
Conversation
stefpiatek
reviewed
Oct 26, 2021
Collaborator
stefpiatek
left a comment
There was a problem hiding this comment.
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) |
Collaborator
There was a problem hiding this comment.
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() |
Collaborator
There was a problem hiding this comment.
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 |
Collaborator
There was a problem hiding this comment.
🎉 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!") |
Collaborator
There was a problem hiding this comment.
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!") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Answers UCL-MPHY0021-21-22/RSE-Classwork#12