forked from INM-6/Python-Module-of-the-Week
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamedTuple.py
32 lines (26 loc) · 851 Bytes
/
NamedTuple.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
from collections import namedtuple
from itertools import zip_longest
def analyse_string(string):
qs = string.count('q')
ingredients = sorted(list(set(string)))
d_p = sum((ord(s_c) - ord(s_p))**2 for s_c, s_p in zip_longest(
string,
'pineapple',
fillvalue=chr(0)))
return (qs, ingredients, d_p)
def sensible_analyse_string(string):
Analysis = namedtuple('Analysis', [
'number_of_qs',
'sorted_letters',
'pineapple_distance'
])
qs = string.count('q')
ingredients = sorted(list(set(string)))
d_p = sum((ord(s_c) - ord(s_p))**2 for s_c, s_p in zip_longest(
string,
'pineapple',
fillvalue=chr(0)))
return Analysis(qs, ingredients, d_p)
print(analyse_string(string='kumquat'))
b = sensible_analyse_string('pineapple').number_of_qs
print(b)