🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # katrinleinweber's solution

## to Rotational Cipher in the R Track

Published at Jul 13 2018 · 1 comment
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.`

## Installation

See this guide for instructions on how to setup your local R environment.

## How to implement your solution

In each problem folder, there is a file named `<exercise_name>.R` containing a function that returns a `NULL` value. Place your implementation inside the body of the function.

## How to run tests

Inside of RStudio, simply execute the `test_<exercise_name>.R` script. This can be conveniently done with testthat's `auto_test` function. Because exercism code and tests are in the same folder, use this same path for both `code_path` and `test_path` parameters. On the command-line, you can also run `Rscript test_<exercise_name>.R`.

## Submitting Incomplete Solutions

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

### test_rotational-cipher.R

``````source("./rotational-cipher.R")
library(testthat)

context("rotational cipher")

test_that("rotate a by 1", {
text <- "a"
key <- 1
expect_equal(rotate(text, key), "b")
})

test_that("rotate a by 26, same output as input", {
text <- "a"
key <- 26
expect_equal(rotate(text, key), "a")
})

test_that("rotate a by 0, same output as input", {
text <- "a"
key <- 0
expect_equal(rotate(text, key), "a")
})

test_that("rotate m by 13", {
text <- "m"
key <- 13
expect_equal(rotate(text, key), "z")
})

test_that("rotate n by 13 with wrap around alphabet", {
text <- "n"
key <- 13
expect_equal(rotate(text, key), "a")
})

test_that("rotate capital letters", {
text <- "OMG"
key <- 5
expect_equal(rotate(text, key), "TRL")
})

test_that("rotate spaces", {
text <- "O M G"
key <- 5
expect_equal(rotate(text, key), "T R L")
})

test_that("rotate numbers", {
text <- "Testing 1 2 3 testing"
key <- 4
expect_equal(rotate(text, key), "Xiwxmrk 1 2 3 xiwxmrk")
})

test_that("rotate punctuation", {
text <- "Let's eat, Grandma!"
key <- 21
expect_equal(rotate(text, key), "Gzo'n zvo, Bmviyhv!")
})

test_that("rotate all letters", {
text <- "The quick brown fox jumps over the lazy dog."
key <- 13
expect_equal(rotate(text, key),
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt.")
})

message("All tests passed for exercise: rotational-cipher")``````
``````library(dplyr)
library(magrittr)

rotate <- function(text, key) {

shift <- function(byte) {

# define alphabet boundaries for ASCII decimals, to enable shifting
wrap <- 26
A <- 65
Z <- 90
a <- 97
z <- 122

if      (between(byte, A, Z)) return((byte - A + key) %% wrap + A)
else-if (between(byte, a, z)) return((byte - a + key) %% wrap + a)
else    return(byte)   #   set decimal to 1 ^      reset range ^
}

# convert text to ASCII decimals
# convert back to (cipher)text
text %>%
utf8ToInt() %>%
purrr::map(shift) %>%
intToUtf8  # %>% return()

}``````

## Community comments

Find this solution interesting? Ask the author a question to learn more. Solution Author
commented over 2 years ago

Comparing this with a solution that doesn't use the magrittr pipe in `microbenchmark` shows how large the `%>%`'s performance hit is.

It looks so clean, but I guess as soon as your pipeline runs for longer than a few minutes repeatedly, investing those few minutes to refactor your code into a series of intermediately `<-`-assigned variables pays off in the long run.

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