Created
December 16, 2011 09:04
-
-
Save philou/1485228 to your computer and use it in GitHub Desktop.
Randori Arab to romans @ Key Consulting 13/12/2011
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Converter { | |
public static String toRomans(int i) { | |
return getMultipleM(i/1000) + toRomans(i/100,"C","D","M") + toRomans(i/10, "X", "L", "C") + toRomans(i%10, "I", "V", "X"); | |
} | |
private static String toRomans(int i, String symbolI, String symbolV, String symbolX) { | |
int moduloRest10 = i%10; | |
if ( moduloRest10 == 0) { | |
return ""; | |
} | |
if ( moduloRest10 == 1) { | |
return symbolI; | |
} | |
if ( moduloRest10 == 4) { | |
return symbolI + symbolV; | |
} | |
if ( moduloRest10 == 5) { | |
return symbolV; | |
} | |
if ( moduloRest10 == 9) { | |
return symbolI + symbolX; | |
} | |
return toRomans( moduloRest10 - 1, symbolI, symbolV, symbolX) + symbolI; | |
} | |
private static String getMultipleM(int i) { | |
if (i == 0) { | |
return ""; | |
} | |
return "M" + getMultipleM(i-1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class ConverterTest { | |
@Test | |
public void acceptance_test() { | |
assertEquals("MMCMXXXI", Converter.toRomans(2931)); | |
} | |
@Test | |
public void roman_for_1_should_be_I() { | |
assertEquals("I", Converter.toRomans(1)); | |
} | |
@Test | |
public void roman_for_2_should_be_II() { | |
assertEquals("II", Converter.toRomans(2)); | |
} | |
@Test | |
public void roman_for_4_should_be_IV() { | |
assertEquals("IV", Converter.toRomans(4)); | |
} | |
@Test | |
public void roman_for_5_should_be_V() { | |
assertEquals("V", Converter.toRomans(5)); | |
} | |
@Test | |
public void roman_for_7_should_be_VII() { | |
assertEquals("VII", Converter.toRomans(7)); | |
} | |
@Test | |
public void roman_for_10_should_be_X() { | |
assertEquals("X", Converter.toRomans(10)); | |
} | |
@Test | |
public void roman_for_9_should_be_IX() { | |
assertEquals("IX", Converter.toRomans(9)); | |
} | |
@Test | |
public void roman_for_20_should_be_XX() { | |
assertEquals("XX", Converter.toRomans(20)); | |
} | |
@Test | |
public void roman_for_21_should_be_XXI() { | |
assertEquals("XXI", Converter.toRomans(21)); | |
} | |
@Test | |
public void roman_for_32_should_be_XXXII() { | |
assertEquals("XXXII", Converter.toRomans(32)); | |
} | |
@Test | |
public void roman_for_29_should_be_XXIX() { | |
assertEquals("XXIX", Converter.toRomans(29)); | |
} | |
@Test | |
public void roman_for_50_should_be_L() { | |
assertEquals("L", Converter.toRomans(50)); | |
} | |
@Test | |
public void roman_for_74_should_be_LXXIV() { | |
assertEquals("LXXIV", Converter.toRomans(74)); | |
} | |
@Test | |
public void roman_for_90_should_be_XC() { | |
assertEquals("XC", Converter.toRomans(90)); | |
} | |
@Test | |
public void roman_for_100_should_be_C() { | |
assertEquals("C", Converter.toRomans(100)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* on pensait plus tot à la structure de la solution | |
* on y est allé petit à petit, en se disant "voyons où ça nous mène" | |
* on est parti du bon côté dès le début | |
* + adapté que le Kata Bowling : réflection préliminaire moins indispensable | |
* le fait de tourner : c'est comme dans un vrai projet | |
* + on a réussi ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment