Skip to content

Instantly share code, notes, and snippets.

@roopeshvaddepally
Created April 10, 2011 08:20
Show Gist options
  • Save roopeshvaddepally/912137 to your computer and use it in GitHub Desktop.
Save roopeshvaddepally/912137 to your computer and use it in GitHub Desktop.
convert a phone number with alphabets in it to proper numbers
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