Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the core of question one (simple shift cypher), and set up …
…autotest/rspec.
- Loading branch information
Luke Redpath
committed
Oct 11, 2010
1 parent
b860f36
commit 0b805ec
Showing
6 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.rspec | ||
--format nested | ||
--color |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Autotest.add_discovery { "rspec2" } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'rspec' | ||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[.. minisculus])) |