Advertisement
Abhisek92

Roman

Apr 28th, 2023 (edited)
872
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import re
  2.  
  3. class Solution(object):
  4.     def __init__(self):
  5.         self.pattern = re.compile(r"^(M*)((CM)|(CD)|(DC{0,3})|(C{0,3}))((XC)|(XL)|(LX{0,3})|(X{0,3}))((IX)|(IV)|(VI{0,3})|(I{0,3}))$")
  6.  
  7.     def romanToInt(self, s):
  8.         groups = re.search(self.pattern, s).groups()
  9.         m = 1000 * len(groups[0])
  10.  
  11.         c1 = 900 if groups[2] else 0
  12.         c2 = 400 if groups[3] else 0
  13.         c3 = 500 + (100 * (len(groups[4]) - 1)) if groups[4] else 0
  14.         c4 = 100 * len(groups[5]) if groups[5] else 0
  15.  
  16.         x1 = 90 if groups[7] else 0
  17.         x2 = 40 if groups[8] else 0
  18.         x3 = 50 + (10 * (len(groups[9]) - 1)) if groups[9] else 0
  19.         x4 = 10 * len(groups[10]) if groups[10] else 0
  20.  
  21.         v1 = 9 if groups[12] else 0
  22.         v2 = 4 if groups[13] else 0
  23.         v3 = 5 + (len(groups[14]) - 1) if groups[14] else 0
  24.         v4 = len(groups[15]) if groups[15] else 0
  25.  
  26.         return m + c1 + c2 + c3 + c4 + x1 + x2 + x3 + x4 + v1 + v2 + v3 + v4
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement