Censor - so trivial?


Is it really so trivial?

def censor(text, fword):
        return text.replace(fword,"*"*len(fword))

print censor("what the duck, motherducker?!","duck")


what the ****, mother****er?!?!


If you use built in function like that, yes, it is really so trivial.

If you find this to trivial, try without replace(), this should be more challenging, since now you have to code instead of a built in function doing everything for you


In previous tasks, there is a note to not use specific functions (like "reversed"). In this one I do not find that kind of restrictions... Thats why i am little startled :wink:

I`ll try not to use replace(), thanks in advance!


there is only a restriction in the reverse, but some of the others can also be pretty resolved much simpler with the help of built in functions.

it does take out the fun if you use built in functions. There is even a median function which you can import from numpy :stuck_out_tongue:


@stetim94 , please rate:

def censor(text, fword):
    for x in text:
        if x==fword:
    return " ".join(text)

but this will not work so well like previous one...


you could also consider using range()? this will give you indexes of text. If you want you can try this.

Or use enumerate, which gives both list items and indexes.

or you can make an empty list and use if/else to determine if a word needs censoring, and append to the empty list.

So many options, i find it hard to judge which is the right one


If the last word in the text is an fword, that will not get the **** print.


Provided that a user's censor function passes Codecademy's submission correctness test (SCT), it could be considered a correct solution, even if it seems trivial. The important issue really comes down to what the user learns by doing the exercise. Clearly, @cwysocki, the amount of thought you have put into this exercise has made it a valuable exercise for you and for those who have responded.

The instructions and the submission correctness test (SCT) for this exercise are unclear as to what constitutes a correct solution. It leaves open the question of what should delimit the individual words in the text string. For example, if we write a censor function, and then make this call ...

print censor("this hacker is wack hack", "hack")

... a censor function that produces this output (result 1) can pass ...

this hacker is wack ****

... and a censor function that produces this output (result 2) can also pass ...

this ****er is wack ****

The two results are obviously different, however both versions of the function pass.

The solution in the original post by @cwysocki would produce result 2.

The following censor function passed and produced result 1...

def censor(text, word):
    return " ".join(["*" * len(w) if w == word else w for w in text.split()])

That version of the function uses whitespace to separate words.

The SCT does include a model function named answer that is used to test the user's censor function. It uses whitespace to distinguish individual words, therefore if the following call were made ...

print answer("this hacker is wack hack", "hack")

... the result would be ...

this hacker is wack ****

However, none of the test cases in the SCT include any words that have the censored string embedded within.


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