-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman.py
300 lines (261 loc) · 8.39 KB
/
roman.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
#roman.py
from math import *
class Roman:
"""
Roman Class
"""
def __init__(self, x):
"""
Roman Class Constructor. Roman(x)
x is an integer that's absolute value is less than 2,000,000
"""
if(type(x) != int):
raise ValueError("For Roman(x), x MUST be an integer")
if(2000000 < abs(x)):
raise ValueError("For Roman(x), abs(x) cannot be more than 2 million")
self.dataInt = x
self.dataString = self.intToRoman(x)
def __repr__(self):
return "Roman(" + str(self.dataInt) + ")"
def __str__(self):
return self.dataString
def intToRoman(self, x):
temp = x
s = ""
#Case where temp = 0
if(temp == 0):
return "N"
#Account for negative
if(temp < 0):
s += "-"
temp = -1 * temp
#Start building Roman Numeral here
while(temp >= 1000000):
s += "(M)"
temp -= 1000000
while(temp >= 900000):
s += "(C)(M)"
temp -= 900000
while(temp >= 500000):
s += "(D)"
temp -= 500000
while(temp >= 400000):
s += "(C)(D)"
temp -= 400000
while(temp >= 100000):
s += "(C)"
temp -= 100000
while(temp >= 90000):
s += "(X)(C)"
temp -= 90000
while(temp >= 50000):
s += "(L)"
temp -= 50000
while(temp >= 40000):
s += "(X)(L)"
temp -= 40000
while(temp >= 10000):
s += "(X)"
temp -= 10000
while(temp >= 9000):
s += "M(X)"
temp -= 9000
while(temp >= 5000):
s += "(V)"
temp -= 5000
while(temp >= 4000):
s += "M(V)"
temp -= 4000
while(temp >= 1000):
s += "M"
temp -= 1000
while(temp >= 900):
s += "CM"
temp -= 900
while(temp >= 500):
s += "D"
temp -= 500
while(temp >= 400):
s += "CD"
temp -= 400
while(temp >= 100):
s += "C"
temp -= 100
while(temp >= 90):
s += "XC"
temp -= 90
while(temp >= 50):
s += "L"
temp -= 50
while(temp >= 40):
s += "XL"
temp -= 40
while(temp >= 10):
s += "X"
temp -= 10
while(temp >= 9):
s += "IX"
temp -= 9
while(temp >= 5):
s += "V"
temp -= 5
while(temp >= 4):
s += "IV"
temp -= 4
while(temp >= 1 ):
s += "I"
temp -= 1
return s
def __add__(self, other):
if(type(other) == Roman):
return Roman(self.dataInt + other.dataInt)
elif(type(other) == int):
return Roman(self.dataInt + other)
else:
raise ValueError("+ used incorrectly")
def __radd__(self, other):
return self.__add__(other)
def __sub__(self, other):
if(type(other) == Roman):
return Roman(self.dataInt - other.dataInt)
elif(type(other) == int):
return Roman(self.dataInt - other)
else:
raise ValueError("- used incorrectly")
def __rsub__(self, other):
if(type(other) == Roman):
return Roman(other.dataInt - self.dataInt)
elif(type(other) == int):
return Roman(other - self.dataInt)
else:
raise ValueError("- used incorrectly")
def __mul__(self, other):
if(type(other) == Roman):
return Roman(self.dataInt * other.dataInt)
elif(type(other) == int):
return Roman(self.dataInt * other)
else:
raise ValueError("* used incorrectly")
def __rmul__(self, other):
return self.__mul__(other)
def __floordiv__(self, other):
if(type(other) == Roman):
return Roman(self.dataInt // other.dataInt)
elif(type(other) == int):
return Roman(self.dataInt // other)
else:
raise ValueError("// used incorrectly")
def __rfloordiv__(self, other):
if(type(other) == Roman):
return Roman(other.dataInt // self.dataInt)
elif(type(other) == int):
return Roman(other // self.dataInt)
else:
raise ValueError("// used incorrectly")
def __truediv__(self, other):
if(type(other) == Roman):
return (Roman(self.dataInt // other.dataInt), Roman(self.dataInt % other.dataInt))
elif(type(other) == int):
return (Roman(self.dataInt // other), Roman(self.dataInt % other))
else:
raise ValueError("/ used incorrectly")
def __rtruediv__(self, other):
if(type(other) == Roman):
return (Roman(other.dataInt // self.dataInt), Roman(other.dataInt % self.dataInt))
elif(type(other) == int):
return (Roman(other // self.dataInt), Roman(other % self.dataInt))
else:
raise ValueError("/ used incorrectly")
def __pow__(self, other):
if(type(other) == Roman):
return Roman(self.dataInt ** other.dataInt)
elif(type(other) == int):
return Roman(self.dataInt ** other)
else:
raise ValueError("** used incorrectly")
def __rpow__(self, other):
if(type(other) == Roman):
return Roman(other.dataInt ** self.dataInt)
elif(type(other) == int):
return Roman(other ** self.dataInt)
else:
raise ValueError("** used incorrectly")
def __eq__(self, other):
if(type(other) == Roman):
if(self.dataInt == other.dataInt):
return True
else:
return False
elif(type(other) == int):
if(self.dataInt == other):
return True
else:
return False
else:
raise ValueError("== used incorrectly")
def __ne__(self, other):
if(type(other) == Roman):
if(self.dataInt != other.dataInt): return True
else: return False
elif(type(other) == int):
if(self.dataInt != other): return True
else: return False
else:
raise ValueError("!= used incorrectly")
def __lt__(self, other):
if(type(other) == Roman):
if(self.dataInt < other.dataInt):
return True
else:
return False
elif(type(other) == int):
if(self.dataInt < other):
return True
else:
return False
else:
raise ValueError("< used incorrectly")
def __le__(self, other):
if(type(other) == Roman):
if(self.dataInt <= other.dataInt):
return True
else:
return False
elif(type(other) == int):
if(self.dataInt <= other):
return True
else:
return False
else:
raise ValueError("<= used incorrectly")
def __ge__(self, other):
if(type(other) == Roman):
if(self.dataInt >= other.dataInt):
return True
else:
return False
elif(type(other) == int):
if(self.dataInt >= other):
return True
else:
return False
else:
raise ValueError(">= used incorrectly")
def __gt__(self, other):
if(type(other) == Roman):
if(self.dataInt > other.dataInt):
return True
else:
return False
elif(type(other) == int):
if(self.dataInt > other):
return True
else:
return False
else:
raise ValueError("> used incorrectly")
def __neg__(self):
return Roman(-1 * self.dataInt)
for i in range(0, 1001, 1):
G = globals()
G[Roman(i).dataString] = Roman(i)