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
| >>> def average_age(members):
... return sum([x[2] for x in members])/len(members)
...
>>> average_age([('s','s',35),('a','a',34),('n','n',34),('d','o',36),('j','j',34)])
34.6
>>> def oldest_member(members):
... return max(members, key=(lambda x:x[2]))[0]
...
>>> oldest_member([('s','s',35),('a','a',34),('n','n',34),('d','o',36),('j','j',34)])
'd'
>>> def second_oldest_member(members):
... return sorted(members, key=(lambda x:x[2]))[1][0]
...
>>> second_oldest_member([('s','s',35),('a','a',34),('n','n',34),('d','o',36),('j','j',34)])
'n'
>>> def next_older_member(members, nickname):
... return min([a for a in members if a[2] > [b for b in members if b[0]==nickname][0][2]],key=(lambda x:x[2]))[0]
...
>>> next_older_member([('st','ms',19),('m','my',17),('k','nk',23),('f','kf',22),('sh','ss',20)],'f')
'k'
>>> def check_future_age(current_members, future_members):
... return len({i[2]-j[2] for i,j in zip(current_members,future_members)}) == 1
...
>>> check_future_age([('st','ms',19),('m','my',17),('k','nk',23),('f','kf',22),('sh','ss',20)],[('st','ms',22),('m','my',20),('k','nk',26),('f','kf',25),('sh','ss',23)])
True
>>> check_future_age([('st','ms',19),('m','my',17),('k','nk',23),('f','kf',22),('sh','ss',20)],[('st','ms',22),('m','my',20),('k','nk',26),('f','kf',25),('sh','ss',24)])
False
|