10/15 Censor - What am I doing wrong?


#1

Here's what I have:

def censor(text, word):
text_split = text.split()
join_text = []
for i in range(len(text)):
    if text_split[i] == word:
        join_text.append("*"*len(word))
    else:
        join_text.append(text_split[i])
return " ".join(join_text)

It seems I'm getting an index error. What am I doing wrong? Thanks!


#2

This route seemed easier...wasn't comfortable with the splitting.

This works just as well using the replace operator.

def censor(text, word):
censored_word = ""
for c in word:
    censored_word += "*"
if word in text:
   new_text = text.replace(word, censored_word)
return new_text

#3

@mmartin712 this is why you get that error. check out this excerpt below

#imagine I have text
text = "they know they know they know"

#call len on text
print len(text)   #29

#now i split text. default delimeter is white spaces
text_split = text.split() #['they', 'know', 'they', 'know', 'they', 'know']

#call len on text_split
print len(text_split) #6

Now the problem in your code is the for loop you are using range(len(text)) which is probably bigger than the range(size) of text_split(which is a list) so when you write

if text_split[i] == word:

you will get an error when you the index i is bigger than the size of text_split. Now with all that said all you have to change is one thing in your code.
Change the for loop block to

for i in range(len(text_split)):

#4

I found this work, try it

def censor(text, word):
if word in text:
new_text = text.replace(word, "*" * len(word))
return new_text


#5

That was it. Thank you!


#6

**good job


def censor(text,word):
n=len(word)

c=text.replace(word,'*'*n)
return c

print censor('anand is a good boy','good')


#7

I guess the point of all these exercises isn't cheating and trying to use built-in functions, but doing them the long way to practice fundamentals! So you had to write something like this instead:

def censor(text, word):
    words = text.split()
    equivalent = "*" * len(word)
    for index, item in enumerate(words):
        if item == word:
            words[index] = equivalent
    return " ".join(words)

#8

Personally I think you should use range(0,len(text)) on line four. Maybe that will help.


#9

The program is working correctly. And your proposal doesn't really change much:

for index in range(0, len(word)):
    if words[index] == word:
        words[index] = equivalent

#10

this is how i did it:

def censor(text,word):
answer=[]
chop=text.split()
for x in chop:
if x==word:
y=''len(word)
answer.append(y)
else:
answer.append(x)
return ' '.join(answer)


#11

I like how clean this is, but how would you go about editing it so that the censored word will be filtered out regardless of whether the letters are uppercase or lowercase? i'm just not sure where to put ".lower" is what I'm getting at.

edit: ahhh, sorry this appeared so low on the page, it was in reference to javacoder41347's post. i'm not sure how to properly comment yet, this is my first one


#12

Here I have some 'clean' code for you!

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

This as about as concise as you can get this code.

Good luck and have fun!


#13
import time
start_time = time.time()
def censor(text,word):
    a = text.split( )
    b=[]
    for i in range(len(a)):
        if a[i] == word:
            b.append("*" * len(word))
        else:
            b.append(a[i])
    return " ".join(b)
print("--- %s seconds ---" % (time.time() - start_time))

#14

Nice try! And I found out that with out the "if" part the method also works.

def censor(text, word):
new_text = text.replace(word, "*" * len(word))
return new_text

#15

without using replace (only using functions that hint thing give me):

def censor(text, word):
    censored = []
    for w in text.split():
        if w == word:
            censored.append('*' * len(word))
        else:
            censored.append(w)

    return " ".join(censored)

#16

string.replace() is great but it will replace every instance of the keyword, whether it is a separate word or not.
For eg: text ="this cat is called catherine"
word = "cat"


#17

I tried and find this works:

def censor(text, word):
text = text.lower() # text in lowercase
word = word.lower() # word in lowercase
if word in text:
new_text = text.replace(word, "*" * len(word))
return new_text


#18

Hey! This is what I came up with, and then I thought I would look around for other soltions. I've posted a question, because this solution displays properly, but doesn't actually pass :frowning:


#19

def censor(text, word):
if word in text:
return text.replace(word, "*" * len(word))


#20

@munim_uiu refer to @zeziba's code in this same thread on post #12 to see how you can improve your code