24
7
We are all used to the old-school telephone keypad, right? For reference, here is what it looks like:
Given a String consisting only of lowercase ASCII letters and single spaces, your task is to return the number of taps one should make in order to type down the full String with a telephone keypad as the one above.
For those who are unfamiliar with this, here's how it works:
The key with the digit
2
, for example, also has the stringabc
written down on it. In order to typea
, you must press this key once, forb
you must press twice and forc
you must press thrice.For consecutive letters that are on the same key, you must wait 1 second before pressing again. So, if you want to type
cb
, you must press 3 times forc
, wait a second and then press twice forb
, so still 5 taps.The same applies for all other keys, except for a single space, which only requires 1 press. Also note that the keys
7
and9
have four letters on them. The same algorithm is applied, the only difference being the number of letters. The strings corresponding to each key can be found in the image above (but lowercase), or in the following list, that contains all the characters you might receive:"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz", " "
Test Cases
Input -> Output (Explanation) "" -> 0 (nothing should be tapped) "water" -> 8 ("w,a,t" each require 1 tap (on keys 9, 2 and 8), "e" requires 2 taps (on key 3), "r" requires 3 taps (on key 7), 1+1+1+2+3 = 8) "soap" -> 9 (4+3+1+1) "candela" -> 13 (3+1+2+1+2+3+1) "code golf" -> 20 (3+3+1+2+1(for the space)+1+3+3+3) "king of the hill" -> 33 (2+3+2+1+1+3+3+1+1+2+2+1+2+3+3+3)
Specs
Standard I/O rules and Default Loopholes apply.
You may only take input in your language's native String type. Output can either be an integer or a string representation of that integer.
This is code-golf, the shortest answer in every language wins.
Related, Inspired by this – Mr. Xcoder – 2017-06-28T11:03:18.353
Related. Related. – Shaggy – 2017-06-28T11:17:32.903
2I think this would be a more interesting question if you did 1 tap per second, and had to wait 1 second, and counted seconds instead of taps. – Yakk – 2017-06-28T16:22:01.183
@Yakk That would be far too complicated – Mr. Xcoder – 2017-06-28T18:15:13.267
@Mr.Xcoder Are you sure though? I have seen the code-wizards here do impossible stuff in less space than a tweet. – J_F_B_M – 2017-06-28T22:54:06.857
@J_F_B_M That wouldn't be very hard, but it is too complicated for the purpose of this challenge, at least. – Mr. Xcoder – 2017-06-29T06:53:07.967
Thrice, word is thrice. Once, twice, thrice. As a non-English speaker I saw our common struggle there :) – Ege Bayrak – 2017-06-29T07:46:16.770
@EgeBayrak Happy now? – Mr. Xcoder – 2017-06-29T07:47:22.767
Can we take all uppercase if that makes the code shorter? – Adám – 2017-07-04T08:24:23.730
@Adám No, sorry. I don't want to create confusion with old answers that might become shorter too. – Mr. Xcoder – 2017-07-04T08:26:15.520