-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathromannumbers.py
81 lines (65 loc) · 2.42 KB
/
romannumbers.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
class Solution:
def __init__(self):
self.I = 1
self.V = 5
self.X = 10
self.L = 50
self.C = 100
self.D = 500
self.M = 1000
self.counter = 0
self.list1 = []
def __str__(self) -> str:
return str(self.counter)
def romanToInt(self, s: str) -> int:
self.list1[:0] = s
self.stringiter = iter(self.list1)
for char in self.stringiter:
if char == "I":
if len(self.list1) > self.list1.index("I") + 1:
if self.list1[self.list1.index("I") + 1] == "V":
self.counter += 4
next(self.stringiter)
elif self.list1[self.list1.index("I") + 1] == "X":
self.counter += 9
next(self.stringiter)
else:
self.counter += self.I
else:
self.counter += self.I
if char == "V":
self.counter += self.V
if char == "X":
if len(self.list1) > self.list1.index("X") + 1:
if self.list1[self.list1.index("X") + 1] == "L":
self.counter += 40
next(self.stringiter)
elif self.list1[self.list1.index("X") + 1] == "C":
self.counter += 90
next(self.stringiter)
else:
self.counter += self.X
else:
self.counter += self.X
if char == "L":
self.counter += self.L
if char == "C":
if len(self.list1) > self.list1.index("C") + 1:
if self.list1[self.list1.index("C") + 1] == "D":
self.counter += 400
next(self.stringiter)
elif self.list1[self.list1.index("C") + 1] == "M":
self.counter += 900
next(self.stringiter)
else:
self.counter += self.C
else:
self.counter += self.C
if char == "D":
self.counter += self.D
if char == "M":
self.counter += self.M
return self.counter
romanclass = Solution()
print(romanclass.romanToInt("IM"))
print(romanclass)