-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoman-to-integer.py
47 lines (35 loc) · 1.22 KB
/
Roman-to-integer.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
class PySolutions:
def roman_to_int(self, rnum):
symb = ["IV", "IX", "XL", "XC",
"CD", "CM","I", "V", "X",
"L", "C", "D", "M"]
nums = [4, 9, 40, 90, 400, 900,
1, 5, 10, 50, 100, 500, 1000]
number = int()
rnum = rnum.upper()
for i in range(0,6):
if symb[i] in rnum:
number += nums[i]
rnum = rnum.replace(symb[i], "")
for i in range(6,13):
x = rnum.count(symb[i]) # x is number of a character in the string
number += nums[i]*x
return number
def int_to_roman(self, num2):
symb2 = ["I", "IV", "V", "IX",
"X", "XL","L", "XC",
"C", "CD", "D", "CM", "M"]
nums2 = [1, 4, 5, 9, 10, 40, 50,
90, 100, 400, 500, 900, 1000]
roman = ""
for i in range(1,14):
x = num2 // nums2[-i]
num2 -= nums2[-i]*x
roman += symb2[-i]*x
return roman
while True:
rnum = input("Type in roman number: ")
num2 = input("Type in a number: ")
num2 = int(num2)
print(PySolutions().roman_to_int(rnum))
print(PySolutions().int_to_roman(num2))