Find and delete different characters between strings

Asked

Viewed 634 times

-1

lst1 = ["carro"]
lst2 = ["carroa", "carrooao"]

How do I check if the word in lst1 exists in any substring of lst2. If it exists delete the excess characters of the word that has the substring, making the string lst2, equal to lst1. Do not use size comparison logic or just play the right value on the other variables. I need the character or substring to be identified to be deleted.

Expected result:

lst2 = ["carro", "carro"]
  • 2

    If you want the result to be three strings it is not easier to copy the contents of the first string?

  • If the contents of a str4 = "carraao" the result should be "carro" also?

  • @Joséhenriqueluckmann Yes

  • @LINQ No, logic needs to find different characters to be deleted.

  • What if they’re out of order? Like, "I believe"

  • @Joséhenriqueluckmann does not apply, in my case the amendment will always be at the end right.

  • Totally non-sense, just do: str2=str3=str1 :-)

  • @Sidon I think I was unclear, I changed the text of the problem to make more sense to you.

  • @Gmattos, just one more question, and if the string doesn’t have all the letters, for example : carraaa

  • Ah ok, now is another question, although it remains nonsense. :-)

Show 5 more comments

4 answers

0

Here is my solution (based on reply of José Henrique, but with some amendments):

def remove_caracteres(modelo, palavra):
    i = 0 # Índice na palavra a ser testada
    n = 0 # Número de caracteres do modelo encontrados na palavra testada
    for letra in modelo:
        while i < len(palavra):
            if letra == palavra[i]:
                i += 1
                n += 1
                break
            i += 1
    if n == len(modelo):
        return modelo
    return palavra

The difference is that this function is able to treat some more cases.

Cases where extra characters are removed:

  • remove_caracteres("carro", "carroa")

    "carro"

  • remove_caracteres("carro", "carrooao")

    "carro"

  • remove_caracteres("carro", "carraao")

    "carro"

Cases where the extra characters nay are removed:

  • remove_caracteres("carro", "carraaa")

    "carraaa"

  • remove_caracteres("carro", "acrro")

    "acrro"

  • remove_caracteres("carro", "caro")

    "caro"

  • remove_caracteres("carro", "carr")

    "carr"

Extreme cases:

  • remove_caracteres("", "carroa")

    ""

  • remove_caracteres("carro", "")

    "carro"

  • remove_caracteres("", "")

    ""

0

I didn’t understand why to do it, but anyway, it can be done like this:

str1 = "carro"
str2 = "carroa"
str3 = "carrooao"

if ((len(str1) < len(str2)) and (str2[:len(str1)] == str1)):
    str2 = str1;

if ((len(str1) < len(str3)) and (str3[:len(str1)] == str1)):
    str3 = str1;

print str1
print str2
print str3

Exit:

str1 = "car"
str2 = "car"
str3 = "car"

  • This code does not work for a string like "carraao"

  • Logic needs to check if the character is different to be deleted. Sorry, I was not very clear. In your case you are only checking the size and excluding from the len()

  • You cannot use size comparison. Nonsense, do str2=str3=str1

0

Since you cannot simply set one variable equal to another, I created a function to manually remove the excesses, follow an example:

def remove_letras(modelo, palavra):
  #passa letra por letra verificando se 
  #existe alguma correspondende e vai removendo 
  #as que não correspondem
  atual = 0
  for letra in modelo:
    palavra = palavra[:atual] + palavra[palavra.find(letra):]
    atual += 1
  #remove os excessos do final
  return  palavra[:atual] 


#TESTES
str1 = "carro"
str2 = "carroa"
str3 = "carrooao"
str4 = "carraao"

str2 = remove_letras(str1, str2)
str3 = remove_letras(str1, str3)
str4 = remove_letras(str1, str4)

print str1
print str2
print str3
print str4

Follow the test link: Repl.it

0


I solved my problem using two functions I developed. The first one would take a list as a comparison parameter that would be modelos and the word that would be compared as palavra. It would return what was found as anomaly, in case "Carroa", would return "a".

def clnw(modelos, palavra):

    for a in modelos:

       palavra = palavra.replace(a, "")

    lx = palavra

   if lx != "":
       return lx 
   else: return ""  

The function below would take the "a" as rst and the word that would be corrected as plvr.

def fixw(rst, plvr):
       st = plvr.rfind(rst)
       fn = len(plvr) -1

       plvrl = list(plvr)
       rstl = list(rst)

    while fn >= st:

        del plvrl[fn]

        fn = fn-1
    fix = ''.join(plvrl)

    return fix

Returning "Carro", with these logics can fix the strings as long as the anomaly is left, as was my case.

Browser other questions tagged

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