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

# SergiiVlasiuk's solution

## to Secret Handshake in the Scala Track

Published at Aug 17 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.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

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

### SecretHandshakeTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.2.0 */
class SecretHandshakeTest extends FunSuite with Matchers {

test("wink for 1") {
SecretHandshake.commands(1) should be (List("wink"))
}

pending
}

test("close your eyes for 100") {
pending
SecretHandshake.commands(4) should be (List("close your eyes"))
}

test("jump for 1000") {
pending
SecretHandshake.commands(8) should be (List("jump"))
}

test("combine two actions") {
pending
SecretHandshake.commands(3) should be (List("wink", "double blink"))
}

test("reverse two actions") {
pending
SecretHandshake.commands(19) should be (List("double blink", "wink"))
}

test("reversing one action gives the same action") {
pending
SecretHandshake.commands(24) should be (List("jump"))
}

test("reversing no actions still gives no actions") {
pending
SecretHandshake.commands(16) should be (List())
}

test("all possible actions") {
pending
}

test("reverse all possible actions") {
pending
}

test("do nothing for zero") {
pending
SecretHandshake.commands(0) should be (List())
}
}``````
``````object SecretHandshake {
val secrets: Map[Int, String] = Map(
1 -> "wink",
8 -> "jump"
)

val reverse: Int = 16

def commands(input: Int): List[String] = {
val list = secrets.foldLeft(List[String]())((acc, pair) =>
if ((input & pair._1) > 0) pair._2 :: acc
else acc
)
println(s"input & reverse =>  \$input & \$reverse = " + (input & reverse))
if ((input & reverse) > 0) list
else list.reverse
}
}``````