-
Notifications
You must be signed in to change notification settings - Fork 0
/
roman integer.py
37 lines (33 loc) · 917 Bytes
/
roman 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
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
i = 0
num = 0
while i < len(s):
if i+1<len(s) and s[i:i+2] in roman:
num+=roman[s[i:i+2]]
i+=2
else:
#print(i)
num+=roman[s[i]]
i+=1
return num
ob1 = Solution()
print(ob1.romanToInt("I"))
print(ob1.romanToInt("II"))
print(ob1.romanToInt("III"))
print(ob1.romanToInt("IV"))
print(ob1.romanToInt("V"))
print(ob1.romanToInt("VI"))
print(ob1.romanToInt("VII"))
print(ob1.romanToInt("VIII"))
print(ob1.romanToInt("IX"))
print(ob1.romanToInt("X"))
print(ob1.romanToInt("L"))
print(ob1.romanToInt("C"))
print(ob1.romanToInt("D"))
print(ob1.romanToInt("M"))