From b48050f3e6e831d730c8702c15777db16a34fa45 Mon Sep 17 00:00:00 2001 From: Wei Liao Date: Mon, 14 Oct 2024 10:35:30 +0100 Subject: [PATCH 01/15] Create data structure for example --- group.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index e2ec347..77c3525 100644 --- a/group.py +++ b/group.py @@ -2,4 +2,37 @@ # Your code to go here... -my_group = +my_group = { + 'Jill': { + 'age': 26, + 'job': 'biologist', + 'connections': { + 'Zalika': 'friend', + 'John': 'partner', + 'Nash': 'cousin' + } + }, + 'Zalika': { + 'age': 28, + 'job': 'artist', + 'connections': { + 'Jill': 'friend', + 'Nash': 'tenant' + } + }, + 'John': { + 'age': 27, + 'job': 'writer', + 'connections': { + 'Jill': 'partner' + } + }, + 'Nash': { + 'age': 34, + 'job': 'chef', + 'connections': { + 'John': 'cousin', + 'Zalika': 'landlord' + } + } +} From 021d2b93b83d12499509a9ed7b462e383f9c7c34 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Mon, 14 Oct 2024 10:37:40 +0100 Subject: [PATCH 02/15] Changing Nash to be John's cousin, not Jill's cousin. --- group.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/group.py b/group.py index 77c3525..97d5fa4 100644 --- a/group.py +++ b/group.py @@ -8,8 +8,7 @@ 'job': 'biologist', 'connections': { 'Zalika': 'friend', - 'John': 'partner', - 'Nash': 'cousin' + 'John': 'partner' } }, 'Zalika': { @@ -24,7 +23,8 @@ 'age': 27, 'job': 'writer', 'connections': { - 'Jill': 'partner' + 'Jill': 'partner', + 'Nash': 'cousin' } }, 'Nash': { From a77bfa91701d33445ae0a91993410c67efda4458 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Mon, 14 Oct 2024 10:42:40 +0100 Subject: [PATCH 03/15] Adding a quick test to check how data structure handles no connections. --- group.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/group.py b/group.py index 97d5fa4..bd3d302 100644 --- a/group.py +++ b/group.py @@ -36,3 +36,9 @@ } } } + +# 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}') From fabcc686cbe31eb07b2e4b6a66ca434a8c661f91 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Mon, 14 Oct 2024 10:51:22 +0100 Subject: [PATCH 04/15] Adding an entry to show how 'no-job' and 'no-connections' cases are entered. --- group.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/group.py b/group.py index bd3d302..e511b5a 100644 --- a/group.py +++ b/group.py @@ -34,6 +34,11 @@ 'John': 'cousin', 'Zalika': 'landlord' } + }, + 'Nobody': { + 'age': 24, + 'job': None, + 'connections': None } } From 5d71df42b6e291f80846cf65f7c47d7ead25c00c Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Mon, 14 Oct 2024 11:30:07 +0100 Subject: [PATCH 05/15] Adding forget function to remove connections. --- group.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/group.py b/group.py index e511b5a..1e20e5a 100644 --- a/group.py +++ b/group.py @@ -43,7 +43,17 @@ } # 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}') +# 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.") \ No newline at end of file From 13e383d86da785a6124a0347245b2957d7e26101 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Mon, 14 Oct 2024 11:38:01 +0100 Subject: [PATCH 06/15] Adding add_person function to add people to my_group. --- group.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index 1e20e5a..8cd89b2 100644 --- a/group.py +++ b/group.py @@ -56,4 +56,20 @@ def forget(person_1, 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.") \ No newline at end of file + 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} \ No newline at end of file From 54a63034ca0e38823cd4e35b55219130deabb36e Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Mon, 14 Oct 2024 11:42:02 +0100 Subject: [PATCH 07/15] Adding average_age() function to calculate mean age of group. --- group.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index 8cd89b2..2593a11 100644 --- a/group.py +++ b/group.py @@ -1,6 +1,7 @@ """An example of how to represent a group of acquaintances in Python.""" # Your code to go here... +import numpy as np my_group = { 'Jill': { @@ -72,4 +73,9 @@ def add_person(name, age, job=None, relations=None): if relations and not isinstance(relations, dict): print("Connections must be dict") return - my_group[name] = {"age": age, "job": job, "connections": relations} \ No newline at end of file + 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) \ No newline at end of file From 2831f4c9fa37b51bb36b74d850046ec9d64058df Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Tue, 15 Oct 2024 14:53:12 +0100 Subject: [PATCH 08/15] Adding first homework function (max_age) --- group.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index 2593a11..daaa27d 100644 --- a/group.py +++ b/group.py @@ -78,4 +78,11 @@ def add_person(name, age, job=None, relations=None): # Mean age def average_age(): ages = [my_group[person]["age"] for person in my_group.keys()] - return np.mean(ages) \ No newline at end of file + return np.mean(ages) + +# Homework Assignment +def homework(): + max_age = np.max([person["age"] for person in my_group.values()]) + print(max_age) + +homework() From 281ed191c7cdafad99b8461542e5f43eb790ce26 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Tue, 15 Oct 2024 14:55:46 +0100 Subject: [PATCH 09/15] Adding second part of homework function (average_connections) --- group.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index daaa27d..e5a8123 100644 --- a/group.py +++ b/group.py @@ -9,7 +9,7 @@ 'job': 'biologist', 'connections': { 'Zalika': 'friend', - 'John': 'partner' + 'John': 'partner', } }, 'Zalika': { @@ -84,5 +84,7 @@ def average_age(): 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() if person["connections"]]) + print(mean_connections) homework() From 1e2ac35e6f23149316b31baaf6ca1b733d6ea22d Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Tue, 15 Oct 2024 14:57:13 +0100 Subject: [PATCH 10/15] Adding third part of homework function (max age if at least one connection) --- group.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/group.py b/group.py index e5a8123..01fe8e6 100644 --- a/group.py +++ b/group.py @@ -86,5 +86,7 @@ def homework(): print(max_age) mean_connections = np.mean([len(person["connections"]) for person in my_group.values() if person["connections"]]) 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) homework() From f05fa08410c6287c4063aad8798a7a4feccab5f2 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Tue, 15 Oct 2024 14:57:40 +0100 Subject: [PATCH 11/15] Changing age of 'Nobody' to test third part of homework. --- group.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group.py b/group.py index 01fe8e6..48fc07e 100644 --- a/group.py +++ b/group.py @@ -37,7 +37,7 @@ } }, 'Nobody': { - 'age': 24, + 'age': 40, 'job': None, 'connections': None } From 86298fb799efd14dc65cde93d567669f4b0a2baf Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Tue, 15 Oct 2024 15:29:45 +0100 Subject: [PATCH 12/15] Adding final part of homework function (max age with friends) --- group.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/group.py b/group.py index 48fc07e..e31b486 100644 --- a/group.py +++ b/group.py @@ -88,5 +88,7 @@ def homework(): 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 person["connections"] and "friend" in person["connections"].values()]) + print(max_age_with_friends) homework() From 671483ecea3f389cba83d309b0885818563992cf Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Tue, 15 Oct 2024 15:49:28 +0100 Subject: [PATCH 13/15] Updating how no-connections are handled to allow for inclusion in calculation of mean connections. --- group.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/group.py b/group.py index e31b486..ab61a87 100644 --- a/group.py +++ b/group.py @@ -39,7 +39,7 @@ 'Nobody': { 'age': 40, 'job': None, - 'connections': None + 'connections': {} } } @@ -84,7 +84,7 @@ def average_age(): 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() if person["connections"]]) + 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) From 3f22264f880a6351ef3d39f78900312445afcbbc Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Tue, 15 Oct 2024 17:09:56 +0100 Subject: [PATCH 14/15] Removing unnecessary statement after changing the way no-connections is represented. --- group.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/group.py b/group.py index ab61a87..1a5a722 100644 --- a/group.py +++ b/group.py @@ -88,7 +88,7 @@ def homework(): 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 person["connections"] and "friend" in person["connections"].values()]) + 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() From 25a3cb715da907b878d9f5970812e8caac9c4507 Mon Sep 17 00:00:00 2001 From: Luke Amos Date: Wed, 16 Oct 2024 19:13:03 +0100 Subject: [PATCH 15/15] Adding JSON and YAML writing/loading. --- data_structure.json | 39 +++++++++++++++++++++++++++++++++++++++ data_structure.yaml | 28 ++++++++++++++++++++++++++++ group.py | 16 ++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 data_structure.json create mode 100644 data_structure.yaml diff --git a/data_structure.json b/data_structure.json new file mode 100644 index 0000000..851bc5e --- /dev/null +++ b/data_structure.json @@ -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": {} + } +} \ No newline at end of file diff --git a/data_structure.yaml b/data_structure.yaml new file mode 100644 index 0000000..95e174f --- /dev/null +++ b/data_structure.yaml @@ -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 diff --git a/group.py b/group.py index 1a5a722..c18156f 100644 --- a/group.py +++ b/group.py @@ -92,3 +92,19 @@ def homework(): print(max_age_with_friends) homework() + +# 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)