Respuesta :
Answer:
import re
dict = {
"A" :"2", "B" : "2", "C" : "2",
"D": "3", "E" : "3", "F" : "3",
"G": "4", "H" : "4", "I" : "4",
"J": "5", "K" : "5", "L" : "5",
"M" : "6", "N" : "6", "O" : "6",
"P": "7", "Q" : "7", "R" : "7", "S" : "7",
"T": "8", "U" : "8", "V" : "8",
"W": "9", "X" : "9", "Y": "9", "Z" : "9",
}
def phoneAlphaConverter(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
alpha_phone = input('Enter 10-Digit phone number: ').upper()
print(phoneAlphaConverter(dict, alpha_phone))
Explanation:
The programming language used is python 3.
The numeric equivalent of alphabets are as follows:
- 2 ABC
- 3 DEF
- 4 GHI
- 5 JKL
- 6 MNO
- 7 PQRS
- 8 TUV
- 9 WXYZ
In the code, It begins by importing the regular expression module which will allow us carry out special string operations on text.
Next, a dictionary is defined to hold all the alphabets and their number equivalent.
A function that would carry out the replacement of alphabets with their numbers is created; In the function, a regular expression is created with the dictionary keys (Alphabet), and for each dictionary key, the function will substitute it with its value (Number) and return it.
Finally, the code prompts the user to enter a 10-digit number, converts all the alphabets in the input to upper case using the .upper() method, The code calls the function and prints the result.
check attachment to see code in action.