generated from UCL-ARC-RSEing-with-Python/friend-group
-
Notifications
You must be signed in to change notification settings - Fork 69
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
implement my own four comprehension expressions to my group previous work #52
Open
Jiasheng-Yang
wants to merge
3
commits into
UCL-COMP0233-24-25:main
Choose a base branch
from
Jiasheng-Yang:weihong-jiasheng01-with-jiasheng
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,110 @@ | ||
"""An example of how to represent a group of acquaintances in Python.""" | ||
"""Summary data from group #7""" | ||
|
||
# Your code to go here... | ||
|
||
my_group = | ||
my_group = [ | ||
{ | ||
"name": "Jill", | ||
"age": 26, | ||
"job": "Biologist", | ||
"relations": [ | ||
{"name": "Zalika", "relationship": "friend"}, | ||
{"name": "John", "relationship": "partner"} | ||
] | ||
}, | ||
{ | ||
"name": "Zalika", | ||
"age": 28, | ||
"job": "Artist", | ||
"relations": [ | ||
{"name": "Jill", "relationship": "friend"} | ||
] | ||
}, | ||
{ | ||
"name": "John", | ||
"age": 27, | ||
"job": "Writer", | ||
"relations": [ | ||
{"name": "Jill", "relationship": "partner"}, | ||
{"name": "Nash", "relationship": "cousin"} | ||
] | ||
}, | ||
{ | ||
"name": "Nash", | ||
"age": 34, | ||
"job": "Chef", | ||
"relations": [ | ||
{"name": "John", "relationship": "cousin"}, | ||
{"name": "Zalika", "relationship": "landlord"} | ||
] | ||
} | ||
] | ||
|
||
def print_my_group(): | ||
for people in my_group: | ||
print(f"{people['name']} is {people['age']}, a/an {people.get('job', 'No job')}, ", end='') | ||
relations = [f"{connection['name']}'s {connection['relationship']}" for connection in people['relations']] | ||
print(f"{' and '.join(relations)}") | ||
|
||
def forget(person1, person2): | ||
for person in my_group: | ||
if person["name"] == person1: | ||
person["relations"] = [c for c in person["relations"] if c["name"] != person2] | ||
if person["name"] == person2: | ||
person["relations"] = [c for c in person["relations"] if c["name"] != person1] | ||
|
||
def add_person(name, age, job, relations): | ||
new_person = { | ||
"name": name, | ||
"age": int(age), | ||
"job": job, | ||
"relations": relations | ||
} | ||
my_group.append(new_person) | ||
|
||
def avg_age(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can use sum() here |
||
total_ages = 0 | ||
for person in my_group: | ||
total_ages += person['age'] | ||
average_age = total_ages / len(my_group) | ||
print(f"The average age of my group is {average_age}") | ||
|
||
"""print_my_group() | ||
forget("Jill", "John") | ||
add_person("Jiasheng", 24, "HPC Engineer", [{"name": "Jill", "relationship": "friend"}]) | ||
print_my_group() | ||
avg_age() | ||
""" | ||
|
||
"""Implement my own functions""" | ||
|
||
# Q1: the maximum age of people in the group | ||
max_age = max([person['age'] for person in my_group]) | ||
print(f"the maximum age of people in the group: {max_age}") | ||
|
||
# Q2: the average (mean) number of relations among members of the group | ||
avg_relation = sum(len(person['relations']) for person in my_group) / len(my_group) | ||
print(f"the average (mean) number of relations among members of the group: {avg_relation}") | ||
|
||
# Q3: the maximum age of people in the group that have at least one relation | ||
max_age_one_relation = max([person['age'] for person in my_group if person['relations']]) | ||
print(f"the maximum age of people in the group that have at least one relation: {max_age_one_relation}") | ||
|
||
# Q4: the maximum age of people in the group that have at least one friend | ||
person_with_friend = [] | ||
for person in my_group: | ||
for rel in person['relations']: | ||
if rel['relationship'] == 'friend': | ||
person_with_friend.append(person['age']) | ||
max_age_one_friend = max(person_with_friend) | ||
print(f"the maximum age of people in the group that have at least one friend: {max_age_one_friend}") | ||
|
||
""" | ||
[more advanced method for Q4] this method taught by my team member during the class | ||
utilize the generator expression with any() function | ||
""" | ||
max_age_friend_advanced = max( | ||
(person['age'] for person in my_group if any(rel['relationship'] == 'friend' for rel in person['relations'])), | ||
default=None | ||
) | ||
print(f"the advanced method utilizing the generator expression with any() function: {max_age_friend_advanced}") |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can break out of the loop earlier here ;)