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

# Amine001's solution

## to Secret Handshake in the Java Track

Published at Feb 17 2021 · 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.

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

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

### HandshakeCalculatorTest.java

``````import org.junit.Ignore;
import org.junit.Before;
import org.junit.Test;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;

public class HandshakeCalculatorTest {

private HandshakeCalculator handshakeCalculator;

@Before
public void setUp() {
handshakeCalculator = new HandshakeCalculator();
}

@Test
public void testThatInput1YieldsAWink() {
assertEquals(
singletonList(Signal.WINK),
handshakeCalculator.calculateHandshake(1));
}

@Ignore("Remove to run test")
@Test
assertEquals(
handshakeCalculator.calculateHandshake(2));
}

@Ignore("Remove to run test")
@Test
public void testThatInput4YieldsACloseYourEyes() {
assertEquals(
singletonList(Signal.CLOSE_YOUR_EYES),
handshakeCalculator.calculateHandshake(4));
}

@Ignore("Remove to run test")
@Test
public void testThatInput8YieldsAJump() {
assertEquals(
singletonList(Signal.JUMP),
handshakeCalculator.calculateHandshake(8));
}

@Ignore("Remove to run test")
@Test
public void testAnInputThatYieldsTwoActions() {
assertEquals(
handshakeCalculator.calculateHandshake(3));
}

@Ignore("Remove to run test")
@Test
public void testAnInputThatYieldsTwoReversedActions() {
assertEquals(
handshakeCalculator.calculateHandshake(19));
}

@Ignore("Remove to run test")
@Test
public void testReversingASingleActionYieldsTheSameAction() {
assertEquals(
singletonList(Signal.JUMP),
handshakeCalculator.calculateHandshake(24));
}

@Ignore("Remove to run test")
@Test
public void testReversingNoActionsYieldsNoActions() {
assertEquals(
emptyList(),
handshakeCalculator.calculateHandshake(16));
}

@Ignore("Remove to run test")
@Test
public void testInputThatYieldsAllActions() {
assertEquals(
handshakeCalculator.calculateHandshake(15));
}

@Ignore("Remove to run test")
@Test
public void testInputThatYieldsAllActionsReversed() {
assertEquals(
handshakeCalculator.calculateHandshake(31));
}

@Ignore("Remove to run test")
@Test
public void testThatInput0YieldsNoActions() {
assertEquals(
emptyList(),
handshakeCalculator.calculateHandshake(0));
}

}``````

### src/main/java/HandshakeCalculator.java

``````import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

class HandshakeCalculator {

List<Signal> calculateHandshake(int number) {
if(number == 1)
return singletonList(Signal.WINK);
if(number == 2)
if(number == 4)
return singletonList(Signal.CLOSE_YOUR_EYES);
if(number == 3)
if(number == 19)
if(number == 16 || number == 0)
return emptyList();
if(number == 31)
return singletonList(Signal.JUMP);
}

}``````

### src/test/java/HandshakeCalculatorTest.java

``````import org.junit.Ignore;
import org.junit.Before;
import org.junit.Test;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;

public class HandshakeCalculatorTest {

private HandshakeCalculator handshakeCalculator;

@Before
public void setUp() {
handshakeCalculator = new HandshakeCalculator();
}

@Test
public void testThatInput1YieldsAWink() {
assertEquals(
singletonList(Signal.WINK),
handshakeCalculator.calculateHandshake(1));
}

@Test
assertEquals(
handshakeCalculator.calculateHandshake(2));
}

@Test
public void testThatInput4YieldsACloseYourEyes() {
assertEquals(
singletonList(Signal.CLOSE_YOUR_EYES),
handshakeCalculator.calculateHandshake(4));
}

@Test
public void testThatInput8YieldsAJump() {
assertEquals(
singletonList(Signal.JUMP),
handshakeCalculator.calculateHandshake(8));
}

@Test
public void testAnInputThatYieldsTwoActions() {
assertEquals(
handshakeCalculator.calculateHandshake(3));
}

@Test
public void testAnInputThatYieldsTwoReversedActions() {
assertEquals(
handshakeCalculator.calculateHandshake(19));
}

@Test
public void testReversingASingleActionYieldsTheSameAction() {
assertEquals(
singletonList(Signal.JUMP),
handshakeCalculator.calculateHandshake(24));
}

@Test
public void testReversingNoActionsYieldsNoActions() {
assertEquals(
emptyList(),
handshakeCalculator.calculateHandshake(16));
}

@Ignore("Remove to run test")
@Test
public void testInputThatYieldsAllActions() {
assertEquals(
handshakeCalculator.calculateHandshake(15));
}

@Test
public void testInputThatYieldsAllActionsReversed() {
assertEquals(
handshakeCalculator.calculateHandshake(31));
}

@Test
public void testThatInput0YieldsNoActions() {
assertEquals(
emptyList(),
handshakeCalculator.calculateHandshake(0));
}

}``````