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

# jeff-m-sullivan's solution

## to Secret Handshake in the R Track

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

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

## Source

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

## Submitting Incomplete Solutions

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

### test_secret-handshake.R

``````source("./secret-handshake.R")
library(testthat)

context("secret handshake")

test_that("wink for 1", {
expect_equal(handshake(1), c("wink"))
})

})

test_that("close your eyes for 100", {
})

test_that("jump for 1000", {
expect_equal(handshake(8), c("jump"))
})

test_that("combine two actions", {
})

test_that("reverse two actions", {
})

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),
})

test_that("reverse all possible actions", {
expect_equal(handshake(31),
})

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\\>",
"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)
}``````