-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.py
268 lines (220 loc) · 6.38 KB
/
func.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
"""
Functions to anglicize integers in the range 1 -> 999,999
The primary function in this module is anglicize(). This is a great module to help understand preconditions
Author: Michael Dickey
Date: Dec 23, 2021
"""
import random
def anglicize(n):
"""
Returns: a string representation of the integer n in English
Examples:
3: 'three'
45: 'forty five'
100: 'one hundred'
127: 'one hundred and twenty seven'
1001: 'one thousand and one'
990899: 'nine hundred and ninety eight thousand nine hundred and ninety nine'
Paramter: the intereger to anglicize
Precondition: n is an integer in the range 0 <= n < 1,000,000
"""
number_string = ''
def anglicize0to9(n):
"""
returns the neglish equivalent of single digit integer 0-9
"""
if n == 0:
return 'zero'
if n == 1:
return 'one'
if n == 2:
return 'two'
if n == 3:
return 'three'
if n == 4:
return 'four'
if n == 5:
return 'five'
if n == 6:
return 'six'
if n == 7:
return 'seven'
if n == 8:
return 'eight'
if n == 9:
return 'nine'
def anglicize10to19(n):
if n == 10:
return 'ten'
if n == 11:
return 'eleven'
if n == 12:
return 'twelve'
if n == 13:
return 'thirteen'
if n == 14:
return 'fourteen'
if n == 15:
return 'fifteen'
if n == 16:
return 'sixteen'
if n == 17:
return 'seventeen'
if n == 18:
return 'eighteen'
if n == 19:
return 'nineteen'
def anglicize20to99(n):
if n // 10 == 2:
if n % 10 > 0:
return 'twenty ' + anglicize0to9(n % 10)
else:
return 'twenty'
if n // 10 == 3:
if n % 10 > 0:
return 'thirty ' + anglicize0to9(n % 10)
else:
return 'thirty'
if n // 10 == 4:
if n % 10 > 0:
return 'forty ' + anglicize0to9(n % 10)
else:
return 'forty'
if n // 10 == 5:
if n % 10 > 0:
return 'fifty ' + anglicize0to9(n % 10)
else:
return 'fifty'
if n // 10 == 6:
if n % 10 > 0:
return 'sixty ' + anglicize0to9(n % 10)
else:
return 'sixty'
if n // 10 == 7:
if n % 10 > 0:
return 'seventy ' + anglicize0to9(n % 10)
else:
return 'seventy'
if n // 10 == 8:
if n % 10 > 0:
return 'eighty ' + anglicize0to9(n % 10)
else:
return 'eighty'
if n // 10 == 9:
if n % 10 > 0:
return 'ninety ' + anglicize0to9(n % 10)
else:
return 'ninety'
def anglicize0to99(n):
# groups the above two functions.
if n < 10:
return anglicize0to9(n)
elif n >= 10 and n < 20:
return anglicize10to19(n)
else:
return anglicize20to99(n)
def anglicize100to999(n):
if n // 100 == 1:
if n % 100 > 0:
return 'one hundred ' + anglicize0to99(n % 100)
else:
return 'one hundred'
if n // 100 == 2:
if n % 100 > 0:
return 'two hundred ' + anglicize0to99(n % 100)
else:
return 'two hundred'
if n // 100 == 3:
if n % 100 > 0:
return 'three hundred ' + anglicize0to99(n % 100)
else:
return 'three hundred'
if n // 100 == 4:
if n % 100 > 0:
return 'four hundred ' + anglicize0to99(n % 100)
else:
return 'four hundred'
if n // 100 == 5:
if n % 100 > 0:
return 'five hundred ' + anglicize0to99(n % 100)
else:
return 'five hundred'
if n // 100 == 6:
if n % 100 > 0:
return 'six hundred ' + anglicize0to99(n % 100)
else:
return 'six hundred'
if n // 100 == 7:
if n % 100 > 0:
return 'seven hundred ' + anglicize0to99(n % 100)
else:
return 'seven hundred'
if n // 100 == 8:
if n % 100 > 0:
return 'eight hundred ' + anglicize0to99(n % 100)
else:
return 'eight hundred'
if n // 100 == 9:
if n % 100 > 0:
return 'nine hundred ' + anglicize0to99(n % 100)
else:
return 'nine hundred'
def anglicize0to999(n):
if n < 100:
return anglicize0to99(n)
else:
return anglicize100to999(n)
# call the appropriate sub function(s)
if n < 10:
number_string = anglicize0to9(n)
return number_string
if n >= 10 and n < 20:
number_string = anglicize10to19(n)
return number_string
if n >= 20 and n < 100:
number_string = anglicize20to99(n)
return number_string
if n >= 100 and n < 1000:
number_string = anglicize100to999(n)
return number_string
if n >= 1000 and n < 9999:
number_string = anglicize0to9(n // 1000) + ' thousand '
if n % 1000 == 0:
return number_string
else:
number_string = number_string + anglicize0to999(n % 1000)
return number_string
#dev test cases
n = random.randint(0,9)
print('n is: ', n)
print(anglicize(n))
n = random.randint(10,19)
print('n is: ', n)
print(anglicize(n))
n = random.randint(20,99)
print('n is: ', n)
print(anglicize(n))
n = 100
print('n is: ', n)
print(anglicize(n))
n = 108
print('n is: ', n)
print(anglicize(n))
n = 118
print('n is: ', n)
print(anglicize(n))
n = random.randint(100,199)
print('n is: ', n)
print(anglicize(n))
n = random.randint(100,999)
print('n is: ', n)
print(anglicize(n))
n = 1000
print('n is: ', n)
print(anglicize(n))
n = 6000
print('n is: ', n)
print(anglicize(n))
n = random.randint(1000,9999)
print('n is: ', n)
print(anglicize(n))