-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem17.py
58 lines (47 loc) · 1.7 KB
/
problem17.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
"""
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.
"""
numbers = [
"", "one", "two",
"three", "four", "five",
"six", "seven", "eight",
"nine", "ten", "eleven",
"twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen",
"eighteen", "nineteen"
]
ranks = ["simple", "decades", "hundred", "thousand"]
decades = [
"zero", "one",
"twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
]
def letter_word(number):
word = ""
length = len(str(number))
dec = int(str(number)[-2:-1]) if length > 1 else 0
last = int(str(number)[-1])
i = length
while i > 2:
index = length - i
#последний десяток
if str(number)[index] != '0':
word += "{} {} ".format(
numbers[int((str(number)[index]))],
ranks[i-1]
)
i -= 1
if len(word) > 0 and (dec+last) > 0:
word += "and "
if dec >= 2:
word += "{} {}".format(decades[dec], numbers[last])
else:
below20index = int("{}{}".format(dec,last))
word += "{}".format(numbers[below20index])
return "".join(word.split())
count = 0
for i in range(1,1001):
count += len(letter_word(i))
print (count)