🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of yashar

yashar's solution

to Secret Handshake in the R Track

Published at Feb 12 2020 · 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.

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("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")
signals <- c("wink", "double blink", "close your eyes", "jump")

handshake <- function(n) {
  b <- as.logical(intToBits(n)[1:5])
  out <- signals[b[1:4]]
  if(length(out) == 0) return(c())
  if (b[5]) return (rev(out))
  else return (out)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?