# araknoid's solution

## to Rotational Cipher in the Kotlin Track

Published at Jun 03 2019 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

## Setup

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

## Making the test suite pass

Execute the tests with:

``````\$ gradlew test
``````

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore` annotation.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### RotationalCipherTest.kt

``````import org.junit.Ignore
import org.junit.Test
import kotlin.test.assertEquals

class RotationalCipherTest {

@Test
fun testRotateLowercaseABy0() {
val cipher = RotationalCipher(0)
assertEquals("a", cipher.encode("a"))
}

@Ignore
@Test
fun testRotateLowercaseABy1NoWrapAround() {
val cipher = RotationalCipher(1)
assertEquals("b", cipher.encode("a"))
}

@Ignore
@Test
fun testRotateLowercaseABy26SingleWrapAround() {
val cipher = RotationalCipher(26)
assertEquals("a", cipher.encode("a"))
}

@Ignore
@Test
fun testRotateLowercaseMBy13NoWrapAround() {
val cipher = RotationalCipher(13)
assertEquals("z", cipher.encode("m"))
}

@Ignore
@Test
fun testRotateLowercaseNBy1SingleWrapAround() {
val cipher = RotationalCipher(13)
assertEquals("a", cipher.encode("n"))
}

@Ignore
@Test
fun testRotateCapitalLettersNoWrapAround() {
val cipher = RotationalCipher(5)
assertEquals("TRL", cipher.encode("OMG"))
}

@Ignore
@Test
fun testSpacesAreUnalteredByRotation() {
val cipher = RotationalCipher(5)
assertEquals("T R L", cipher.encode("O M G"))
}

@Ignore
@Test
fun testNumbersAreUnalteredByRotation() {
val cipher = RotationalCipher(4)
assertEquals("Xiwxmrk 1 2 3 xiwxmrk", cipher.encode("Testing 1 2 3 testing"))
}

@Ignore
@Test
fun testPunctuationIsUnalteredByRotation() {
val cipher = RotationalCipher(21)
assertEquals("Gzo'n zvo, Bmviyhv!", cipher.encode("Let's eat, Grandma!"))
}

@Ignore
@Test
fun testAllLettersRotateCorrectly() {
val cipher = RotationalCipher(13)
assertEquals(
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt.",
cipher.encode("The quick brown fox jumps over the lazy dog."))
}

}``````
``````class RotationalCipher(private val shift: Int) {
fun encode(string: String) = string.asSequence()
.map {
when {
it.isUpperCase() -> it.shiftUpperCaseBy(shift)
it.isLowerCase() -> it.shiftLowerCaseBy(shift)
else -> it
}
}
.joinToString("")
}

private fun Char.shiftLowerCaseBy(shift: Int) = this.shiftBy(shift, 'a')

private fun Char.shiftUpperCaseBy(shift: Int) = this.shiftBy(shift, 'A')

private fun Char.shiftBy(shift: Int, startingChar: Char) = when {
this - startingChar + shift >= 26 -> startingChar + (this - startingChar + shift - 26)
else -> this + shift
}``````

### What can you learn from this solution?

A huge amount can be learned from reading other peopleโs code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?