Return to Snippet

Revision: 37549
at December 11, 2010 16:15 by vishalag


Initial Code
def telrank(str):
    """ Synopsis:  Rank a string based on how fast it is to type on a typical phone keypad (lower is faster). Return -1 if invalid input.
    Examples: 'PAPA': P-A-P-A = 4
              'FATHER': DDD(F)-A-T-GG(H)-DD(E)-PPP(R) = 12
              'DADDY': D-A-D-PAUSE-D-WWW(Y) = 7 + PAUSE_PENALTY
    Reference:
                 ABC    DEF
          GHI    JKL    MNO
          PQRS   TUV    WXYZ
    """
    SEQ = "ADGJMPTWBEHKNQUXCFILORVY00000S0Z" 
    PAUSE_PENALTY = 100 # penalty if same key has to be pressed twice (for delay)

    str = str.rstrip()
    if not str.isalpha():
        return -1

    rank = 0
    order = -1

    for letter in str.upper():
        idx = SEQ.find(letter)
        level = idx/8
        if order == idx%8:
            pause_penalty = PAUSE_PENALTY
        else:
            pause_penalty = 0
            order = idx%8
        rank = rank + level + pause_penalty + 1
    return rank

Initial URL
http://i.vshal.com/2010/12/telrank.html

Initial Description


Initial Title
TelRank: Telephone Keypad Speed Ranker

Initial Tags
phone

Initial Language
Python