Censor: letter by letter


#1


Censor exercise:

Code works. An alternate way to do things letter by letter.


def censor(text, word):
    i = 0
    tempstr = ""
    while i < len(text):
        j = 0
        j2 = 0
        while j < len(word) and j2 < len(word):
            if text[i+j] == word[j]:
                j += 1
                j2 += 1
            else:
                j2 += 1
        if j == len(word):
            k = 0
            while k < len(word):
                tempstr += "*"
                k += 1
                i += 1
        else:
            tempstr += text[i]
            i += 1
    return tempstr
print censor("All dogs dance in the rain with other dogs and sometimes cats, but definitely dogs.", "dogs")


#2

Normally we would consider this as a posted solution and delete it. In this case, while it might work, anything this complex is not an alternative but an example of what not to do. Clever as it is, it is too much.


#3

Yes, it's awful! But I spent so long on it, I really wanted to share. I started this way because I couldn't think of another way, and down the rabbit hole I went. I'm currently working on more efficient means.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.