Created
March 11, 2014 17:28
-
-
Save rwarbelow/9490710 to your computer and use it in GitHub Desktop.
We created a class called SecretCode that takes a word or sentence and encodes it. It also has a method that decodes the secret message.
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
ENCODE = { "a" => "j", "b" => "3", "c" => "l", "d" => "m", "e" => ".", | |
"f" => "o", "g" => "p", "h" => "4", "i" => "r", "j" => "&", "k" => "t", | |
"l" => "u", "m" => "v", "n" => "#", "o" => "x", "p" => "y", "q" => "9", | |
"r" => "a", "s" => "$", "t" => "c", "u" => "[", "v" => "e", "w" => "f", | |
"x" => "g", "y" => "8", "z" => "i", " " => " " } | |
DECODE = { "j" => "a", "3" => "b", "l" => "c", "m" => "d", "." => "e", | |
"o" => "f", "p" => "g", "4" => "h", "r" => "i", "&" => "j", "t" => "k", | |
"u" => "l", "v" => "m", "#" => "n", "x" => "o", "y" => "p", "9" => "q", | |
"r" => "a", "s" => "$", "t" => "c", "u" => "[", "v" => "e", "w" => "f", | |
"x" => "g", "y" => "8", "z" => "i" } | |
class SecretCode | |
def initialize(word) | |
@word = word | |
@encoded_word = [] | |
@decoded_word = [] | |
end | |
def split_word | |
@word.split("") | |
end | |
def encode | |
split_word.each do |letter| | |
@encoded_word << ENCODE[letter.downcase] | |
end | |
p @encoded_word.join | |
end | |
def decode | |
split_word.each do |letter| | |
@decoded_word << DECODE[letter.downcase] | |
end | |
p @decoded_word.join | |
end | |
end | |
code = SecretCode.new("Hello world") | |
code.encode | |
code = SecretCode.new("4.a. r$ j $.#c.#l.") | |
code.decode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment