Published at Sep 26 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.

- 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.`

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

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

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.

Wikipedia https://en.wikipedia.org/wiki/Caesar_cipher

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

```
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{
var _key: Int
constructor(key: Int) {
_key= key
}
fun encode(sentance: String): String = sentance.map {
when(it){
in 'a'..'z' -> 'a' + (((it + _key) - 'a')%26)
in 'A'..'Z'-> 'A' + (((it + _key) - 'A')%26)
else -> it
}
}.joinToString ("")
}
```

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?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments