Find and delete different characters between strings

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

How do I check if the word in lst1 exists in some substring of lst2. If there is a delete of the surplus characters of the word that has the substring, making the string in lst2 equal to that of lst1. Without using size comparison logics or just playing the right value in other variables . I need the character or substring to be identified to be deleted.

Expected result:

lst2 = ["carro", "carro"]
    
asked by anonymous 05.07.2017 / 22:12

4 answers

0

I solved my problem using two functions that I developed. The First would pick up a list as a comparison parameter that would be modelos and the word that would be compared to palavra . It would return what was found as an anomaly, in the 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 get "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 you can correct the strings since the anomaly is on the left, as was the case for me.

    
12.07.2017 / 19:07
0

I do not understand why doing this, 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

Output:

  

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

    
05.07.2017 / 22:30
0

Since you can not simply define a variable equal to another, I created a function to manually remove the excesses, here's 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

    
05.07.2017 / 23:13
0

Here is my solution (based on answer from Josà © Henrique , but with a few changes):

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 can handle some more cases.

Cases where the extra characters are removed:

  • remove_caracteres("carro", "carroa")
      

    "carro"

  • remove_caracteres("carro", "carrooao")
      

    "carro"

  • remove_caracteres("carro", "carraao")
      

    "carro"

Cases where the no characters 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("", "")
      

    ""

06.07.2017 / 12:35