-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroman_numbers_recognizer.py
308 lines (283 loc) · 9.24 KB
/
roman_numbers_recognizer.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
This program recognizes roman numbers from 1 to 3999.
'''
class RomanNumbersRecognizer(object):
"""
Represents a class that recognizes roman numbers from 1 to 3999.
"""
def __init__(self):
self.text = None
self.token_index = 0
def recognize(self, input_text):
"""
:param string input_text: The text to be processed.
:return: A number, if the string translated to a valid number;
or None, if the string was invalid.
Initializes the recognition of an input text.
"""
self.text = input_text
self.token_index = 0
result = self.check_thousands() + self.check_hundreds() + \
self.check_decimals() + self.check_ones()
return result if self.token_index >= len(self.text) else None
def get_next_token(self):
"""
:return: A string character; or None, if there aren't any characters.
Gets the next token.
"""
return self.text[self.token_index]\
if self.token_index < len(self.text) else None
def check_thousands(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of characters that represent algarisms in
the thousands place (M).
"""
result = None
if self.get_next_token() == 'M':
self.token_index += 1
result = 1000
result += self.check_more_m()
return result
return 0
def check_more_m(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of M characters.
"""
result = None
if self.get_next_token() == 'M':
self.token_index += 1
result = 1000
result += self.check_yet_more_m()
return result
return 0
def check_yet_more_m(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of M characters.
"""
if self.get_next_token() == 'M':
self.token_index += 1
return 1000
return 0
def check_hundreds(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of characters that represent algarisms in
the hundreds place (C, D and M).
"""
result = None
if self.get_next_token() == 'C':
self.token_index += 1
result = 100
if self.get_next_token() == 'M':
self.token_index += 1
return 900
else:
result += self.check_more_d_c()
return result
elif self.get_next_token() == 'D':
self.token_index += 1
result = 500
result += self.check_more_c()
return result
return 0
def check_more_d_c(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of D and C characters.
"""
result = None
if self.get_next_token() == 'D':
self.token_index += 1
result = 300
result += self.check_more_c()
return result
elif self.get_next_token() == 'C':
self.token_index += 1
result = 100
result += self.check_yet_even_more_c()
return result
return 0
def check_more_c(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of C characters.
"""
result = None
if self.get_next_token() == 'C':
self.token_index += 1
result = 100
result += self.check_even_more_c()
return result
return 0
def check_even_more_c(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of C characters.
"""
result = None
if self.get_next_token() == 'C':
self.token_index += 1
result = 100
result += self.check_yet_even_more_c()
return result
return 0
def check_yet_even_more_c(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of C characters.
"""
if self.get_next_token() == 'C':
self.token_index += 1
return 100
return 0
def check_decimals(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of characters that represent algarisms in
the decimals place (X, L and C).
"""
result = None
if self.get_next_token() == 'X':
self.token_index += 1
result = 10
result += self.check_more_x_l_c()
return result
elif self.get_next_token() == 'L':
self.token_index += 1
result = 50
result += self.check_more_x()
return result
return 0
def check_more_x_l_c(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of X, L and C characters.
"""
result = None
if self.get_next_token() == 'X':
self.token_index += 1
result = 10
result += self.check_yet_even_more_x()
return result
elif self.get_next_token() == 'L':
self.token_index += 1
result = 30
return result
elif self.get_next_token() == 'C':
self.token_index += 1
return 80
return 0
def check_more_x(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of X characters.
"""
result = None
if self.get_next_token() == 'X':
self.token_index += 1
result = 10
result += self.check_even_more_x()
return result
return 0
def check_even_more_x(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of X characters.
"""
result = None
if self.get_next_token() == 'X':
self.token_index += 1
result = 10
result += self.check_yet_even_more_x()
return result
return 0
def check_yet_even_more_x(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of X characters.
"""
if self.get_next_token() == 'X':
self.token_index += 1
return 10
return 0
def check_ones(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of characters that represent algarisms in
the ones place (I, V and X).
"""
result = None
if self.get_next_token() == 'I':
self.token_index += 1
result = 1
result += self.check_more_i_v_x()
return result
elif self.get_next_token() == 'V':
self.token_index += 1
result = 5
result += self.check_more_i()
return result
return 0
def check_more_i_v_x(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of I, V and X characters.
"""
result = None
if self.get_next_token() == 'I':
self.token_index += 1
result = 1
result += self.check_yet_even_more_i()
return result
if self.get_next_token() == 'V':
self.token_index += 1
return 3
if self.get_next_token() == 'X':
self.token_index += 1
return 8
return 0
def check_yet_even_more_i(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of I characters.
"""
if self.get_next_token() == 'I':
self.token_index += 1
return 1
return 0
def check_more_i(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of I characters.
"""
result = None
if self.get_next_token() == 'I':
self.token_index += 1
result = 1
result += self.check_even_more_i()
return result
return 0
def check_even_more_i(self):
"""
:return: A number which was the result of the checking.
Checks for the existence of I characters.
"""
result = None
if self.get_next_token() == 'I':
self.token_index += 1
result = 1
result += self.check_yet_even_more_i()
return result
return 0
if __name__ == '__main__':
RECOGNIZER = RomanNumbersRecognizer()
while True:
INPUT_TEXT = (raw_input('Write a roman number. '))
RECOGNIZEMENT = RECOGNIZER.recognize(INPUT_TEXT)
print '%s - %s' % (INPUT_TEXT, RECOGNIZEMENT) if RECOGNIZEMENT \
else '%s - %s' % (INPUT_TEXT, 'Invalid number')