Created
April 17, 2015 17:40
-
-
Save madwork/18ea6ea0d28ffd2a97a9 to your computer and use it in GitHub Desktop.
Caesar Cipher Implementation in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CaesarCipher | |
# Example: | |
# cipher = CaesarCipher.new "Ruby is a dynamic, open source programming language!" | |
# cipher.encrypt(13) | |
# => "ehol vf n qlanzvp, bcra fbhepr cebtenzzvat ynathntr!" | |
class CaesarCipher | |
attr_accessor :sentence | |
def initialize(sentence) | |
@sentence = sentence | |
end | |
def decrypt(i) | |
compute { |index| index - i } | |
end | |
def encrypt(i) | |
compute { |index| index + i } | |
end | |
private | |
def compute | |
@sentence.downcase.each_char.each_with_object(String.new) do |char, str| | |
if alphabet.has_key?(char) | |
str << alphabet.key(yield(alphabet.fetch(char)) % 26) | |
else | |
str << char | |
end | |
end | |
end | |
def alphabet | |
@alphabet ||= ("a".."z").each_with_index.to_h | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/spec' | |
require 'minitest/autorun' | |
$LOAD_PATH.unshift File.expand_path('.', File.dirname(__FILE__)) | |
require 'caesar_cipher.rb' | |
describe CaesarCipher do | |
subject { CaesarCipher.new "Hello World!" } | |
it { subject.must_be_instance_of CaesarCipher } | |
it { subject.sentence.must_equal "Hello World!" } | |
describe "#encrypt" do | |
subject { CaesarCipher.new "Hello World!" } | |
it { subject.encrypt(1).must_equal "ifmmp xpsme!" } | |
it { subject.encrypt(13).must_equal "uryyb jbeyq!" } | |
end | |
describe "#decrypt" do | |
subject { CaesarCipher.new "uryyb jbeyq!" } | |
it { subject.decrypt(13).must_equal "hello world!" } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$LOAD_PATH.unshift File.expand_path('.', File.dirname(__FILE__)) | |
require 'caesar_cipher.rb' | |
WORDS = ["what", "how", "why", "when", "where", "which", "is", "the", "of", "by", "on"] | |
question = "jung vf gur orfg jro senzrjbex?" | |
response = "Qtax Nm Qzhkr hr ne bntqrd sgd adrs vda eqzldvnqj!" | |
def detect(cipher) | |
26.times.each do |i| | |
decrypt = cipher.decrypt(i) | |
puts decrypt if WORDS.detect { |word| decrypt.include?(word) } | |
end | |
end | |
cipher = CaesarCipher.new(question) | |
detect(cipher) | |
cipher.sentence = response | |
detect(cipher) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment