Python implements the method of converting Roman numerals into ordinary Arabic numerals

  • 2020-05-30 20:28:42
  • OfStack

This article illustrates an example of how Python converts Roman numerals into ordinary Arabic numerals. I will share it with you for your reference as follows:

Roman numerals, we have seen in some TV or real life, recently, when learning Python, I also encountered the interpretation of Roman numerals, so I wrote a small program to practice the conversion of Roman numerals to ordinary Numbers in our daily life.

First of all, let's take a look at 1, the underlying law of Roman numerals,

In Roman numerals, seven different letters are repeated or combined to represent a variety of Numbers.

I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000

The combination relationship is:

The above characters can be used up to 3 times in a row at the same time

For example: 4, the Roman numeral should be IV, not IIII. When there are more than three combinations, the low and high combination should be used. 4 is a combination of I and V, I means 1, V means 5

Function usage:


getRomanNum('MDCCCLXXXVIII')

I get 1888

Python code:


def getRomanNum(RomanStr):
 """Roman numerals will be converted into digital,RomanStr is a RomanString"""
   import re
   if re.search('^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$',RomanStr)!=None:
     NumDic = {"pattern":"","retNum":0}
     RomanPattern = {
       "0":('','','','M'),
       "1":('CM','CD','D','C',100),
       "2":('XC','XL','L','X',10),
       "3":('IX','IV','V','I',1)
       }
     i = 3
     NumItems = sorted(RomanPattern.items())
     for RomanItem in NumItems:
       if RomanItem[0] != '0':
         patstr = NumDic["pattern"].join(['',RomanItem[1][0]])
         if re.search(patstr,RomanStr) != None:
           NumDic["retNum"] += 9*RomanItem[1][4]
           NumDic["pattern"] = patstr
         else:
           patstr = NumDic["pattern"].join(['',RomanItem[1][1]])
           if re.search(patstr,RomanStr) != None:
             NumDic["retNum"] += 4*RomanItem[1][4]
             NumDic["pattern"] = patstr
           else:
             patstr = NumDic["pattern"].join(['',RomanItem[1][2]])
             if re.search(patstr,RomanStr) != None:
               NumDic["retNum"] += 5*RomanItem[1][4]
               NumDic["pattern"] = patstr
       if NumDic["pattern"] == '':
         NumDic["pattern"] = '^'
       tempstr = ''
       sum = 0
       for k in range(0,4):
         pstr = RomanItem[1][3].join(['','{']).join(['',str(k)]).join(['','}'])
         patstr = NumDic["pattern"].join(['',pstr])
         if re.search(patstr,RomanStr) != None:
           sum = k*(10**i)
           tempstr = patstr
       if tempstr <> '':
         NumDic["pattern"] = tempstr
       else:
         NumDic["pattern"] = patstr
       NumDic['retNum'] += sum
       i -= 1
     return NumDic['retNum']
   else:
     print 'String is not a valid Roman numerals'

PS: here is another conversion tool for your reference:

Online universal unit converter flash edition:
http://tools.ofstack.com/zhuanhuanqi/all_zhuanhuanqi

For more information about Python, please check out the topics on this site: Python data structure and algorithm tutorial, Python Socket programming skills summary, Python function skills summary, Python string manipulation skills summary, Python introduction and advanced classic tutorial and Python file and directory manipulation skills summary.

I hope this article is helpful for you to design Python program.


Related articles: