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

# sojbaek's solution

## to Secret Handshake in the R Track

Published at Nov 01 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")``````
``````handshake <- function(n) {
base=2;
bits = rep(0,5);
for (ii in 1:5) {
r = n %% base;
n = floor(n/2);
bits[ii]=r;
}
handshakes = actions[as.logical(bits[1:4])];
if (bits[5]==1) { handshakes = rev(handshakes);};
if (length(handshakes)==0) handshakes = c();
return(handshakes);
}``````