forked from UCL-COMP0233-24-25/friend-group
-
Notifications
You must be signed in to change notification settings - Fork 1
/
group.py
66 lines (50 loc) · 1.86 KB
/
group.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""An example of how to represent a group of acquaintances in Python."""
# Your code to go here...
my_group = {}
Jill = {'name':'Jill', 'age':'26', 'job':'biologist',
'connections':{'friend':['Zalika'],
'partner':['John']}
}
Zalika = {'name':'Zalika', 'age':'28', 'job':'biologist',
'connections':{'friend':['Jill']}
}
John = {'name':'Jhon', 'age':'27', 'job':'writer',
'connections':{'partner':['Jill']}
}
Nash = {'name':'Nash', 'age':'34', 'job':'chef',
'connections':{'cousin':['Jhon'],
'landlord':['Zalika']}
}
my_group['Jill'] = Jill
my_group['Zalika'] = Zalika
my_group['John'] = John
my_group['Nash'] = Nash
print(my_group)
ages = []
print('the maximum age of people in the group:')
for person in my_group:
ages.append(my_group[person]['age'])
print(max(ages))
relation_num = 0
person_num = 0
print('the average (mean) number of relations among members of the group:')
for person in my_group:
relation_num += len(my_group[person]['connections'])
person_num += 1
print(relation_num/person_num)
ages_with_relation = []
print('the maximum age of people in the group that have at least one relation:')
for person in my_group:
if len(my_group[person]['connections']) != 0:
ages_with_relation.append(my_group[person]['age'])
print(max(ages_with_relation))
print('[more advanced] the maximum age of people in the group '
'that have at least one friend')
ages_with_friend = []
print('the maximum age of people in the group that have at least one friend:')
for person in my_group:
if len(my_group[person]['connections']) != 0:
connection = my_group[person]['connections']
if 'friend' in connection:
ages_with_friend.append(my_group[person]['age'])
print(max(ages_with_friend))