Convert a string of numbers into a word

Asked

Viewed 361 times

-1

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

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

The values entered in the arguments can "turn around", in the sense that if '2222' is inserted, the letter will return to 'A', '22222' the letter will be 'B', etc. As it works on a keystroke mobile phone. Right now I have a dictionary where I assign all the numbers to the corresponding letters like this:

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

My doubt is how I do so that in case it is inserted '2222' as argument, it is assigned again the value 'A' (I do not know how I do the program "go around").

  • Welcome to the Sopt community. Wasn’t it clear to me, reading numbers won’t be a direct dictionary? In case, provide '2222' will be the same as provide '2' and provide '22222' will be the same as provide '22'?

  • Exactly, if we imagine that we are typing on a mobile phone with keys, staying to click on the '2' key repeatedly will cause the letters to change consecutively, in the program this is what has to happen, in case of being entered the value '2222' read back as letter 'A'.

  • Ah! Do you want to make a mobile keyboard? Then I think your model is wrong. You should have a dic for each key. I will propose an answer.

1 answer

0

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

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

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

When the user clicks on a key, increment a counter and to find out which letter make the counter module with the vector size of the pressed key.

Your example of "2222", for example, would turn to "i += 4". Whereas 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 chooses 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 ") )
  • Ipacheco, your idea is valid, but remember that this code of yours does not create a dictionary. And that dictionaries do not have numerical indices. At least not for what I tested here. Modify your code to suit your response. =)

  • So being a vector what would it look like when calling the function? What would be the argument?

  • @Rubico, sorry, my Python is rudimentary, it was more like a pseudo-code. :-)

  • @Stagg, since no one gave a full answer, I developed the concept into a tiny little mongrel python. I’m sure you can get better from there. For example, put all the tuples in a hash per key and simplify the if that chooses the tuple.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.