-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path017.py
59 lines (51 loc) · 1.04 KB
/
017.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
def namer(n, over100 = False):
names = [
(1000000000, 'billion'),
(1000000, 'million'),
(1000, 'thousand'),
(100, 'hundred')
]
names2 = [
(90, 'ninety'),
(80, 'eighty'),
(70, 'seventy'),
(60, 'sixty'),
(50, 'fifty'),
(40, 'forty'),
(30, 'thirty'),
(20, 'twenty'),
(19, 'nineteen'),
(18, 'eighteen'),
(17, 'seventeen'),
(16, 'sixteen'),
(15, 'fifteen'),
(14, 'fourteen'),
(13, 'thirteen'),
(12, 'twelve'),
(11, 'eleven'),
(10, 'ten'),
(9, 'nine'),
(8, 'eight'),
(7, 'seven'),
(6, 'six'),
(5, 'five'),
(4, 'four'),
(3, 'three'),
(2, 'two'),
(1, 'one')
]
for k, label in names:
if n >= k:
return namer(n // k) + [label] + namer(n % k, True)
prefix = ['and'] if over100 else []
for k, label in names2:
if n >= k:
return prefix + [label] + namer(n % k, False)
return []
def name(n):
names = namer(n)
return 'zero' if len(names) == 0 else ''.join(names)
def main(lim):
return sum(len(name(n)) for n in range(1, lim + 1))
if __name__ == '__main__':
print(main(1000)) # 21124