-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.py
72 lines (67 loc) · 1.89 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
69
70
71
72
number_dic = {1:"one",
2:"two",
3:"three",
4:"four",
5:"five",
6:"six",
7:"seven",
8:"eight",
9:"nine",
10:"ten",
11:"eleven",
12:"twelve",
13:"thirteen",
14:"fourteen",
15:"fifteen",
16:"sixteen",
17:"seventeen",
18:"eighteen",
19:"nineteen",
20:"twenty",
30:"thirty",
40:"forty",
50:"fifty",
60:"sixty",
70:"seventy",
80:"eighty",
90:"ninety",
100:"hundred",
1000:"thousand"}
def nearest_power_of_ten(n):
# returns the power of ten just below or equal n
k = 0
while 10**k <= n:
k += 1
k -= 1
return k
def decompose(n, k=-1):
if k < 0: k = nearest_power_of_ten(n)
n_list = []
temp = n
for i in range(k+1):
n_list.append(int(temp/10**(k-i)))
temp -= n_list[-1]*(10**(k-i))
return n_list
def calculate(n_list):
l = len(n_list)
return sum([k*10**(l-i-1) for i,k in enumerate(n_list)])
def spell(n):
if number_dic.has_key(n) and n < 100:
return number_dic[n]
n_list = decompose(n)
if n < 100:
return "%s %s" % (number_dic[n_list[0]*10], number_dic[n_list[1]])
if n < 1000:
# below 1000 > n > 100
n_prime = calculate(n_list[1:])
if n_prime > 0:
return "%s %s %s %s" % (number_dic[n_list[0]], number_dic[100],
"and", spell(n_prime))
else:
return "%s %s" % (number_dic[n_list[0]], number_dic[100])
if n == 1000: return "one thousand"
raise
result = 0
for i in range(1,1001):
result += len(spell(i).replace(" ",""))
print result