-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_017.py
46 lines (29 loc) · 1.11 KB
/
problem_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
# coding: utf-8
'''
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
'''
# EXTERNAL LIBRARY USED: inflect
import inflect
p = inflect.engine()
def count(number):
word = p.number_to_words(number)
word = word.replace(' ', '')
word = word.replace('-', '')
word = word.replace(',', '')
return len(word)
def sum_count(_max):
return sum([count(i) for i in range(1, _max + 1)])
def test_count():
assert count(342) == 23
assert count(115) == 20
def test_sum_count():
assert sum_count(5) == 19
def main():
return sum_count(1000)
if __name__ == '__main__':
test_count()
test_sum_count()
print(sum_count(1000))
# 21124 in 44.9ms