diff --git a/src/main/kotlin/RomanNumerals.kt b/src/main/kotlin/RomanNumerals.kt index 9c45744..6eeb512 100644 --- a/src/main/kotlin/RomanNumerals.kt +++ b/src/main/kotlin/RomanNumerals.kt @@ -1,11 +1,21 @@ fun Int.toRoman(): String { - if (this >= 1000) { - return "M".repeat(this / 1000) - } else if (this >= 100) { - return "C".repeat(this / 100) - } else if (this >= 10) { - return "X".repeat(this / 10) - } else { - return "I".repeat(this) + val powersOfTen = listOf(1000, 100, 10, 1) + powersOfTen.forEach { unit -> + if (this >= unit) { + return symbolFor(unit).repeat(this / unit) + } } -} \ No newline at end of file + throw RomanNumeralException("Can't yet convert ${this} to Roman numerals") +} + +fun symbolFor(unit: Int): String { + return when(unit) { + 1000 -> "M" + 100 -> "C" + 10 -> "X" + 1 -> "I" + else -> throw RomanNumeralException("No mapping to symbol for ${unit}") + } +} + +class RomanNumeralException(message: String) : RuntimeException(message) \ No newline at end of file