-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.py
executable file
·68 lines (48 loc) · 1.91 KB
/
17.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
68
#! /usr/bin/env python
# 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.
def number_to_word(n):
if n > 1000:
return -1
ones = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
teens = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
tens = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
hundred = 'hundred'
thousand = 'onethousand'
number = ''
place_val = 1
while n > 0:
val = n % 10
n /= 10
if val == 0:
place_val *= 10
continue
if place_val == 1:
if n % 10 == 1:
n /= 10
place_val *= 10
number = '{}'.format(teens[val - 1])
else:
number = '{}{}'.format(ones[val - 1], number).strip()
elif place_val == 10:
number = '{}{}'.format(tens[val - 1], number).strip()
elif place_val == 100:
if len(number) > 0:
number = '{}{}{}{}'.format(ones[val - 1], hundred, 'and', number).strip()
else:
number = '{}{}{}'.format(ones[val - 1], hundred, number).strip()
elif place_val == 1000:
number = '{}{}'.format(thousand, number).strip()
place_val *= 10
return number
length = 0
for x in range(1, 1001):
length += len(number_to_word(x).strip())
print length