Censor


#1



Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "hey" when it should return "*** *** ***".


I think I am close since I have one of the letters as an asterisk but I don't know how to get all of the letters to have asterisks. Also, do I need to append this to another string?


def censor(text,word):
    j = len(word)
    i = text
    k = ""
    if i.index(word, 0, len(i)) != -1:
        for l in word:
            print i.replace(l, "*") * j
        return word
        return i
print censor("hey hey hey", "hey")


#2

Misuse of a poorly understood built-in string method, replace(). Suggest scrap this idea and look for something more algorithmic.

The two return statements are both erroneous, and one is unreachable at any length.

When do we need the length of the word? Once, in the if statement. No need to cache it. Do we really need a copy of text? Not at all, if we never mutate it (attempt to change it).

Your approach appears to be doing it letter by letter, which is taking it to an extreme for such a simple problem.

Given a phrase and a word to censor out of it, and using the replace method approach,


>>> def censor (text, word): return text.replace(word, '*' * len(word))

>>> censor("hey hey hey", "hey")
'*** *** ***'
>>>

Not much practice or thought process in that, though, is there? Think of an iterative approach that does not use any built-in so there is some real practice, and real learning.


#3

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