Sha256: b345437b470c55f84d056d2cf38b614d30d64a0104e10fdf3a76cc8272620fba

Contents?: true

Size: 935 Bytes

Versions: 214

Compression:

Stored size: 935 Bytes

Contents

import java.util.*

class DiamondPrinter {

    companion object {
        private val A_INT = 'A'.toInt()

        private fun blank(length: Int): String {
            return Collections.nCopies(length, " ").joinToString("")
        }
    }

    fun printToList(chr: Char): List<String> {
        val nRows = 2 * (chr.toInt() - A_INT) + 1

        val result = mutableListOf<String>()

        // Populate the top rows.
        for (nRow in 0..(nRows + 1) / 2 - 1) {
            val rowChr = (A_INT + nRow).toChar()

            val leftHalfOfRow = blank((nRows - 1) / 2 - nRow) + rowChr + blank(nRow)
            val rightHalfOfRow = leftHalfOfRow.reversed().drop(1)
            val fullRow = "$leftHalfOfRow$rightHalfOfRow"

            result.add(fullRow)
        }

        // Populate the bottom rows by 'reflecting' all rows above the middle row.
        result.addAll(result.reversed().drop(1))

        return result
    }

}

Version data entries

214 entries across 214 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.179 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.178 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.177 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.176 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.175 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.174 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.173 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.172 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.171 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.170 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.169 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.167 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.166 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.165 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.164 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.163 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.162 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.161 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt
trackler-2.2.1.160 tracks/kotlin/exercises/diamond/.meta/src/reference/kotlin/DiamondPrinter.kt