Sha256: 5f4d8ccfb0cdafaec073abc032bb63015fbb4b106be5583802d85086e6a395ce

Contents?: true

Size: 689 Bytes

Versions: 396

Compression:

Stored size: 689 Bytes

Contents

object Atbash {
    private val GROUP_SIZE = 5

    fun encode(s: String): String = cipherSubstitution(s).mapIndexed { index, char -> char + groupFinalizer(index) }.joinToString("").trimEnd()

    fun decode(s: String): String = cipherSubstitution(s)

    private fun cipherSubstitution(s: String): String = s.fold("", { accum, char -> accum + substitute(char) })

    private fun substitute(c: Char): String {
        return when {
            c.isDigit() -> c.toString()
            c.isLetter() -> ('a' + ('z' - c.toLowerCase())).toString()
            else -> ""
        }
    }

    private fun groupFinalizer(index: Int): String = if ((index + 1) % GROUP_SIZE == 0) " " else ""



}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.97 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.96 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.95 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.94 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.93 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.92 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.91 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.90 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.89 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.88 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.87 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.86 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.85 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.84 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.83 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.82 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.81 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.80 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.79 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt