Skip to content

Instantly share code, notes, and snippets.

@mecampbellsoup
Last active December 25, 2015 00:08
Show Gist options
  • Select an option

  • Save mecampbellsoup/6885026 to your computer and use it in GitHub Desktop.

Select an option

Save mecampbellsoup/6885026 to your computer and use it in GitHub Desktop.
secret handshake binary decoder!!!
# # 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".
# 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.
# The program should consider strings specifying an invalid binary as the value 0.
class SecretHandshake
attr_accessor :secret_handshake, :commands
def initialize secret_handshake
@secret_handshake = secret_handshake
@commands = []
end
def commands
self.secret_handshake.chars.reverse.each_with_index do |command, i| # so now my first command is the rightmost binary
@commands << "wink" if command.to_i == 1 && i == 0
@commands << "double blink" if command.to_i == 1 && i == 1
@commands << "close your eyes" if command.to_i == 1 && i == 2
@commands << "jump" if command.to_i == 1 && i == 3
@commands.reverse! if command.to_i == 1 && i == 4
end
@commands
end
end
require_relative '../lib/binary_handshake'
require 'pry'
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