10.censor


#1
My code

def censor(text, word):
    result_word = ""
    splitted = text.split()
    for words in splitted:
        if words == word:
            "*".join(result_word)
        elif words != word:
            words.join(result_word)
    return result_word
    
print(censor("test quiest", "test"))

Error:
Your function fails on censor("hey hey hey","hey"). It returns "" when it should return "*** *** ***".


#2

.join() is to join a list into a string:

print " ".join(["hello", "world"])

if you want to concatenate strings, just use +:

"hello " + "world"

#3

Hello,

I think it'll be simpler if we use str.replace("","")

Example:
str = "this is string example....wow!!! this is really string"
print str.replace("is", "was")
Output ---> thwas was string example....wow!!!

def censor(argument1, argument2):

We have 2 argument. So we can use argument2 to replace words in text.

How to count number of letters??

We can use len(). We'll see the number of letter.

Afterthat we can multiplied the symbol "" (Ex: 10 * "")

Have fun!


#5

sorry, in the task no anything about method ".replace", so i won't use it.


#6

Can't really imagine how it helps.


#7

don't use join in your loop, use + to concatenate strings

i wasn't giving too much hints, given the fact you might wanted to re-think your solution now that you hopefully understand .join() isn't a good idea


#8

Hint in the brief tells that:

You can use

string.split()
# and 
" ".join(list)

to help you here.


#9

true, if result_word was a list, you could use join() after the loop to join the list into string, you shouldn't use it inside the loop

but in your case, result_word is a string

in both cases, don't use join inside the loop


#10

My code

def censor(text, word):
    result_word = ""
    splitted = text.split(" ", 1)
    for words in splitted:
        if words == word:
            lenofword = len(words)
            result_word += "*" * lenofword
            #"*".join(result_word)
        elif words != word:
            result_word += " " + words
            #words.join(result_word)
    return result_word

Error:
Your function fails on censor("hey hey hey","hey"). It returns "*** hey hey" when it should return "*** *** ***".


#11

here:

splitted = text.split(" ", 1)

why did you add 1 as second argument? It causes your string to be not be splitted correctly:

# desired
['hey', 'hey', 'hey']
# your split result
['hey', 'hey hey']

#12

Okay, some versions.

First version

    result_word = ""
    splitted = text.split(" ")
    for words in splitted:
        if words == word:
            lenofword = len(words)
            result_word += "*" * lenofword 
            #"*".join(result_word)
        elif words != word:
            result_word += " " + words
            #words.join(result_word)
    return result_word[/details]

First version error
Your function fails on censor("hey hey hey","hey"). It returns "******" when it should return " *** ***".

Second, i just add + " " at end of first condition in cycle.

Second code

def censor(text, word):
    result_word = ""
    splitted = text.split(" ")
    for words in splitted:
        if words == word:
            lenofword = len(words)
            result_word += "*" * lenofword + " "
            #"*".join(result_word)
        elif words != word:
            result_word += " " + words
            #words.join(result_word)
    return result_word

Second error:
Your function fails on censor("hey hey hey","hey"). It returns "*** *** *** " when it should return "*** *** ***".


#13

the second is pretty close, you only have an additional space at the end of your sentence:

***_***_***_

i replaced the spaces with underscores, so you can see it. You learned string slicing, maybe you can use it here to get rid of the trailing space?


#14

Hm...

updated code

def censor(text, word):
    result_word = ""
    splitted = text.split(" ")
    for words in splitted:
        if words == word:
            lenofword = len(words)
            result_word += " " + "*" * lenofword + " "
            #"*".join(result_word)
        elif words != word:
            result_word += " " + words
            #words.join(result_word)
    lenofword_2 = len(result_word)
    return result_word[1:lenofword_2-1]

Error!
Your function fails on censor("hey hey hey","hey"). It returns "*** *** " when it should return " *** ***".


#15

this line:

result_word += " " + "*" * lenofword + " "

no need to add even more spaces at the end

then slice off the first character (additional space) of your string and return it


#16

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