Pages

Sunday, April 10, 2022

How to convert Roman numbers to Integer ?



package java8.algorithemic;

import java.util.HashMap;
import java.util.Map;

public class RomanToInteger {

    private static Map<Character, Integer> romanIntegerMap = new HashMap<>(7);

    static {
        romanIntegerMap.put('I', 1);
        romanIntegerMap.put('V', 5);
        romanIntegerMap.put('X', 10);
        romanIntegerMap.put('L', 50);
        romanIntegerMap.put('C', 100);
        romanIntegerMap.put('D', 500);
        romanIntegerMap.put('M', 1000);
    }

    public int romanToInt(String s) {
        char[] charArray = s.toCharArray();
        int intValue = 0;
        char beforeChar = '\u0000';
        for(int i = 0; i < charArray.length; i++) {
            intValue += getBaseIntegerForRoman(beforeChar, charArray[i]);
            beforeChar = charArray[i];
        }
        return intValue;
    }

    public int getBaseIntegerForRoman(char beforeChar, char currentChar) {
        if (currentChar == 'V' && beforeChar == 'I') {
            return getCurrentValue(currentChar, beforeChar);
        } if (currentChar == 'X' && beforeChar == 'I') {
            return getCurrentValue(currentChar, beforeChar);
        } else if (currentChar == 'L' && beforeChar == 'X') {
            return getCurrentValue(currentChar, beforeChar);
        } else if (currentChar == 'C' && beforeChar == 'X') {
            return getCurrentValue(currentChar, beforeChar);
        } else if (currentChar == 'D'&& beforeChar == 'C') {
            return getCurrentValue(currentChar, beforeChar);
        } else if (currentChar == 'M' && beforeChar == 'C') {
            return getCurrentValue(currentChar, beforeChar);
        } else {
            int val = getBaseIntegerForRoman(currentChar);
            return val;
        }
    }

    public int getCurrentValue(char currentChar, char beforeChar) {
        int beforeVal  = getBaseIntegerForRoman(beforeChar);
        int currentVal = getBaseIntegerForRoman(currentChar) - beforeVal;
        return currentVal - beforeVal;
    }

    public int getBaseIntegerForRoman(char ch) {
        return this.romanIntegerMap.get(ch);
    }
}


Test class as follows.

package java8.algorithemic;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class RomanToIntegerTest {
    @Test
    public void test() {
        RomanToInteger romanToInteger = new RomanToInteger();
        Assertions.assertEquals(4,romanToInteger.romanToInt("IV"));
        Assertions.assertEquals(9,romanToInteger.romanToInt("IX"));
        Assertions.assertEquals(13,romanToInteger.romanToInt("XIII"));
        Assertions.assertEquals(18,romanToInteger.romanToInt("XVIII"));
        Assertions.assertEquals(19,romanToInteger.romanToInt("XIX"));
        Assertions.assertEquals(4500,romanToInteger.romanToInt("MMMMD"));
        Assertions.assertEquals(90,romanToInteger.romanToInt("XC"));
        Assertions.assertEquals(80,romanToInteger.romanToInt("LXXX"));
    }
}

0 comments:

Post a Comment

Share

Widgets