From 40697ace04360512b027fb59f310fee7b9a2adb4 Mon Sep 17 00:00:00 2001 From: Jennivine <45113203+Jennivine@users.noreply.github.com> Date: Mon, 14 Oct 2024 21:42:10 +0100 Subject: [PATCH 1/2] implemented a data structure to represent a group of acquaintances using dictionaries and lists --- group.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index e2ec347..1975706 100644 --- a/group.py +++ b/group.py @@ -2,4 +2,33 @@ # Your code to go here... -my_group = +my_group = [ + { + "Name": "Jill", + "Age": 26, + "Job": "Biologist", + "Connections": {"Friends": ["Zalika"], + "Partner": "John"} + }, + { + "Name": "Zalika", + "Age": 28, + "Job": "Artist", + "Connections": {"Friends": ["Jill"], + "Landlord": "Nash"} + }, + { + "Name": "John", + "Age": 27, + "Job": "Writer", + "Connections": {"Partner": "Jill"} + }, + { + "Name": "Nash", + "Age": 34, + "Job": "Chef", + "Connections": {"Cousins": ["John"], + "Tennants": ["Zalika"]} + } +] + From 5835d6789607de93d0357beccaa9a7ba1090cb87 Mon Sep 17 00:00:00 2001 From: Jennivine <45113203+Jennivine@users.noreply.github.com> Date: Mon, 14 Oct 2024 22:00:41 +0100 Subject: [PATCH 2/2] added comprehension expressions to get information about age and number of relations --- group.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/group.py b/group.py index 1975706..98a63a2 100644 --- a/group.py +++ b/group.py @@ -7,28 +7,41 @@ "Name": "Jill", "Age": 26, "Job": "Biologist", - "Connections": {"Friends": ["Zalika"], - "Partner": "John"} + "Connections": {"Zalika": "friend", + "John": "partner"} }, { "Name": "Zalika", "Age": 28, "Job": "Artist", - "Connections": {"Friends": ["Jill"], - "Landlord": "Nash"} + "Connections": {"Jill": "friend", + "Nash": "landlord"} }, { "Name": "John", "Age": 27, "Job": "Writer", - "Connections": {"Partner": "Jill"} + "Connections": {"Jill": "partner"} }, { "Name": "Nash", "Age": 34, "Job": "Chef", - "Connections": {"Cousins": ["John"], - "Tennants": ["Zalika"]} + "Connections": {"John": "cousin", + "Zalika": "tennant"} } ] +import numpy as np + +max_age = max(person["Age"] for person in my_group) +print(f"Maximum age: {max_age}") + +avg_nb_relations = np.mean([len(person["Connections"]) for person in my_group]) +print(f"Average number of relations: {avg_nb_relations}") + +max_age_with_at_least_one_relation = max(person["Age"] for person in my_group if len(person["Connections"]) >= 1) +print(f"Maximum age of people who have at least one relation: {max_age_with_at_least_one_relation}") + +max_age_with_at_least_one_friend = max(person["Age"] for person in my_group if sum(relation=="friend" for relation in person["Connections"].values()) >= 1) +print(f"Maximum age of people who have at least one friend {max_age_with_at_least_one_friend}") \ No newline at end of file