ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Aug 29 2019
·
0 comments

Instructions

Test suite

Solution

There are 10 types of people in the world: Those who understand binary, and those who don't.

You and your fellow cohort of those in the "know" when it comes to binary decide to come up with a secret "handshake".

```
1 = wink
10 = double blink
100 = close your eyes
1000 = jump
10000 = Reverse the order of the operations in the secret handshake.
```

Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.

Here's a couple of examples:

Given the input 3, the function would return the array ["wink", "double blink"] because 3 is 11 in binary.

Given the input 19, the function would return the array ["double blink", "wink"] because 19 is 10011 in binary. Notice that the addition of 16 (10000 in binary) has caused the array to be reversed.

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

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.

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`

.

Bert, in Mary Poppins http://www.imdb.com/title/tt0058331/quotes/qt0437047

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

```
source("./secret-handshake.R")
library(testthat)
context("secret handshake")
test_that("wink for 1", {
expect_equal(handshake(1), c("wink"))
})
test_that("double blink for 10", {
expect_equal(handshake(2), c("double blink"))
})
test_that("close your eyes for 100", {
expect_equal(handshake(4), c("close your eyes"))
})
test_that("jump for 1000", {
expect_equal(handshake(8), c("jump"))
})
test_that("combine two actions", {
expect_equal(handshake(3), c("wink", "double blink"))
})
test_that("reverse two actions", {
expect_equal(handshake(19), c("double blink", "wink"))
})
test_that("reversing one action gives the same action", {
expect_equal(handshake(24), c("jump"))
})
test_that("reversing no actions still gives no actions", {
expect_equal(handshake(16), c())
})
test_that("all possible actions", {
expect_equal(handshake(15),
c("wink", "double blink", "close your eyes", "jump"))
})
test_that("reverse all possible actions", {
expect_equal(handshake(31),
c("jump", "close your eyes", "double blink", "wink"))
})
test_that("do nothing for zero", {
expect_equal(handshake(0), c())
})
test_that("do nothing if lower 5 bits not set", {
expect_equal(handshake(32), c())
})
message("All tests passed for exercise: secret-handshake")
```

```
tonumberstring <- function(n, base=2) {
remainder <- n
digits <- c()
while(remainder > 0) {
d <- floor(log(remainder, base))
digits <- c(digits, d)
remainder <- remainder - base^d
}
outs <- rep.int("0", times=max(digits)+1 )
outs[digits+1] <- "1"
return(paste0(rev(outs), collapse=""))
}
handshake <- function(n) {
if(n==0) return(NULL)
binstr <- tonumberstring(n)
patterns <- list(wink="1\\>",
"double blink"="1[01]\\>",
"close your eyes"="1[01]{2}\\>",
"jump"="1[01]{3}\\>"
)
matches <- names(patterns)[sapply(patterns, grepl, x=binstr)]
if(length(matches)==0) return(NULL)
reverse <- grepl("1[01]{4}\\>", binstr)
if(reverse)
return(rev(matches))
else
return(matches)
}
```

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,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments