Skip to content

Instantly share code, notes, and snippets.

@dlresende
Last active March 23, 2017 00:50
Show Gist options
  • Save dlresende/d2faf9f0ebb2ed781749 to your computer and use it in GitHub Desktop.
Save dlresende/d2faf9f0ebb2ed781749 to your computer and use it in GitHub Desktop.
Roman Numerals Kata

Roman Numerals Converter Kata

Write a program to convert decimals to Roman numerals. Example:

  • roman(1) = "I"
  • roman(4) = "IV"
  • roman(1954) = "MCMLIV"
  • roman(1990) = "MCMXC"

Use the following table to make the correspondence between Roman numerals and decimals:

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000
@nadir79
Copy link

nadir79 commented Mar 23, 2017

import java.util.TreeMap;

public class RomanConversion {

private final static TreeMap<Integer, String> transco = new TreeMap<Integer, String>();

static {

    transco .put(1000, "M");
    transco .put(900, "CM");
    transco .put(500, "D");
    transco .put(400, "CD");
    transco .put(100, "C");
    transco .put(90, "XC");
    transco .put(50, "L");
    transco .put(40, "XL");
    transco .put(10, "X");
    transco .put(9, "IX");
    transco .put(5, "V");
    transco .put(4, "IV");
    transco .put(1, "I");

}

public final static String ConvertRoman(int parametre) {
    int l =  transco .floorKey(number);
    if ( parametre == l ) {
        return transco.get(parametre );
    }
    return transco .get(l) + ConvertRoman(parametre -l);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment