© Parineeth M R
Question 27. Convert a string having Roman numerals into an integer
The integer equivalent for the roman numerals is given below
To find the integer equivalent of a string containing roman numerals, we process the string from the rear. This simplifies the computation. If the current roman numeral is greater than the next numeral, we add the current numeral to the result. For instance consider XI. When processing X, since X is greater than I, we add 10 to the result. If the current roman numeral is less than the next numeral, then we subtract the current numeral from the result. For instance consider IX. When processing I, since I is less than X, we subtract 1 from the result.
C/C++
/*Helper function that returns the numeric value of a Roman alphabet*/ int get_numeric_value(char c) { int result; switch(c) { case 'I': result = 1; break; case 'V': result = 5; break; case 'X': result = 10; break; case 'L': result = 50; break; case 'C': result = 100; break; case 'D': result = 500; break; case 'M': result = 1000; break; default: result = 0; break; } return result; } /* Main function that converts a Roman string into an integer str1: valid input string with Roman alphabets Return value: integer equivalent of the Roman string */ int roman_to_int(char *str1) { int i, result; /*Process the string from the rear*/ i = strlen(str1) - 1; if (i < 0) return 0; result = get_numeric_value(str1[i]); --i; while (i >= 0) { int cur_value = get_numeric_value(str1[i]); int next_value = get_numeric_value(str1[i+1]); if (cur_value < next_value) result -= cur_value; else result += cur_value; --i; } return result; }
Java
/*Helper function that returns the numeric value of a Roman alphabet*/ public static int getNumericValue(char c) { int result; switch(c) { case 'I': result = 1; break; case 'V': result = 5; break; case 'X': result = 10; break; case 'L': result = 50; break; case 'C': result = 100; break; case 'D': result = 500; break; case 'M': result = 1000; break; default: result = 0; break; } return result; } /* Main function that converts a Roman string into an integer str1: valid input string with Roman alphabets Return value: integer equivalent of the Roman string */ public static int romanToInt(char[] str1) { /*Process the string from the rear*/ int i = str1.length - 1; if (i < 0) return 0; int result = getNumericValue(str1[i]); --i; while (i >= 0) { int curDigitVal = getNumericValue(str1[i]); int nextDigitVal = getNumericValue(str1[i+1]); if (curDigitVal < nextDigitVal) result -= curDigitVal; else result += curDigitVal; --i; } return result; }
Python
#str1: valid input string with Roman alphabets #Return value: integer equivalent of the Roman string def roman_to_int(str1) : table = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} i = len(str1) – 1 #Process the string from the rear if (i < 0): return 0 result = table[str1[i]] i -= 1 while (i >= 0) : cur_digit_val = table[str1[i]] next_digit_val = table[str1[i+1]] if (cur_digit_val < next_digit_val): result -= cur_digit_val else: result += cur_digit_val i -= 1 return result