# michaelbannister/tdd-template forked from properdom/tddWorkshop

Create function toDecimalPlaceValues

```The next test was going to be "11 is written as XI" and I realised
I would need to be able to split a number into its decimal place
values (e.g. 11 => 10 + 1, 345 => 300 + 40 + 5), so I TDD'd out this
new function.```
michaelbannister committed Jul 3, 2016
1 parent b52d09d commit bd49f8b5787e896acce0759ddb0e61175dbf2f54
Showing with 37 additions and 11 deletions.
1. +22 −11 src/main/kotlin/RomanNumerals.kt
2. +15 −0 src/test/kotlin/DecimalToRomanConverterTest.kt
 @@ -1,21 +1,32 @@ private val powersOfTen = listOf(1000, 100, 10, 1) fun Int.toRoman(): String { val powersOfTen = listOf(1000, 100, 10, 1) powersOfTen.forEach { unit -> if (this >= unit) { return symbolFor(unit).repeat(this / unit) return unit.toRomanDigit().repeat(this / unit) } } 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}") } fun toDecimalPlaceValues(number: Int): List { val largestPowerNeeded = powersOfTen.find { number > it } ?: 1 if (largestPowerNeeded == 1) { return listOf(number) } else { val multiplier = number / largestPowerNeeded val remainder = number % largestPowerNeeded val placeValueForThisPower = multiplier * largestPowerNeeded return listOf(placeValueForThisPower) + toDecimalPlaceValues(remainder) } } private fun Int.toRomanDigit(): String = when(this) { 1000 -> "M" 100 -> "C" 10 -> "X" 1 -> "I" else -> throw RomanNumeralException("No mapping to symbol for \${this}") } class RomanNumeralException(message: String) : RuntimeException(message) internal class RomanNumeralException(message: String) : RuntimeException(message)
 @@ -26,5 +26,20 @@ class DecimalToRomanConverterTest { arrayOf(1000, "M"), arrayOf(2000, "MM") ) @Test @Parameters fun testPlaceValues(number: Int, placeValueRepresentation: String) { assertEquals(placeValueRepresentation, formatPlaceValues(toDecimalPlaceValues(number))) } private fun parametersForTestPlaceValues() = arrayOf( arrayOf(9, "9"), arrayOf(14, "10+4"), arrayOf(4025, "4000+20+5") ) private fun formatPlaceValues(placeValues: List) = placeValues.joinToString(separator = "+") }