Python Forum
Help with implementation of the logic to convert roman numeral to integer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with implementation of the logic to convert roman numeral to integer
#1
I am trying to convert the given roman numerals to just numerals, with the following logic :
Here,
M - 1000, C-100, X-10, V-5, I-1

example :
R_num- MCMXCVI
index- 1234567

logic - 1000 + (1000-100) + (100-10) + 5 + 6
index- 1 + (3-2) + (5-4) + 6 + 7
Here i am searching next value from the current value subtracting it if its not greater we are adding it normally.

Having hard time to think properly in python. Please help.

def roman_numeral(num):
    """
    Write a Python class to convert an roman numeral to a integer.
    Logic: https://www.rapidtables.com/convert/number/how-roman-numerals-to-number.html
    """
    # Input the string 
    # Map of roman numerals and the corresponding values in a dictionary.
    NUMERALS = {1000:'M', 900:'CM', 500:'D', 400:'CD', 100:'C', 90:'XC',
                50:'L', 40:'XL', 10:'X', 9:'IX', 5:'V', 4:'IV', 1:'I'}
    retval=[]
    
    #Check if each char matches with the dictionary and take the numerical value of the inputed roman 
    
    for k in range(len(num)):
        for i,j in NUMERALS.items():
            if(j==num[k]):
                retval.append(i)
                
    elm_count = len(retval)       
    result=0 
    result_less=0
    result_more=0
    ind_tracker=0
    
#Check if next char from the position of current char if that numerical value is greater then current numerical value.
#If it is greater subtract the current numeric value, if not greater then add it.    
    for ind,i in enumerate(retval):
        print('ind= ',ind,'i= ', i)
#Using this below condition to skip if we have already subtracted the current value from previous value.
        if( ind_tracker>ind):
            continue
        if((ind+1 < elm_count)):
                if(i<retval[ind+1]):
                    #print('result=',result,'retval[ind]=',retval[ind],'retval[ind+1]=', retval[ind+1])
                    result_less=retval[ind+1]-retval[ind]
                    print('result_less=',result_less)
                    ind_tracker=ind+1
                else:
                    result_more+=retval[ind]+result_less
                    print('result_more=',result_more)
                    
                    result=result_more   
    print('final result= ',result)    
    return result

roman_numeral('MCMXCVI')
Reply
#2
Shouldn't this be added to previous thread?
https://python-forum.io/Thread-Convertin...n-numerals
Reply
#3
This is the backward operation. Previous thread was converting decimal value to Roman Numerals.

You are having a hard time thinking of a Python solution because you do not have a clear general solution.

The way I think Roman Numerals work is when a lower value numeral appears left of a higher value numeral you do subtraction, else you do addition. This is easiest done right to left. Let's see if that works.

MCMXCVI

List the numerals so the rightmost numeral is at the top and the list and the leftmost at the bottom. To the right of each numeral write the decimal equivalent. Put a minus sign in front of the value if the value is less than the value above. Add all the values to get the decimal equivalent of the Roman Numeral.
Num Val   Sum
          0
I   1     1
V   5     6
C   100   106
X   -10   96
M   1000  1096
C   -100  996
M   1000  1996
To convert this to words:
sum = 0
previous_value = 0
for each numeral in the roman numeral starting from the right and going left
   value = decimal value of numeral
   if value < previous_value
       sum = sum - value
   else
       sum = sum + value
   previos_value = value
I think that looks pretty easy to translate into Python.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert 4 bytes to an integer ? GiggsB 11 6,811 Jan-20-2022, 03:37 AM
Last Post: GiggsB
  Converting number to roman numerals jagasrik 6 3,590 Aug-11-2020, 03:47 AM
Last Post: voidptr
  Roman Numeral Ordered List? (first post) CopperyMarrow15 1 1,719 Nov-06-2019, 11:06 PM
Last Post: Larz60+
  implementation fuzzy logic using sckit fuzzy nana_4895 0 2,086 Oct-21-2019, 03:28 AM
Last Post: nana_4895
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,134 Aug-06-2018, 05:19 PM
Last Post: vanicci
  Convert integer to string Maigol 5 5,501 Mar-05-2018, 10:25 PM
Last Post: python_master89
  cannot convert float infinity to integer error despite rounding off floats Afterdarkreader 3 15,198 Dec-15-2017, 02:01 AM
Last Post: micseydel
  How to convert character to integer in python rajeev1729 2 3,835 Oct-03-2017, 04:30 PM
Last Post: nilamo
  Logic to convert an integer to YEAR / MONTH hugobaur 9 7,893 Oct-18-2016, 11:58 AM
Last Post: hugobaur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020