Skip to content
Permalink
Browse files

Fix toDecimalPlaceValues for multiples of 10

A number ending in 0 was getting a redundant PlaceValue(0,1)
at the end of the list.
  • Loading branch information
michaelbannister committed Jul 3, 2016
1 parent 5eba971 commit 8be1840719170b411f20c2b4c8fefa8591580fdd
Showing with 13 additions and 9 deletions.
  1. +5 −2 src/main/kotlin/RomanNumerals.kt
  2. +8 −7 src/test/kotlin/DecimalToRomanConverterTest.kt
@@ -10,7 +10,10 @@ fun Int.toRoman(): String {
}

fun toDecimalPlaceValues(number: Int): List<PlaceValue> {
val largestPowerNeeded = powersOfTen.find { number > it } ?: 1
if (number == 0) {
return emptyList()
}
val largestPowerNeeded = powersOfTen.find { number >= it } ?: 1
if (largestPowerNeeded == 1) {
return listOf(PlaceValue(number, 1))
} else {
@@ -31,5 +34,5 @@ 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).toString()
override fun toString(): String = "$multiplier*$unit"
}
@@ -33,13 +33,14 @@ class DecimalToRomanConverterTest {
assertEquals(placeValueRepresentation, formatPlaceValues(toDecimalPlaceValues(number)))
}

private fun parametersForTestPlaceValues() =
arrayOf(
arrayOf(9, "9"),
arrayOf(14, "10+4"),
arrayOf(4025, "4000+20+5")
)
private fun parametersForTestPlaceValues() =
arrayOf(
arrayOf(9, "9*1"),
arrayOf(14, "1*10 + 4*1"),
arrayOf(4025, "4*1000 + 2*10 + 5*1"),
arrayOf(10, "1*10")
)

private fun formatPlaceValues(placeValues: List<PlaceValue>) = placeValues.joinToString(separator = "+")
private fun formatPlaceValues(placeValues: List<PlaceValue>) = placeValues.joinToString(separator = " + ")
}

0 comments on commit 8be1840

Please sign in to comment.
You can’t perform that action at this time.