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.180 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.179 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.178 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.177 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.176 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.175 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.174 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.173 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.172 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.171 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.170 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.169 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.167 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.166 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.165 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.164 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.163 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.162 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.161 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt
trackler-2.2.1.160 tracks/kotlin/exercises/atbash-cipher/.meta/src/reference/kotlin/Atbash.kt