10. censor


#1

I'd appreciate if someone told me what is wrong with this code:

def censor(text, word):
    text = text.split()
    for w in text:
        if w == word:
            n = len(word)*'*'
            text_new = str(text).replace(word, n)
        else:
            text_new = text
    return " ".join(text_new)

The error says: Oops, try again. Your function fails on censor("hey hey hey","hey"). It returns "[ ' * * * ' , ' * * * ' , ' * * * ' ]" when it should return "*** *** ***".
So it looks like .join method is not working for me but I can't figure out why...


#2

i see a mishmash of different approaches used to solve this problem.

you split the string into a list here:

text = text.split()

but then later you use replace:

str(text).replace(word, n)

which is a method for strings, not lists.

Which way do you want to go? i can help you with both, but this code goes in two (if not three) different directions

How do you plan to design this program? Can you walk me through it?


#3

I split the string so that I can do loop and iterate over each word - to separate those I want to replace but then I got back to str again as replace method works for strings only so I thought I should have each word as a separete string and finally join them all together. But yeah, I see now my logic is a bit messy. Would it be possible to write it without splitting text?


#4

yes, you could use replace(), but this is not fun.

Lets go for the text split. There are two things you can do:
1. make an empty list (lets call this list text_new) before the loop, use if to determine if the word requires censoring, then append n (which contains your asterisks) to new_text, else append the uncensored word to text_new
2. use range() in your for loop, so you get indexes so you replace words in text if they require censoring

in both case, join the string and return it


#5

I tried the first one. Looks like join method is fine now, but the iteration stops after getting to first hey.

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

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


#6

are you sure return is indented correctly? It should be outside the for loop, It appears this way. I ran your code and got a pass, try refreshing the page


#7

Right, refreshing helped! Thanks a lot!


#8

you probably submitted so many times, your browser might be sending cached data. Do you understand your solution and are you happy with it?


#9

yes, I do understand it. Now it looks so clear and simple, though at first glimpse it sounded complicated (or I made it so in my head). Still a long way to go.


#10

actually, it is complicated. But i told you the steps you needed to take, that makes it easier. So my advice for you, next time is to break the problem down into steps.

Make small steps, then put it together. See how you can approach a step, what do i need? list? string? integers? loop? and so on


#13

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