Created
August 23, 2014 04:15
-
-
Save trigun0x2/2f9dd1d08a2b7a660dde to your computer and use it in GitHub Desktop.
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
class SecretNumber | |
attr_accessor :encrypted_num | |
def initialize(encrypted_num) | |
@encrypted_num = encrypted_num | |
end | |
KEY = { "q" => 0, | |
"w" => 1, | |
"e" => 2, | |
"r" => 3, | |
"t" => 4, | |
"y" => 5, | |
"u" => 6, | |
"i" => 7, | |
"o" => 8, | |
"p" => 9 | |
} | |
def decrypt_num | |
num_pairs = get_uniq_num_pairs(@encrypted_num) | |
num_pairs_to_phone_num(num_pairs) | |
end | |
def get_uniq_num_pairs(string) | |
pair_number = Array.new | |
string.split("").each_slice(2) do |char_pair| | |
pair_number << [ KEY[char_pair[0]] , KEY[char_pair[1]] ] | |
end | |
p pair_number.uniq | |
end | |
def num_pairs_to_phone_num(num_pairs) | |
pair_sums = num_pairs | |
.collect do |pair| | |
sum = pair.sum | |
sum.to_s.size == 2 ? sum : "" | |
end | |
pair_sums.uniq.join | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment