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

killv5's solution

to Secret Handshake in the CFML Track

Published at Feb 25 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
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.


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:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

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(){
					expect( SUT.commands( number='15' ) ).toBe( ["wink","double blink","close your eyes","jump"] );
				});

				it( 'reverse all possible actions', function(){
					expect( SUT.commands( number='31' ) ).toBe( ["jump","close your eyes","double blink","wink"] );
				});

				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( number ) {
		ans = [];
		if (int(number)%2==1) arrayAppend(ans, 'wink');
		if (int(number/2)%2==1) arrayAppend(ans, 'double blink');
		if (int(number/4)%2==1) arrayAppend(ans, 'close your eyes');
		if (int(number/8)%2==1) arrayAppend(ans, 'jump');
		if (int(number/16)%2==1) {
			temp = duplicate(ans);
			for (i=1;i<=len(ans);i++) {
				ans[i] = temp[len(temp)+1-i];
			}
		}
		return ans;
	}
	
}

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?