How to generate alphabets from mobile number using Python

Python Program

Using the mobile keypad layout:

1 => 1,   2 => ABC, 3 => DEF, 4 => GHI, 5 => JKL,6 => MNO, 7 => PQRS, 8 => TUV, 9 => WXYZ, 0 =>  0+_
For a mobile number, generate the possible combinations of alphabets matching each digit of the number.

I have written the program in Python. This program will generate all possible combinations of alphabets for the phone number given by the user.

For Example:

Enter your mobile number: 9854386072
The sequence combinations are:
WTJGDTM PA
WUJGDUM PA
WTKHDUM PA

ZVLIFVO_SC

[code lang=”py”]#! /usr/bin/env python
# -*- coding: utf-8 -*-

dict = {0 : " _+", 1 : "1", 2 : "abc", 3 : "def", 4 : "ghi",
5 : "jkl", 6 : "mno", 7 : "pqrs", 8 : "tuv", 9 : "wxyz"}

lis = [list(dict.get(x)) for x in range(0, 10)]
"""
the above line generate the list of values
eg: [[‘ ‘, ‘_’, ‘+’], [‘1′], [‘a’, ‘b’, ‘c’]….[‘w’, ‘x’,’y’, ‘z’]]
"""
mob_num = list(raw_input("Enter mobile number: "))
#get the input(phone number) from the user as list

num = [int(x) for x in mob_num] #convert list of strings to list of integers

phonekey = [lis[x] for x in num] #re-arranging the list as per phone number

res = ["".join([a,b,c,d,e,f,g,h,i,j]) for a in phonekey[0] for b in phonekey[1]
for c in phonekey[2] for d in phonekey[3] for e in phonekey[4]
for f in phonekey[5] for g in phonekey[6] for h in phonekey[7]
for i in phonekey[8] for j in phonekey[9]]

for x in res:
print x[/code]

Right click here and “select save link as” to download the above Python program.
I hope this post was helpful!

Related Posts Plugin for WordPress, Blogger...

  • http://www.ByteChip.com Ramkumar

    Good one, Try putting the code under WP Syntax plugin.

    • http://www.howopensource.com Manivannan

      Yup…Thanks Ram :)