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

# RaulToomba's solution

## to Secret Handshake in the CFML Track

Published at Jul 13 2018 · 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.

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

# Or start up a test watcher that will rerun when files change

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

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

component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'SecretHandshake' );
}

function run(){

describe( "My SecretHandshake class", function(){

describe( 'Create a handshake for a number', function(){

it( 'wink for 1', function(){
expect( SUT.commands( number='1' ) ).toBe( ["wink"] );
});

it( 'double blink for 10', function(){
expect( SUT.commands( number='2' ) ).toBe( ["double blink"] );
});

it( 'close your eyes for 100', function(){
expect( SUT.commands( number='4' ) ).toBe( ["close your eyes"] );
});

it( 'jump for 1000', function(){
expect( SUT.commands( number='8' ) ).toBe( ["jump"] );
});

it( 'combine two actions', function(){
expect( SUT.commands( number='3' ) ).toBe( ["wink","double blink"] );
});

it( 'reverse two actions', function(){
expect( SUT.commands( number='19' ) ).toBe( ["double blink","wink"] );
});

it( 'reversing one action gives the same action', function(){
expect( SUT.commands( number='24' ) ).toBe( ["jump"] );
});

it( 'reversing no actions still gives no actions', function(){
expect( SUT.commands( number='16' ) ).toBe( [] );
});

it( 'all possible actions', function(){
});

it( 'reverse all possible actions', function(){
});

it( 'do nothing for zero', function(){
expect( SUT.commands( number='0' ) ).toBe( [] );
});

});

});

}

}

### SolutionTest.cfc

component extends="SecretHandshakeTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}
/**
* Your implmentation of the SecretHandshake exercise
*/
component {

/**
* @returns
*/
function commands( input ) {
binary=FormatBaseN(input,2)
a=listToArray(binary, "");
shake=[];
if ( (arrayLen(a) >= 1) && (a[arrayLen(a)] == 1)){
ArrayAppend(shake,"wink");
}
if ( (arrayLen(a) >= 2) && (a[arrayLen(a)-1] == 1)){
}
if ( (arrayLen(a) >= 3) && (a[arrayLen(a)-2] == 1)){
}
if ( (arrayLen(a) >= 4) && (a[arrayLen(a)-3] == 1)){
ArrayAppend(shake,"jump");
}
if ( (arrayLen(a) >= 5) && (a[arrayLen(a)-4] == 1)){
b=[];
for ( var i = arrayLen( shake ); i >= 1; i-- ) {
ArrayAppend(b,shake[i]);
}
shake=b;
}
return shake;
}

}