view combinations_permutations/phone_to_words/question.rst @ 0:20fea762903e

Import some exercises and solutions
author Louis Opter <kalessin@kalessin.fr>
date Sat, 29 Jun 2013 19:30:31 -0700
parents
children
line wrap: on
line source

Phone Number to Words
=====================

Picture a telephone keypad and write a function that takes a phone number in
argument (as a string) and return the list of possible words that map to this
number (the words don't have to be valid dictionary words).

The following function is provided:

.. code-block:: python

   def d2l(digit):
        return {
            "2": "abc", "3": "def",
            "4": "ghi", "5": "jkl", "6": "mno",
            "7": "pqrs", "8": "tuv", "9": "wxyz"
        }[digit]

Example: p2w("23") → [ad, ae, af, bd, be, bf, cd, ce, cf].