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 Ruby resources 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
=begin Write your code for the 'Secret Handshake' exercise in this file. Make the tests in `secret_handshake_test.rb` pass. To get started with TDD, see the `README.md` file in your `ruby/secret-handshake` directory. =end # Elapsed time: don't understand reverse case completely, stolen solution: b13189eed990952b3d7213a1 class SecretHandshake def initialize(number) @number = number.to_i end def commands list =  list << "wink" if match?(0b1) list << "double blink" if match?(0b10) list << "close your eyes" if match?(0b100) list << "jump" if match?(0b1000) list.reverse! if match?(0b10000) list end private def match?(binary) (@number & binary).nonzero? 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.