mdemont
mdemont

Reputation: 107

Codecademy Practice Makes Perfect 10/15 (Word Censoring)

I'm on Codecademy, the section called "Practice Makes Perfect", on problem 10/15, the word-censoring one. The problem goes like this:

Write a function called censor that takes two strings, text and word, as input. It should return the text with the word you chose replaced with asterisks.

My idea was to do this:

def censor(text, word):
    length_of_word = len(word)
    word_now_censored = '*' * length_of_word
    wordlist = text.split()
    for item in wordlist:
        if item == word:
            item = word_now_censored
    return " ".join(wordlist)

But, so it seems, changing the value of item in the for loop doesn't change the value of the item in the list.

I thought another way could be to use a while loop, going from i = 0 to i < len(wordlist), and then modify wordlist[i] as needed, but I'd just like to understand why my for-loop method doesn't work.

Upvotes: 3

Views: 15169

Answers (16)

Matthew Culiat
Matthew Culiat

Reputation: 1

I'll appreciate it if you will take a look at this one.

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

Upvotes: 0

lifeisabadcode
lifeisabadcode

Reputation: 1

I've made it quite simple and I don't see why no one mentioned this.

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

Upvotes: 0

Hai Tran
Hai Tran

Reputation: 71

My idea was just:

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

This is might be simple one but I think simple is good. And I didn't have to use any loop, is it good? If you like my answer, please let me know, I'll be really happy. Thank you

Upvotes: 0

Sreekanth
Sreekanth

Reputation: 87

def censor(text, word):
    a = word
    b = len(a)
    for a in text:
        text = text.replace(word, "*"*b)
    return text 

Upvotes: 0

user5835171
user5835171

Reputation: 1

def censor(text, word):
    new_text = text.split()
    ctext = []
    for item in new_text:
        if item == word:
            item = "*" *len(word)
        ctext.append(item)
    return " ".join(ctext)

Upvotes: 0

Albert Zhao
Albert Zhao

Reputation: 9

def censor(text,word):
    text=list(text)

    for n in range(0,len(text)):
        i=0
        while 1==1:
            for i in range(0,len(word)):
                if  text[n+i]==word[i]:
                    i+=1
                else:             
                    break
            if i==len(word):
                for m in range(0,i):
                    text[n+m]='*'
            else:
                break
        n+=i
    return "".join(text)

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

Upvotes: 1

unleu
unleu

Reputation: 13

def censor(text, word):
lista=[]
for i in text.split():
    if i==word:
        lista+=['*'*len(word)]
    else:
        lista+=[i]
return ' '.join(lista)

Upvotes: 0

user5351257
user5351257

Reputation: 1

def censor(text,word) :
    c=''
    for i in text.split() : 
        if i == word : 
             i = "*" * len(word)
             c += ' ' + i
        else :
            c += ' ' + i
    return c       


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

Upvotes: 0

whatwhere50
whatwhere50

Reputation: 3

Just solved it and this was my solution:

def censor(text, word):
   textList = text.split()

   for index, var in enumerate(textList):
       if var == word:
          textList[index] = "*" * len(word)

   return " ".join(textList)

Upvotes: 0

prabhuraj kanche
prabhuraj kanche

Reputation: 1

def censor(text,word):
res = text.split()
temp = ""
for i,j in enumerate(res):
    if j == word:
        res[i] = "*" * len(word)
return " ".join(res)

Upvotes: 0

Coos
Coos

Reputation: 1

Here's my version. Simply build a new word of asterisks the same length as the word, then replace it.

def censor(text, word):
if word in text:
    blabber = ""
    while True:
        blabber += "*"
        if len(blabber) == len(word):
            break
    return text.replace(word, blabber)

Upvotes: 0

akshaynagpal
akshaynagpal

Reputation: 3147

censor takes two strings, text and word, as input. It returns the text with the word you chose replaced with asterisks.

def censor(text,word):
    result = ""
    count = 0
    no_of_stars = 0
    split_list = text.split()
    for i in split_list:
        count += 1
        if(i==word):
            result += "*" * len(i)
        else:
            result +=i
        if(count != len(split_list)):
            result += " "    
    return result   

Upvotes: 0

user3903111
user3903111

Reputation:

Here is another version:

def censor(text, word):
    lst = text.split()
    while word in lst:
        index = lst.index(word)
        lst.remove(word)
        lst.insert(index,'*' * len(word))
    return " ".join(lst)

Upvotes: 0

BeetDemGuise
BeetDemGuise

Reputation: 974

You could simply use re.sub to replace all instances of word:

import re

def censor(text, word):
    return re.sub(r'\b{}\b'.format(word), '*' * len(word), text)

Upvotes: 4

shaktimaan
shaktimaan

Reputation: 12092

Your observation is right

changing the value of item in the for loop doesn't change the value of the item in the list.

There are many ways to go about this. Here is one way. Create another variable new_words_list. Append the word from wordlist to new_words_list if it not word. Else append word_now_censored to new_words_list.

Which translates to:

def censor(text, word):
    length_of_word = len(word)
    word_now_censored = '*' * length_of_word
    wordlist = text.split()
    new_words_list = []
    for item in wordlist:
        if item == word:
            new_words_list.append(word_now_censored)
        else:
            new_words_list.append(item)

    return " ".join(new_words_list)

Upvotes: 1

Stefan van den Akker
Stefan van den Akker

Reputation: 6999

Change it to this:

for index, item in enumerate(wordlist):
    if item == word:
        wordlist[index] = word_now_censored

Upvotes: 6

Related Questions