Skip to content

Instantly share code, notes, and snippets.

@danielpowell4
Created August 29, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielpowell4/9848824035d5edb0201a3b6af12cdd83 to your computer and use it in GitHub Desktop.
Save danielpowell4/9848824035d5edb0201a3b6af12cdd83 to your computer and use it in GitHub Desktop.
What is the press count for texting on older T9 phones?
# 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