Skip to content

Commit 8b25ea5

Browse files
author
Aaryan Trivedi
authored
Create RomanToInt.py
1 parent f3dabfe commit 8b25ea5

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

RomanToInt.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
val = input("Enter your value: ")
2+
class Solution(object):
3+
def romanToInt(self, s):
4+
"""
5+
:type s: str
6+
:rtype: int
7+
"""
8+
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}
9+
i = 0
10+
num = 0
11+
while i < len(s):
12+
if i+1<len(s) and s[i:i+2] in roman:
13+
num+=roman[s[i:i+2]]
14+
i+=2
15+
else:
16+
#print(i)
17+
num+=roman[s[i]]
18+
i+=1
19+
return num
20+
ob1 = Solution()
21+
print(ob1.romanToInt(val))

0 commit comments

Comments
 (0)