Last active
August 29, 2015 13:59
-
-
Save macuk/10665940 to your computer and use it in GitHub Desktop.
Phone number to letters converter
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
Letters = { | |
'0' => %w(0), | |
'1' => %w(1), | |
'2' => %w(a b c), | |
'3' => %w(d e f), | |
'4' => %w(g h i), | |
'5' => %w(j k l), | |
'6' => %w(m n o), | |
'7' => %w(p q r s), | |
'8' => %w(t u v), | |
'9' => %w(w x y z), | |
} | |
def convert(number) | |
mapping = number.map { |d| Letters[d] } | |
first = mapping.first | |
rest = mapping[1..-1] | |
first.product(*rest).map { |l| l.join } | |
end | |
result = convert %w(5 0 7) | |
solution = %w( | |
j0p j0q j0r j0s | |
k0p k0q k0r k0s | |
l0p l0q l0r l0s) | |
puts 'OK' if (solution - result).empty? | |
result = convert %w(3 6 9) | |
solution = %w(dmw dmx dmy dmz | |
dnw dnx dny dnz | |
dow dox doy doz | |
emw emx emy emz | |
enw enx eny enz | |
eow eox eoy eoz | |
fmw fmx fmy fmz | |
fnw fnx fny fnz | |
fow fox foy foz) | |
puts 'OK' if (solution - result).empty? |
gotar
commented
Apr 14, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment