Created
August 29, 2016 20:36
-
-
Save danielpowell4/9848824035d5edb0201a3b6af12cdd83 to your computer and use it in GitHub Desktop.
What is the press count for texting on older T9 phones?
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
# best answer | |
def presses(phrase) | |
groups = ["1", " 0", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "TUV8", "PQRS7", "WXYZ9"] | |
phrase.upcase.chars.map do |c| | |
1 + groups.find { |grp| grp.include?(c) }.index(c) | |
end.reduce(:+) | |
end | |
# another option | |
def presses(phrase) | |
letters = phrase.upcase.split('').map { |s| s.to_s } | |
score = 0 | |
letters.each do |letter| | |
case letter | |
when 'A','D','G','J','M','P','T','W',' ','1','*','#' | |
score += 1 | |
when 'B','E','H','K','N','Q','U','X','0' | |
score += 2 | |
when 'C','F','I','L','O','R','V','Y' | |
score += 3 | |
when 'S','Z','2','3','4','5','6','7','8','9' | |
score += 4 | |
end | |
end | |
return score | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment