Recognize words in python

1

I'm stuck on this problem.

Your little brother has just learned to write one, two and three, in English. You have written a lot of those words in a paper, your task is to recognize them. Note that your little brother is only a child, so he may make small mistakes: for each word, there might be at most one wrong letter. The word length is always correct. It is guaranteed that each letter he wrote is in lower case, and each word he wrote has a unique interpretation.

Input

The first line contains the number of words that your little brother has written. Each of the following lines contains a single word with all letters in lower-case. The words satisfy the constraints above: at most one letter might be wrong, but the word length is always correct. There will be at most 1000 words in the input.

Output

For each test case, print the numerical value of the word.

texto=str(input())
lista=texto.split()

for i in lista:
    if len(i)==5:
        print("3")
    if len(i)==3 and 
        print("2")
    else:
        print("1")

I clarify that I am very new and I am using Python 3.x, what I want to know is if there is a function that allows me to differentiate the letters because in the case of 1 and 2 the word has the same length. Thank you in advance !!

    
asked by Facu Magallanes 04.10.2018 в 17:22
source

1 answer

1

First, let's do an analysis:

The problem says that the child will write a word that represents a number, and this word will have as maximum a wrong letter, so we can deduce, that > there will always be two letters that are correct in the word.

Applying this, we can divide the text entry into a list that contains its letters, for example:

entrada = 'too'
division = ['t','o','o']

and compare the letters of this division with the letters of an original number, for example "two":

That is, compare ['t','o','o'] with ['t','w','o'] .

Since there will always be two letters that are equal, if the number of coincidences equals two, then, in this case, the word written by the child corresponds to 2, otherwise, by discarding, it is 1.

Applying it to serious code:

texto = str(input("Tu numero es: "))
texto_dividido = []
for i in texto:  # Aca se divide la frase en letras
    texto_dividido.append(i)
letras_one = ["o","n","e"] # Base que usaremos para comparar

contador_comparativo = 0  # Para contar la cantidad de coincidencias

contador_indice = 0  # Para avanzar en la lista
if len(texto) == 5:
    print ("3")
else:
    for i in letras_one: # Se comparan las letras de el numero original
                         # con las de la frase del usuario

        if i == texto_dividido[contador_indice]:
            contador_comparativo += 1
        contador_indice += 1
    if contador_comparativo == 2:
        print ("1")
    else: # Si no hay 2 coincidencias, por descarte es 2
        print ("2")

Same code without so much comment:

texto = str(input("Tu numero es: "))
texto_dividido = []
for i in texto:
    texto_dividido.append(i)
letras_one = ["o","n","e"]
contador_comparativo = 0  
contador_indice = 0
if len(texto) == 5:
    print ("3")
else:
    for i in letras_one:
        if i == texto_dividido[contador_indice]:
            contador_comparativo += 1
        contador_indice += 1
    if contador_comparativo == 2:
        print ("1")
    else:
        print ("2")

Exit with "nwo" ( two ):

>>> Tu numero es: nwo
2

Exit with "ine" ( one ):

>>> Tu numero es: ine
1

Regards

    
answered by 15.10.2018 в 02:04