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.
For installation and learning resources, refer to the exercism help page.
For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:
gem install minitest
If you would like color output, you can
require 'minitest/pride' in
the test file, or note the alternative instruction, below, for running
the test file.
Run the tests from the exercise directory using the following command:
To include color from the command line:
ruby -r minitest/pride secret_handshake_test.rb
Bert, in Mary Poppins http://www.imdb.com/title/tt0058331/quotes/qt0437047
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
require 'minitest/autorun' require_relative 'secret_handshake' class SecretHandshakeTest < Minitest::Test def test_handshake_1_to_wink handshake = SecretHandshake.new(1) assert_equal ['wink'], handshake.commands end def test_handshake_10_to_double_blink skip handshake = SecretHandshake.new(2) assert_equal ['double blink'], handshake.commands end def test_handshake_100_to_close_your_eyes skip handshake = SecretHandshake.new(4) assert_equal ['close your eyes'], handshake.commands end def test_handshake_1000_to_jump skip handshake = SecretHandshake.new(8) assert_equal ['jump'], handshake.commands end def test_handshake_11_to_wink_and_double_blink skip handshake = SecretHandshake.new(3) assert_equal ['wink', 'double blink'], handshake.commands end def test_handshake_10011_to_double_blink_and_wink skip handshake = SecretHandshake.new(19) assert_equal ['double blink', 'wink'], handshake.commands end def test_handshake_11111_to_double_blink_and_wink skip handshake = SecretHandshake.new(31) expected = ['jump', 'close your eyes', 'double blink', 'wink'] assert_equal expected, handshake.commands end def test_invalid_handshake skip handshake = SecretHandshake.new('piggies') assert_equal , handshake.commands end end
# SecretHandshake class SecretHandshake attr_reader :decimal def initialize(decimal) @decimal = decimal end def commands return  unless decimal.is_a?(Integer) handshake end def binary format('%05d', decimal.to_s(2)) end private def handshake results =  results << 'wink' if binary.reverse == '1' results << 'double blink' if binary.reverse == '1' results << 'close your eyes' if binary.reverse == '1' results << 'jump' if binary.reverse == '1' results.reverse! if binary.reverse == '1' results end end
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.