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

SergiiVlasiuk's solution

to Secret Handshake in the Scala Track

Published at Aug 17 2019 · 0 comments
Test suite

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.

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.


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.


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"))

  test("double blink for 10") {
    SecretHandshake.commands(2) should be (List("double blink"))

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

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

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

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

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

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

  test("all possible actions") {
    SecretHandshake.commands(15) should be (List("wink", "double blink", "close your eyes", "jump"))

  test("reverse all possible actions") {
    SecretHandshake.commands(31) should be (List("jump", "close your eyes", "double blink", "wink"))

  test("do nothing for zero") {
    SecretHandshake.commands(0) should be (List())
object SecretHandshake {
  val secrets: Map[Int, String] = Map(
    1 -> "wink",
    2 -> "double blink",
    4 -> "close your eyes",
    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

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?