-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler17.py
67 lines (60 loc) · 1.74 KB
/
euler17.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
60
61
62
63
64
65
66
67
def int_to_en_str(n):
"""
:param n: integer <= 1000
:return: the litteral English representation of n
"""
s = str(n)
if len(s) > 4:
raise Exception("Works up to 9999 only")
if s == "1000":
return "one thousand"
if len(s) == 3:
remainder = int_to_en_str(s[1:])
return int_to_en_str(s[0]) + " hundred" + (" and " + remainder if remainder not in ("", "zero") else "")
if len(s) == 2:
if s[0] == '1': # the "teens"
return {
'10': 'ten',
'11': 'eleven',
'12': 'twelve',
'13': 'thirteen',
'14': 'fourteen',
'15': 'fifteen',
'16': 'sixteen',
'17': 'seventeen',
'18': 'eighteen',
'19': 'nineteen',
}[s]
remainder = int_to_en_str(s[1:])
return {
'0': '',
'2': 'twenty',
'3': 'thirty',
'4': 'forty',
'5': 'fifty',
'6': 'sixty',
'7': 'seventy',
'8': 'eighty',
'9': 'ninety',
}[s[0]] + ("-" if s[0] != '0' and remainder != "zero" else '') + (remainder if s[1] != "zero" and remainder != "zero" else "")
if len(s) == 1:
return {
'0': 'zero',
'1': 'one',
'2': 'two',
'3': 'three',
'4': 'four',
'5': 'five',
'6': 'six',
'7': 'seven',
'8': 'eight',
'9': 'nine',
}[s]
def main():
en = []
for i in range(1, 1001):
tmp = int_to_en_str(i)
print(tmp)
en.append(tmp)
print(len(''.join(en).replace(' ', '').replace('-', '')))
main()