Convert a string of numbers into a word

0

I need to do a function that receives a string consisting of numbers between 2-9 and the output is letters, ie basically the function will work as a keyboard of a mobile phone, where the number '2' will match with 'A', '22' will match 'B', until '9999' matches with 'Z'. For example:

>>> def teclas('22 2 333 33')
"CAFE"

The values entered in the arguments can "loop around" in the sense that if you enter '2222', the letter will return 'A', '22222' the letter will be 'B', etc. Just like it works on a phone with keys. Right now I have a dictionary where I assign all numbers to the corresponding letters like this:

dic={'2':'A', '22':'B', '222':'C', '3':'D'....}

My question is how do I, if I enter '2222' as an argument, reassign it to 'A' (I do not know how to do the 'turn around' program).

    
asked by anonymous 16.03.2016 / 14:18

1 answer

0

From your comments, I understand that you want to model a mobile keyboard.

If so, instead of a dictionary for all the keys, you should have a vector for each one.

tecla2 = ("A", "B", "C")
tecla3 = ("D", "E", "F")
# demais teclas...

When the user clicks a key, increment a counter and to find out which letter makes the counter module with the key vector size tightened.

Your example of "2222", for example, would become "i + = 4". Considering that the vector starts at 0: pos = (i - 1)% 3. The result would be position 0, which has the "A" as you want.

When the user selects another key, the counter must be reset.

Concept code below:

letras_tecla_2 = ("a", "b", "c")
letras_tecla_3 = ("d", "e", "f")

def string_from_teclas(teclas):
    ult_tecla = ""
    contador = 0
    string = ""

    for tecla in teclas:
        if tecla == ult_tecla:
            contador += 1
        elif ult_tecla != "":
            contador = contador % 3

            if ult_tecla == "2":
                string += letras_tecla_2[contador]
            elif ult_tecla == "3":
                string += letras_tecla_3[contador]

            contador = 0

        ult_tecla = tecla

    return(string)

print( string_from_teclas("22 222 3 33 ") )
    
16.03.2016 / 15:22