I need a help in the code, I need to count how many changes have been made in the sentence, you have some idea to pass me.this code is to remove repeated substrings that are at the end of the sentence, now I need to count how many changes were made in the " replace "from the code in the list.
def corrigePalavra(str):
palavra = [str[-1:], str[-2:], str[-3:], str[-4:]]
result = str
palavra_modificada = False
for w in palavra:
if result.count(w) > 1:
result = result.replace(w * result.count(w), w, 1)
palavra_modificada = True
return palavra_modificada, result
lista1 = ['programaramar ee legalal','python ee showow','linguagemem de programacaocao']
aux2 = []
cont_palavras_modificadas = -1
for i in lista1:
aux1 = i.split()
for j in aux1:
palavra_modificada, x = corrigePalavra(j)
aux2.append(x)
if palavra_modificada:
cont_palavras_modificadas += 1
b = " ".join(aux2)
print(cont_palavras_modificadas, b)
Exit from my code:
2 programar e legal
4 programar e legal python e show
6 programar e legal python e show linguagem de programacao
Correct Exit:
3 programar e legal
2 python e show
2 linguagem de programacao
ie 3 occurrences in the first sentence, 2 in the second and 2 in the third.