vendredi 29 janvier 2021

how do I take out index and not all of the char in a string?

I want to instruct the computer so that if a character in string 'a' is also a character in string 'b', to remove that character (just that one character), however what the code below is doing is removing all of the same characters from the string at one go. Any help?

I have tried to use a[I] and b[I] to replace the index for each loop, but then the code below needs to be changed entirely, because I can no longer do 'for I in a' and 'if I in b', as I need integer references for the 'i's.

e.g. a = "askhfidshf" b = "fhsdhfojej"

def permutation_checker(a,b):
    if a==b:
        print("a is a permutation of b")

    if a != b:
        while a and b:
            for i in a:
                if i in b:
                    new_a = a.replace(i,"")
                    print(new_a)
                    a = new_a
                    new_b = b.replace(i,"")
                    print(new_b)
                    b =new_b

                    if (a=="") and b == "":
                        print("a is a permutation of b")

Aucun commentaire:

Enregistrer un commentaire