Created
April 10, 2011 08:20
-
-
Save roopeshvaddepally/912137 to your computer and use it in GitHub Desktop.
convert a phone number with alphabets in it to proper numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MAPPING = { | |
tuple('abcABC'): 2, | |
tuple('defDEF'): 3, | |
tuple('ghiGHI'): 4, | |
tuple('jklJKL'): 5, | |
tuple('mnoMNO'): 6, | |
tuple('pqrsPQRS'): 7, | |
tuple('tuvTUV'): 8, | |
tuple('wxyzWXYZ'): 9, | |
} | |
def proper_dict(dictionary, sequence_of_keys, value): | |
for each in sequence_of_keys: | |
dictionary.update({each: value}) | |
def convert(digit, dictionary): | |
if str.isalpha(digit): return str(dictionary.get(digit, "")) | |
else: return digit | |
if __name__ == '__main__': | |
import sys | |
phone = sys.argv[1] | |
d = dict() | |
[proper_dict(d, key, value) for key, value in MAPPING.items()] | |
print "".join([convert(each, d) for each in phone]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment