Skip to content

Instantly share code, notes, and snippets.

@thiagovars
Created April 20, 2015 00:54
Show Gist options
  • Save thiagovars/3cfe02d942e019792bed to your computer and use it in GitHub Desktop.
Save thiagovars/3cfe02d942e019792bed to your computer and use it in GitHub Desktop.
Arabic to Roman
/**
* Created by thiago on 12/04/15.
*/
public class Ruler {
String[] indexRomans = {"M", "D", "C", "L", "X", "V", "I"};
int[] indexArabic = {1000, 500, 100, 50, 10, 5, 1};
public String convertToRoman(int number){
String result = "";
int index = 0;
// find index
while (indexArabic[index] <= number) {
index++;
}
while (number >= indexArabic[index]) {
result += indexRomans[index];
number -= indexArabic[index];
}
return result;
}
}
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class RulerTest {
@Test
public void TestRetornaIVQuandoRecebe4(){
Ruler arabic = new Ruler();
String roman = arabic.convertToRoman(4);
assertEquals("IV", roman);
}
@Test
public void TestRetornaIVQuandoRecebe5(){
Ruler arabic = new Ruler();
String roman = arabic.convertToRoman(5);
assertEquals("V", roman);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment