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

# ajveltri22's solution

## to Secret Handshake in the R Track

Published at Aug 24 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) {
commands <- c(1, 2, 4, 8)
bv <- bitwAnd(n, commands) > 0
output <- names(commands[bv])
if (length(output) == 0) {
output <- c()
} else if (bitwAnd(n, 16)) output <- rev(output)
return(output)
}

first_attempt_handshake <- function(n) {
binary <- function(n) {
if (n < 0 || n%%1 != 0) stop("Input must be a positive integer")
binary <- 0
while (n != 0) {
left_part <- floor(log2(n))
binary <- binary + 10^left_part
n <- n - 2^left_part
}
return(binary)
}

n <- binary(n)
n <- toString(n)
output <- c()
len <- nchar(n)
if (substr(n, len, len) == "1") output <- append(output, "wink")
if (len > 1 && substr(n, len-1, len-1) == "1") output <- append(output, "double blink")
if (len > 2 && substr(n, len-2, len-2) == "1") output <- append(output, "close your eyes")
if (len > 3 && substr(n, len-3, len-3) == "1") output <- append(output, "jump")
if (len > 4 && substr(n, len-4, len-4) == "1") output <- rev(output)
return(output)
}``````