-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem17-NumberLetterCounts.py
44 lines (39 loc) · 1.23 KB
/
Problem17-NumberLetterCounts.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
unit_names = """zero one two three four five six seven eight nine ten
eleven twelve thirteen fourteen fifteen sixteen seventeen
eighteen nineteen""".split()
tens_names = """zero ten twenty thirty forty fifty sixty seventy eighty
ninety""".split()
def english(n):
"Return the English name for n, from 0 to 999999."
if n >= 1000:
thous = english(n // 1000) + " thousand"
n = n % 1000
if n == 0:
return thous
elif n < 100:
return thous + " and " + english(n)
else:
return thous + ", " + english(n)
elif n >= 100:
huns = unit_names[n // 100] + " hundred"
n = n % 100
if n == 0:
return huns
else:
return huns + " and " + english(n)
elif n >= 20:
tens = tens_names[n // 10]
n = n % 10
if n == 0:
return tens
else:
return tens + "-" + english(n)
else:
return unit_names[n]
def letter_count(s):
"Return the number of letters in the string s."
import re
return len(re.findall(r'[a-zA-Z]', s))
def euler17():
return sum(letter_count(english(i)) for i in range(1, 1001))
print(euler17())