Created
June 2, 2012 07:09
-
-
Save dpk/2857091 to your computer and use it in GitHub Desktop.
txt2num: convert a 'telephone word' into raw numbers
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
#!/usr/bin/env ruby | |
def txt2num str | |
letters = {'a' => 2, 'b' => 2, 'c' => 2, | |
'd' => 3, 'e' => 3, 'f' => 3, | |
'g' => 4, 'h' => 4, 'i' => 4, | |
'j' => 5, 'k' => 5, 'l' => 5, | |
'm' => 6, 'n' => 6, 'o' => 6, | |
'p' => 7, 'q' => 7, 'r' => 7, 's' => 7, | |
't' => 8, 'u' => 8, 'v' => 8, | |
'w' => 9, 'x' => 9, 'y' => 9, 'z' => 9} | |
str.chars.map do |char| | |
letters[char.downcase] || char | |
end.join | |
end | |
if ARGV.empty? | |
while gets | |
print txt2num $_ | |
end | |
else | |
ARGV.each {|word| puts txt2num word } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment