3

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.

JeremyK12
  • 71
  • 6
  • What did you try? [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – 0stone0 Apr 26 '22 at 09:05

3 Answers3

1
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)
Dada
  • 6,313
  • 7
  • 24
  • 43
codester_09
  • 5,622
  • 2
  • 5
  • 27
  • 1
    If you decided to silence missing keys(which I'm against to), at least don't do that this way. `except KeyError` is enough. Yours is too broad. – S.B Apr 26 '22 at 09:26
  • @SorousHBakhtiary I give both solutions one with the error and one that is not. This depends on the user which one he/she is using. – codester_09 Apr 27 '22 at 07:09
1

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()
    )
)
S.B
  • 13,077
  • 10
  • 22
  • 49
  • This will give an error when the `key` is not in the `dict`. – codester_09 Apr 26 '22 at 09:21
  • 2
    @SharimIqbal Then OP should decide what to do, raising exception is helpful in some situations. It indicates that the input is not valid. I don't think it's a good idea to silence all exceptions in every scenarios. – S.B Apr 26 '22 at 09:23
0

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!

0stone0
  • 34,288
  • 4
  • 39
  • 64