Skip to content

Commit

Permalink
Implement the core of question one (simple shift cypher), and set up …
Browse files Browse the repository at this point in the history
…autotest/rspec.
  • Loading branch information
Luke Redpath committed Oct 11, 2010
1 parent b860f36 commit 0b805ec
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
.rspec
--format nested
--color
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -8,4 +8,4 @@ namespace :quiz do
puts "Question #1: #{first_question}."
system("open #{first_question.reference_url}")
end
end
end
1 change: 1 addition & 0 deletions autotest/discover.rb
@@ -0,0 +1 @@
Autotest.add_discovery { "rspec2" }
34 changes: 34 additions & 0 deletions minisculus/question-1/question_one.rb
@@ -0,0 +1,34 @@
module Minisculus
module QuestionOne
class MarkI
def initialize(key, character_set = default_character_set)
@key, @character_set = key, character_set
end

def decrypt(chars)
chars.each_char.map { |char| transpose(char) }.join
end

private

def transpose(char)
index = @character_set.index(char) + @key
@character_set[index - @character_set.length]
end

def default_character_set
[
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
".", ",", "?", "!", "'", "\"", " "
]
end
end
end
end

if __FILE__ == $0
end
21 changes: 21 additions & 0 deletions spec/question_one_spec.rb
@@ -0,0 +1,21 @@
require 'spec_helper'
require 'question-1/question_one'

describe "Mark I machine" do # basic ceasar/shift cypher
include Minisculus::QuestionOne

it "should shift a character in a set by the given key when decrypting" do
Minisculus::QuestionOne::MarkI.new(1, %w{a b c}).decrypt("a").should == "b"
Minisculus::QuestionOne::MarkI.new(2, %w{a b c}).decrypt("a").should == "c"
end

it "should shift multiple characters in a set by the given key" do
Minisculus::QuestionOne::MarkI.new(1, %w{a b c}).decrypt("ab").should == "bc"
end

it "should go back to the beginning of the set when shifting moves off the end of the set" do
Minisculus::QuestionOne::MarkI.new(2, %w{a b c}).decrypt("b").should == "a"
Minisculus::QuestionOne::MarkI.new(1, %w{a b c}).decrypt("bc").should == "ca"
end

end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,2 @@
require 'rspec'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[.. minisculus]))

0 comments on commit 0b805ec

Please sign in to comment.