-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project_17.py
executable file
·133 lines (106 loc) · 2.99 KB
/
Project_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python
import math
def getThousands(i):
orig = i
i -= (i % 1000)
# Remove everything but the thousands place
i /= 1000
# Divide to get the raw number of thousands
if (orig % 1000) == 0:
return getOnes(i) + " thousand"
else:
return getOnes(i) + " thousand "
# End if/else block
# End def
def getHundreds(i):
orig = i
i -= (i % 100)
# Remove everything but the hundreds place
i /= 100
# Divide to get the raw number of hundreds
if (orig % 100) == 0:
return getOnes(i) + " hundred"
else:
return getOnes(i) + " hundred and "
# Adding the "and" here is very important
# End if/else block
# End def
def getTens(i):
table = {
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: "fourty",
50: "fifty",
60: "sixty",
70: "seventy",
80: "eighty",
90: "ninty"
}
if i in table:
return (table[i], False)
else:
i -= (i % 10)
# Remove everything but the tens place
return (table[i] + "-", True)
# Adding the "-" here is very important
# End if/else block
# End def
def getOnes(i):
table = {
1: "one",
2: "two",
3: "three",
4: "four",
5: "five",
6: "six",
7: "seven",
8: "eight",
9: "nine"
}
return table[i]
# End def
def main():
_max = 1000
_min = 1
length = 0
for i in xrange(_min, _max + 1):
res = ""
cont = True
if i >= 1000:
res += getThousands(i)
i %= 1000
# End if
if i >= 100:
res += getHundreds(i)
i %= 100
# End if
if i >= 10:
tmp, cont = getTens(i)
res += tmp
i %= 10
# End if
if i != 0 and cont:
res += getOnes(i)
# End if
res = res.replace(" ", "").replace("-", "")
length += len(res)
# End for
print "The total number of letters used to write out the spellings of the numbers from %s to %s (inclusive) is: %s!" % (_min, _max, length)
# End def
if __name__ == "__main__":
main()
# End if
# Goal:
"""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."""