Skip to content

Instantly share code, notes, and snippets.

@bryanaka
Created October 7, 2013 07:17
Show Gist options
  • Save bryanaka/6863725 to your computer and use it in GitHub Desktop.
Save bryanaka/6863725 to your computer and use it in GitHub Desktop.
Rotational Cipher for a Technical Challenge
class RotationalCipher
def initialize(offset)
while offset > 26
offset -= 26
end
generate_cipher_map(offset)
end
class << self
def rotx(offset, string, encrypt=true)
instance = self.new(offset)
encrypt ? instance.encrypt(string) : instance.decrypt(string)
end
end
def encrypt(string)
resolve_cipher(string, @cipher_map)
end
def decrypt(string)
resolve_cipher(string, @cipher_map.invert)
end
private
def generate_cipher_map(offset)
@cipher_map = {}
letters = Array('a'..'z')
cipher_letters = letters[offset..-1].concat(letters[0..offset-1])
cipher_letters.each_with_index do |element, index|
@cipher_map[letters[index]] = element
end
end
def uppercase?(char)
char != char.downcase ? true : false
end
def resolve_cipher(string, rot_map)
result = ''
string.each_char do |c|
is_uppercase = uppercase?(c)
c.downcase!
if is_uppercase && rot_map.has_key?(c)
result << rot_map[c].upcase
elsif !is_uppercase && rot_map.has_key?(c)
result << rot_map[c]
else
result << c
end
end
result
end
end
require 'rspec'
require File.expand_path('../rotational_cipher', __FILE__)
describe RotationalCipher do
let(:rotx) { RotationalCipher.new(5) }
let(:rotx_10){ RotationalCipher.new(10) }
let(:rotx_36){ RotationalCipher.new(36) }
describe 'can encrypt' do
it 'a simple string' do
code = rotx.encrypt 'hi'
code2 = rotx_10.encrypt 'hello'
code3 = rotx_36.encrypt 'hello'
code.should eq('mn')
code2.should eq('rovvy')
code3.should eq('rovvy')
end
it 'a string with puncuation' do
code = rotx.encrypt 'hi!'
code2 = rotx_10.encrypt 'hello,'
code3 = rotx_36.encrypt 'hello,'
code.should eq('mn!')
code2.should eq('rovvy,')
code3.should eq('rovvy,')
end
it 'a string with whitespace' do
code = rotx.encrypt('hi dude')
code.should eq('mn izij')
end
it 'a string with capitalization' do
code = rotx.encrypt('Hi Dude!')
code2 = rotx_10.encrypt('Hello, World')
code.should eq('Mn Izij!')
code2.should eq('Rovvy, Gybvn')
end
end
describe 'can decrypt' do
it 'a simple string' do
code = rotx.decrypt 'mn'
code2 = rotx_10.decrypt 'rovvy'
code.should eq('hi')
code2.should eq('hello')
end
it 'a string with puncuation' do
code = rotx.decrypt 'mn!'
code2 = rotx_10.decrypt 'rovvy,'
code.should eq('hi!')
code2.should eq('hello,')
end
it 'a string with whitespace' do
code = rotx.decrypt('mn izij')
code.should eq('hi dude')
end
it 'a string with capitalization' do
code = rotx.decrypt('Mn Izij!')
code2 = rotx_10.decrypt('Rovvy, Gybvn')
code.should eq('Hi Dude!')
code2.should eq('Hello, World')
end
end
describe 'class method for instance calling' do
it 'mimics the above encrypt method' do
code = RotationalCipher.rotx(10, 'Hello, World')
code.should eq('Rovvy, Gybvn')
end
it 'calls a decrypt method on the instance' do
code = RotationalCipher.rotx(10, 'Rovvy, Gybvn', false)
code.should eq('Hello, World')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment