Skip to content

Instantly share code, notes, and snippets.

@gregeng
Last active December 25, 2015 00:09
Show Gist options
  • Save gregeng/6885439 to your computer and use it in GitHub Desktop.
Save gregeng/6885439 to your computer and use it in GitHub Desktop.
binary-handshake
require 'pry'
class SecretHandshake
attr_accessor :binary
def initialize(binary)
@binary = binary
end
def commands
translated_array = []
binary_array = binary.to_s.split(//)
if binary_array[-1] == "1"
translated_array << "wink"
end
if binary_array[-2] == "1"
translated_array << "double blink"
end
if binary_array[-3] == "1"
translated_array << "close your eyes"
end
if binary_array[-4] == "1"
translated_array << "jump"
end
binary.to_i > 9999 ? translated_array.reverse! : translated_array
end
end
require_relative './binary_handshake'
# # Binary Secret Handshake
# > There are 10 types of people in the world: Those who understand binary, and those who don't.
# You and your fellow flatirons are 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.
# ```
# Write a program that will convert a binary number, represented as a string (i.e. "101010"), and convert it to the appropriate sequence of events for a secret handshake.
# ```
# handshake = SecretHandshake.new "1001"
# handshake.commands # => ["wink","jump"]
# handshake = SecretHandshake.new "11001"
# handshake.commands # => ["jump","wink"]
# ```
# The program should consider strings specifying an invalid binary as the value 0.
RSpec.configure do |config|
# Use color in STDOUT
config.color_enabled = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
# Use the specified formatter
config.formatter = :progress # :progress, :html, :textmate
end
describe 'SecretHandshake' do
it 'should know how to wink with a 1' do
handshake = SecretHandshake.new("1")
handshake.commands.should eq(["wink"])
end
it 'should know how to double blink with a 10' do
handshake = SecretHandshake.new("10")
handshake.commands.should eq(["double blink"])
end
it 'should know how to close your eyes with 100' do
handshake = SecretHandshake.new("100")
handshake.commands.should eq(["close your eyes"])
end
it 'should know to jump with 1000' do
handshake = SecretHandshake.new("1000")
handshake.commands.should eq(["jump"])
end
it 'should know to wink double blink with 11' do
handshake = SecretHandshake.new("11")
handshake.commands.should eq(["wink","double blink"])
end
it 'should know to double blink, wink, with 10011' do
handshake = SecretHandshake.new("10011")
handshake.commands.should eq(["double blink","wink"])
end
it 'should get this complicated handshake correctly' do
handshake = SecretHandshake.new("11111")
handshake.commands.should eq(["jump","close your eyes","double blink","wink"])
end
it 'should not respond to an invalid code' do
handshake = SecretHandshake.new("binary")
handshake.commands.should eq([])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment