-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem17.py
96 lines (93 loc) · 3.26 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
ones = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
tens = ["teen", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"]
hundreds = "hundred"
thousands = "thousand"
def length(n):
string = str(n)
inWords = ""
# if 1000's
if len(string) == 4:
# thousands
inWords = inWords + "onethousand"
# hundreds
inWords = inWords + ones[int(string[1]) - 1]
inWords = inWords + "and"
# tens
if not string[2] == 1:
inWords = inWords + tens[int(string[2]) - 1]
inWords = inWords + ones[int(string[3]) - 1]
return inWords
else:
if string[3] > 5:
inWords = inWords + ones[int(string[3]) - 1]
inWords = inWords + tens[0]
return inWords
else:
if string[3] == 1:
inWords = inWords + "eleven"
elif string[3] == 2:
inWords = inWords + "twelve"
elif string[3] == 3:
inWords = inWords + "thirteen"
elif string[3] == 4:
inWords = inWords + "fourteen"
else:
inWords = inWords + "fifteen"
return inWords
elif len(string) == 3:
#hundreds
inWords = inWords + ones[int(string[0]) - 1]
inWords = inWords + "and"
# tens
if not string[1] == 1:
inWords = inWords + tens[int(string[1]) - 1]
inWords = inWords + ones[int(string[2]) - 1]
return inWords
else:
if string[2] > 5:
inWords = inWords + ones[int(string[2]) - 1]
inWords = inWords + tens[0]
return inWords
else:
if string[2] == 1:
inWords = inWords + "eleven"
elif string[2] == 2:
inWords = inWords + "twelve"
elif string[2] == 3:
inWords = inWords + "thirteen"
elif string[2] == 4:
inWords = inWords + "fourteen"
else:
inWords = inWords + "fifteen"
return inWords
elif len(string) == 2:
# tens
if not string[0] == 1:
inWords = inWords + tens[int(string[0]) - 1]
inWords = inWords + ones[int(string[0]) - 1]
return inWords
else:
if string[1] > 5:
inWords = inWords + ones[int(string[1]) - 1]
inWords = inWords + tens[0]
return inWords
else:
if string[1] == 1:
inWords = inWords + "eleven"
elif string[1] == 2:
inWords = inWords + "twelve"
elif string[1] == 3:
inWords = inWords + "thirteen"
elif string[1] == 4:
inWords = inWords + "fourteen"
else:
inWords = inWords + "fifteen"
return inWords
else:
inWords = inWords + ones[int(string[0]) - 1]
return inWords
upperRange = 1000
total = ""
for i in range(1, upperRange+1):
total = total + length(i)
print(len(total))