Weekly Coding Challenge: Multi-tap Keypad Text Entry on an Old Mobile Phone – written in Python

Posted by

Prior to having fancy iPhones, teenagers would wear out their thumbs sending SMS messages on candybar-shaped feature phones with 3×4 numeric keypads.

------- ------- -------
|     | | ABC | | DEF |
|  1  | |  2  | |  3  |
------- ------- -------
------- ------- -------
| GHI | | JKL | | MNO |
|  4  | |  5  | |  6  |
------- ------- -------
------- ------- -------
|PQRS | | TUV | | WXYZ|
|  7  | |  8  | |  9  |
------- ------- -------
------- ------- -------
|     | |space| |     |
|  *  | |  0  | |  #  |
------- ------- -------

Prior to the development of T9 (predictive text entry) systems, the method to type words was called “multi-tap” and involved pressing a button repeatedly to cycle through the possible values.

For example, to type a letter "R" you would press the 7 key three times (as the screen display for the current character cycles through P->Q->R->S->7). A character is “locked in” once the user presses a different key or pauses for a short period of time (thus, no extra button presses are required beyond what is needed for each letter individually). The zero key handles spaces, with one press of the key producing a space and two presses producing a zero.

In order to send the message "WHERE DO U WANT 2 MEET L8R" a teen would have to actually do 47 button presses. No wonder they abbreviated.

For this assignment, write a module that can calculate the amount of button presses required for any phrase. Punctuation can be ignored for this exercise. Likewise, you can assume the phone doesn’t distinguish between upper/lowercase characters (but you should allow your module to accept input in either for convenience).

Hint: While it wouldn’t take too long to hard code the amount of keypresses for all 26 letters by hand, try to avoid doing so! (Imagine you work at a phone manufacturer who might be testing out different keyboard layouts, and you want to be able to test new ones rapidly.)

Solution:

def presses(phrase):
    keypad_dict = {
        1: ['A','D','G','J','M','P','T','W','*','#',' ','1'],
        2: ['B','E','H','K','N','Q','U','X','0'],
        3: ['C','F','I','L','O','R','V','Y'],
        4: ['S','Z','2','3','4','5','6','8'],
        5: ['7','9']
    }
    press_count = 0
    for p in phrase:
        for key, value in keypad_dict.items():
            if p.upper() in value:
                press_count += key
    return press_count

My first line of thinking for this was to add up how many possible button selections there could ever be. Looking at the keypad above, I concluded that at most, you could only ever click the button 5 times, no matter what message you were trying to type. So, I built a dictionary assigning the number of button presses to each letter and then wrote a standard iteration to match the value in the phrase to the items within the map and once I found that, incremented the counter based on the key and returned it.

All solutions are also now on my GitHub profile if you want to check out all of the coding challenges I’ve completed so far!

All credit to problem sets goes to codewars

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s