generated from UCL-ARC-RSEing-with-Python/friend-group
-
Notifications
You must be signed in to change notification settings - Fork 67
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
Adding answers to week 3 homework assignment. #58
Open
L-Amos
wants to merge
15
commits into
UCL-COMP0233-24-25:main
Choose a base branch
from
L-Amos:luke
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
15 commits
Select commit
Hold shift + click to select a range
b48050f
Create data structure for example
weiliao001211 021d2b9
Changing Nash to be John's cousin, not Jill's cousin.
L-Amos a77bfa9
Adding a quick test to check how data structure handles no connections.
L-Amos fabcc68
Adding an entry to show how 'no-job' and 'no-connections' cases are e…
L-Amos 5d71df4
Adding forget function to remove connections.
L-Amos 13e383d
Adding add_person function to add people to my_group.
L-Amos 54a6303
Adding average_age() function to calculate mean age of group.
L-Amos 2831f4c
Adding first homework function (max_age)
L-Amos 281ed19
Adding second part of homework function (average_connections)
L-Amos 1e2ac35
Adding third part of homework function (max age if at least one conne…
L-Amos f05fa08
Changing age of 'Nobody' to test third part of homework.
L-Amos 86298fb
Adding final part of homework function (max age with friends)
L-Amos 671483e
Updating how no-connections are handled to allow for inclusion in cal…
L-Amos 3f22264
Removing unnecessary statement after changing the way no-connections …
L-Amos 25a3cb7
Adding JSON and YAML writing/loading.
L-Amos 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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"Jill": { | ||
"age": 26, | ||
"job": "biologist", | ||
"connections": { | ||
"Zalika": "friend", | ||
"John": "partner" | ||
} | ||
}, | ||
"Zalika": { | ||
"age": 28, | ||
"job": "artist", | ||
"connections": { | ||
"Jill": "friend", | ||
"Nash": "tenant" | ||
} | ||
}, | ||
"John": { | ||
"age": 27, | ||
"job": "writer", | ||
"connections": { | ||
"Jill": "partner", | ||
"Nash": "cousin" | ||
} | ||
}, | ||
"Nash": { | ||
"age": 34, | ||
"job": "chef", | ||
"connections": { | ||
"John": "cousin", | ||
"Zalika": "landlord" | ||
} | ||
}, | ||
"Nobody": { | ||
"age": 40, | ||
"job": null, | ||
"connections": {} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
Jill: | ||
age: 26 | ||
connections: | ||
John: partner | ||
Zalika: friend | ||
job: biologist | ||
John: | ||
age: 27 | ||
connections: | ||
Jill: partner | ||
Nash: cousin | ||
job: writer | ||
Nash: | ||
age: 34 | ||
connections: | ||
John: cousin | ||
Zalika: landlord | ||
job: chef | ||
Nobody: | ||
age: 40 | ||
connections: {} | ||
job: null | ||
Zalika: | ||
age: 28 | ||
connections: | ||
Jill: friend | ||
Nash: tenant | ||
job: artist |
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.""" | ||
|
||
# Your code to go here... | ||
import numpy as np | ||
|
||
my_group = | ||
my_group = { | ||
'Jill': { | ||
'age': 26, | ||
'job': 'biologist', | ||
'connections': { | ||
'Zalika': 'friend', | ||
'John': 'partner', | ||
} | ||
}, | ||
'Zalika': { | ||
'age': 28, | ||
'job': 'artist', | ||
'connections': { | ||
'Jill': 'friend', | ||
'Nash': 'tenant' | ||
} | ||
}, | ||
'John': { | ||
'age': 27, | ||
'job': 'writer', | ||
'connections': { | ||
'Jill': 'partner', | ||
'Nash': 'cousin' | ||
} | ||
}, | ||
'Nash': { | ||
'age': 34, | ||
'job': 'chef', | ||
'connections': { | ||
'John': 'cousin', | ||
'Zalika': 'landlord' | ||
} | ||
}, | ||
'Nobody': { | ||
'age': 40, | ||
'job': None, | ||
'connections': {} | ||
} | ||
} | ||
|
||
# Quick test - printing who knows who | ||
# for name1 in my_group.keys(): | ||
# if my_group[name1]['connections']: | ||
# for name2 in my_group[name1]['connections']: | ||
# print(f'{name1} knows {name2}') | ||
|
||
### STRETCH GOALS #### | ||
# Forget function | ||
def forget(person_1, person_2): | ||
if person_1 in my_group and person_2 in my_group[person_1]["connections"]: | ||
my_group[person_1]["connections"].pop(person_2) | ||
if person_2 in my_group and person_1 in my_group[person_2]["connections"]: | ||
my_group[person_2]["connections"].pop(person_1) | ||
else: | ||
print("Error: check names.") | ||
|
||
# Add Person | ||
def add_person(name, age, job=None, relations=None): | ||
if not isinstance(name, str): | ||
print("Name must be string") | ||
return | ||
if not isinstance(age, int): | ||
print("Age must be integer") | ||
return | ||
if job and not isinstance(job, str): | ||
print("Job must be string") | ||
return | ||
if relations and not isinstance(relations, dict): | ||
print("Connections must be dict") | ||
return | ||
my_group[name] = {"age": age, "job": job, "connections": relations} | ||
|
||
# Mean age | ||
def average_age(): | ||
ages = [my_group[person]["age"] for person in my_group.keys()] | ||
return np.mean(ages) | ||
|
||
# Homework Assignment | ||
def homework(): | ||
max_age = np.max([person["age"] for person in my_group.values()]) | ||
print(max_age) | ||
mean_connections = np.mean([len(person["connections"]) for person in my_group.values()]) | ||
print(mean_connections) | ||
max_age_with_connections = np.max([person["age"] for person in my_group.values() if person["connections"]]) | ||
print(max_age_with_connections) | ||
max_age_with_friends = np.max([person["age"] for person in my_group.values() if "friend" in person["connections"].values()]) | ||
print(max_age_with_friends) | ||
|
||
homework() | ||
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. this will run whenever you import group.py i believe, probably don't want to run this here. Use if name == "main": |
||
|
||
# Reading + Writing (YAML) | ||
import yaml | ||
with open("data_structure.yaml", "w") as f: | ||
yaml.dump(my_group, stream=f) | ||
with open("data_structure.yaml", "r") as f: | ||
loaded_data = yaml.safe_load(f) | ||
print(loaded_data) | ||
|
||
# Reading + Writing (JSON) | ||
import json | ||
with open("data_structure.json", "w") as f: | ||
json.dump(my_group, f, indent=4, sort_keys=False) | ||
with open("data_structure.json", "r") as f: | ||
loaded_data = json.loads(f.read()) | ||
print(loaded_data) |
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.
good checks, happy w this, maybe you can check if the keys in relations exist but 🤷