From f763a3a8a275b564397c3ae739ee4892e13862e5 Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 20 Oct 2022 10:33:28 +0100 Subject: [PATCH 1/3] Friend group info added to group.py --- group.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index e2ec347..c2d71fd 100644 --- a/group.py +++ b/group.py @@ -2,4 +2,41 @@ # Your code to go here... -my_group = +my_group = [ + { + 'name' : 'Jill', + 'age' : 26, + 'job' : 'biologist', + 'connections' : { + 'friend' : 'Zalika', + 'partner' : 'John' + } + }, + { + 'name' : 'Zalika', + 'age' : 28, + 'job' : 'artist', + 'connections' : { + 'friend' : 'Jill' + } + }, + { + 'name' : 'John', + 'age' : 27, + 'job' : 'writer', + 'connections' : { + 'partner' : 'Jill' + } + }, + { + 'name' : 'Nash', + 'age' : 34, + 'job' : 'chef', + 'connections' : { + 'cousin' : 'John', + 'landlord' : 'Zarika' + } + } +] + + From d82bc52a5a57065a319ad1c852689ebabc8a1c0d Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 20 Oct 2022 11:02:38 +0100 Subject: [PATCH 2/3] Stretch functions added --- stretch_.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 stretch_.py diff --git a/stretch_.py b/stretch_.py new file mode 100644 index 0000000..49d3ff7 --- /dev/null +++ b/stretch_.py @@ -0,0 +1,41 @@ +def forget(person1, person2): + # person1 is the main human who is forgetting person2 + for person in my_group: + if person['name'] == person1: + for connection in person['connections']: + if person['connections'][connection] == person2: + del person['connections'][connection] + break + +def add_person(name, age, job, connections): + my_group.append( + { + 'name' : name, + 'age' : int(age), + 'job' : job, + 'connections' : connections + } + ) + +def average_age(): + total = 0 + for person in my_group: + total += person['age'] + return total/len(my_group) + +my_group = [ + { + 'name' : 'Jill', + 'age' : 26, + 'job' : 'biologist', + 'connections' : { + 'friend' : 'Zalika', + 'partner' : 'John' + } + } +] + +forget('Jill', 'John') +add_person('Rachel', '22', '', {}) +print(my_group) +print(average_age()) \ No newline at end of file From 87d0ac2219dcbc9cf36bb1af83f207952f2d3f4c Mon Sep 17 00:00:00 2001 From: Bronte Wan Date: Thu, 20 Oct 2022 14:23:56 +0100 Subject: [PATCH 3/3] Comprehension expressions added to group.py --- group.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/group.py b/group.py index c2d71fd..32059a1 100644 --- a/group.py +++ b/group.py @@ -2,6 +2,9 @@ # Your code to go here... +from statistics import mean + + my_group = [ { 'name' : 'Jill', @@ -39,4 +42,7 @@ } ] - +print('The maximum age is ' + str(max(person['age'] for person in my_group))) +print('The mean number of relations is ' + str(mean(len(person['connections']) for person in my_group))) +print('The maximum age of people with at least one relation is ' + str(max(person['age'] for person in my_group if person['connections']))) +print('The maximum age of people with at least one friend is ' + str(max(person['age'] for person in my_group if 'friend' in person['connections'])))