Home > Mobile >  How to translate numeric code to words using dictionary
How to translate numeric code to words using dictionary

Time:04-26

I have to make this program that receives a coded string and returns the normal text. For example:

input = 42 32 53 53 63 *43 *21 61 *61 21 73 52 
output = hello i am mark

I have this dictionary with the equivalences:

keypad= {'21': 'a', '22': 'b', '23': 'c', '31': 'd', '32': 'e', '33': 'f', '41': 'g', '42': 'h', '43': 'i',
           '51': 'j', '52': 'k', '53': 'l', '61': 'm', '62': 'n', '63': 'o', '71': 'p', '72': 'q', '74': 'r',
           '74': 's', '81': 't', '82': 'u', '83': 'v', '91': 'w', '93': 'x', '93': 'y', '94': 'z', '*': ' '}

I need to use it as it is. I think I need to iterate each number and get the key in the dictionary and add it to an empty string, but I have no idea on how to do it. If anyone has an idea I'd be really thankful.

CodePudding user response:

I noticed that you have some missing keys in your dictionary. you need to fix that first then try this:

for item in inp.split():
    if item.startswith("*"):
        print(keypad["*"], end="")
        item = item[1:]
    print(keypad[item], end="")
print()

You first check to see it the number starts with a star, if so you need to print the value of * separately, then with item = item[1:] you change *N to N and continue. If you don't want to directly print to the stdout, you could have an empty string variable and instead of printing just concatenate the values with that string:

result = ""
for item in inp.split():
    if item.startswith("*"):
        result  = keypad["*"]
        item = item[1:]
    result  = keypad[item]
print(result)

This could also be writen using generator expression if you are interested:

print(
    "".join(
        f"{keypad['*']}{keypad[i[1:]]}" if i.startswith("*") else keypad[i]
        for i in inp.split()
    )
)

CodePudding user response:

I hope this will help you.

input_value = input('Write something: ').strip().split(' ')


keypad= {'21': 'a', '22': 'b', '23': 'c', '31': 'd', '32': 'e', '33': 'f', '41': 'g', '42': 'h', '43': 'i',
           '51': 'j', '52': 'k', '53': 'l', '61': 'm', '62': 'n', '63': 'o', '71': 'p', '72': 'q', '73': 'r',
           '74': 's', '81': 't', '82': 'u', '83': 'v', '91': 'w', '93': 'x', '93': 'y', '94': 'z', '*': ' '}
output = ""
for a in input_value:
    if '*' not in a:
        output = keypad[a]
    elif a.startswith('*'):
        output =' '
        output =keypad[a[1:]]
    elif a.endswith('*'):
        output =keypad[a[:-1]]
        output =' '
print(output)

I know the above code gives you an error when the key is not in the dictionary Then You can use this one.

input_value = input('Write something: ').strip().split(' ')


keypad= {'21': 'a', '22': 'b', '23': 'c', '31': 'd', '32': 'e', '33': 'f', '41': 'g', '42': 'h', '43': 'i',
           '51': 'j', '52': 'k', '53': 'l', '61': 'm', '62': 'n', '63': 'o', '71': 'p', '72': 'q', '73': 'r',
           '74': 's', '81': 't', '82': 'u', '83': 'v', '91': 'w', '93': 'x', '93': 'y', '94': 'z', '*': ' '}
output = ""
for a in input_value:
    try:
        if '*' not in a:
            output = keypad[a]
        elif a.startswith('*'):
            output =' '
            output =keypad[a[1:]]
        elif a.endswith('*'):
            output =keypad[a[:-1]]
            output =' '
    except KeyError:
        pass
print(output)

CodePudding user response:

Another option:

  1. Split on space, loop
  2. Split on digits, loop
  3. Check if key exist in keypad
  4. Append to output
import re

input = '42 32 53 53 63 *43 *21 61 *61 21 73 52'
keypad= {'21': 'a', '22': 'b', '23': 'c', '31': 'd', '32': 'e', '33': 'f', '41': 'g', '42': 'h', '43': 'i', '51': 'j', '52': 'k', '53': 'l', '61': 'm', '62': 'n', '63': 'o', '71': 'p', '72': 'q', '74': 'r', '74': 's', '81': 't', '82': 'u', '83': 'v', '91': 'w', '93': 'x', '93': 'y', '94': 'z', '*': ' '}

result = ''
for i in input.split(' '):
    for j in list(filter(None, re.split('(\d )', i))):
        if j in keypad:
            result  = keypad[j]
        else:
            result  = '?'
           
print(input)
print(result)
42 32 53 53 63 *43 *21 61 *61 21 73 52
hello i am ma?k

Since key 73 does not exist in keypad, it is decoded as an ?, you can change the else to the desired action when the key does not exist.

Try it online!

  • Related