|
@@ -3,7 +3,7 @@ private val powersOfTen = listOf(1000, 100, 10, 1) |
|
|
fun Int.toRoman(): String { |
|
|
val placeValues = toDecimalPlaceValues(this) |
|
|
return placeValues.map { |
|
|
it.unit.toRomanDigit().repeat(it.multiplier) |
|
|
it.magnitude.toRomanDigit().repeat(it.multiplier) |
|
|
}.joinToString(separator = "") |
|
|
} |
|
|
|
|
@@ -13,11 +13,11 @@ fun toDecimalPlaceValues(number: Int): List<PlaceValue> { |
|
|
} |
|
|
val largestPowerNeeded = powersOfTen.find { number >= it } ?: 1 |
|
|
if (largestPowerNeeded == 1) { |
|
|
return listOf(PlaceValue(number, 1)) |
|
|
return listOf(PlaceValue(multiplier = number, magnitude = 1)) |
|
|
} else { |
|
|
val multiplier = number / largestPowerNeeded |
|
|
val remainder = number % largestPowerNeeded |
|
|
return listOf(PlaceValue(multiplier, largestPowerNeeded)) + toDecimalPlaceValues(remainder) |
|
|
return listOf(PlaceValue(multiplier, magnitude = largestPowerNeeded)) + toDecimalPlaceValues(remainder) |
|
|
} |
|
|
} |
|
|
|
|
@@ -31,6 +31,6 @@ private fun Int.toRomanDigit(): String = when(this) { |
|
|
|
|
|
internal class RomanNumeralException(message: String) : RuntimeException(message) |
|
|
|
|
|
data class PlaceValue(val multiplier: Int, val unit: Int) { |
|
|
override fun toString(): String = "$multiplier*$unit" |
|
|
data class PlaceValue(val multiplier: Int, val magnitude: Int) { |
|
|
override fun toString(): String = "$multiplier*$magnitude" |
|
|
}
|
0 comments on commit
bdc01e4