From afed44b01b983b799987e3587dca3cf84443744e Mon Sep 17 00:00:00 2001 From: Rongru Date: Fri, 28 Oct 2022 17:01:28 +0100 Subject: [PATCH 1/3] first_version by RR --- group.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index e2ec347..4c23d91 100644 --- a/group.py +++ b/group.py @@ -1,5 +1,22 @@ """An example of how to represent a group of acquaintances in Python.""" + # Your code to go here... -my_group = +my_group = {"Jill": {"Age": 26, + "Job": "biologist", + "Relationship": {"Zalika": "friend", + "partner": "John"}}, + + "Zalika": {"Age": 28, + "Job": "artist", + "Relationship": {"Jill": "friend"}}, + + "John": {"Age": 28, + "Job": "writer", + "Relationship": {"Jill": "partner"}}, + + "Nash": {"Age": 34, + "Job": "chef", + "Relationship": {"John": "cousin", + "Zalika": "landlord"}}} From 8fdf02ac9018ef35dffec17b7db00bcfea0be379 Mon Sep 17 00:00:00 2001 From: Rongru Date: Fri, 28 Oct 2022 18:39:42 +0100 Subject: [PATCH 2/3] added forget, new people and average age function --- group.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/group.py b/group.py index 4c23d91..f07b554 100644 --- a/group.py +++ b/group.py @@ -20,3 +20,26 @@ "Job": "chef", "Relationship": {"John": "cousin", "Zalika": "landlord"}}} + + +def forget(person1, person2): + for name, info in my_group.items(): + RelationDic = info["Relationship"] + if name == person1: + RelationDic.pop(person2, None) + if name == person2: + RelationDic.pop(person1, None) + +def add_person(name, age, job, relations = {}): + my_group[name] = {"Age": age, "Job": job, "Relationship": relations } + +def average_age(): + age = 0 + count_num = 0 + for name, info in my_group.items(): + age += info["Age"] + count_num += 1 + average = age/count_num + print(f"The average age of {count_num} people in the group is {average}") + + \ No newline at end of file From f095f8ad6346ccddf57a56262b87629bdeb43f1c Mon Sep 17 00:00:00 2001 From: Rongru Date: Mon, 31 Oct 2022 11:48:30 +0000 Subject: [PATCH 3/3] added summary data in comprehensive expression --- group.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/group.py b/group.py index f07b554..89a05b6 100644 --- a/group.py +++ b/group.py @@ -2,11 +2,10 @@ # Your code to go here... - my_group = {"Jill": {"Age": 26, "Job": "biologist", "Relationship": {"Zalika": "friend", - "partner": "John"}}, + "John": "partner"}}, "Zalika": {"Age": 28, "Job": "artist", @@ -21,6 +20,9 @@ "Relationship": {"John": "cousin", "Zalika": "landlord"}}} +#forget(person1, person2) which removes the connection between two people in the group +#add_person(name, age, job, relations) which adds a new person with the given characteristics to the group +#average_age() which calculates the mean age for the group def forget(person1, person2): for name, info in my_group.items(): @@ -42,4 +44,21 @@ def average_age(): average = age/count_num print(f"The average age of {count_num} people in the group is {average}") - \ No newline at end of file +###Try some of the functions +#add_person("Rongru",24,"Student",{"John": "friend"}) + +#forget("Jill","Zalika") +#my_group + + +##Add the following comprehensive relations +#the maximum age of people in the group +#the average (mean) number of relations among members of the group +#the maximum age of people in the group that have at least one relation +#[more advanced] the maximum age of people in the group that have at least one friend + +MaxiAge_comprehend = max([info["Age"] for name, info in my_group.items()]) +Mean_relation = sum([len(info["Relationship"]) for name, info in my_group.items()])/len(my_group.keys()) +MaxAge_relation = max([info["Age"] for name, info in my_group.items() if len(info["Relationship"]) >1]) +MaxAge_friend = max([info["Age"] for name, info in my_group.items() if "friend" in info["Relationship"].values()]) +