TelRank: Telephone Keypad Speed Ranker


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. def telrank(str):
  2. """ 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.
  3. Examples: 'PAPA': P-A-P-A = 4
  4. 'FATHER': DDD(F)-A-T-GG(H)-DD(E)-PPP(R) = 12
  5. 'DADDY': D-A-D-PAUSE-D-WWW(Y) = 7 + PAUSE_PENALTY
  6. Reference:
  7. ABC DEF
  8. GHI JKL MNO
  9. PQRS TUV WXYZ
  10. """
  11. SEQ = "ADGJMPTWBEHKNQUXCFILORVY00000S0Z"
  12. PAUSE_PENALTY = 100 # penalty if same key has to be pressed twice (for delay)
  13.  
  14. str = str.rstrip()
  15. if not str.isalpha():
  16. return -1
  17.  
  18. rank = 0
  19. order = -1
  20.  
  21. for letter in str.upper():
  22. idx = SEQ.find(letter)
  23. level = idx/8
  24. if order == idx%8:
  25. pause_penalty = PAUSE_PENALTY
  26. else:
  27. pause_penalty = 0
  28. order = idx%8
  29. rank = rank + level + pause_penalty + 1
  30. return rank

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.