Censor


#1

I dont know what is wrong here in my code. The output is what it is written in the question but the IDE is giving me this error: Your function fails on censor(“hey hey hey”,“hey”). It returns " " when it should return " ".

def censor(sentence, word):
  split_word = sentence.split()
  count = len(word)
  new_sentence = ""
  for i in split_word:
    if i == word:
      new_sentence = new_sentence + " " + ("*" * count)
    else:
      new_sentence = new_sentence + " " + i 
  return new_sentence
  
print censor("hey hey hey","hey")

#2

if we replace the spaces with underscores we can see the problem:

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

you have a leading space which shouldn’t be

the simple solution would be to use string slicing to remove the leading space

The problem with your solution is that strings are immutable, so concatenating a string results in a new string with the combined result, so it would be better to use a mutable data type, like a list, then to get from list to string you can use .join(), which also solves your little space problem


#3

I didnt get your point. Can you please give an example?


#4

for censor("hey hey hey","hey") the function should return:

*** *** ***

however, your function returns:

 *** *** ***

which is not the same, you have a leading space, lets do above examples again but replaces the spaces with underscores so we can see the problem:

# correct output for censor("hey hey hey","hey")
***_***_***
# your output for censor("hey hey hey","hey")
_***_***_***

you need to get rid of this leading space, now read my earlier answer again


#5

I have got my solution but the other way:

def censor(sentence, word):
  split_word = sentence.split()
  count = len(word)
  new_sentence = ""
  for loop_word in split_word:
    if loop_word == word:
      new_sentence = new_sentence + " " + "*" * count
    else:
      new_sentence = new_sentence + " " + loop_word
    lead_space = new_sentence[1:]
  return lead_space
  
print censor("thi is is is are is", "is")
  
 

BUT, I want to know your solution as well which I was unable to get it. Can you please share yours?


#6

the string slicing which you did here:

lead_space = new_sentence[1:]

was a solution i suggested.

I just did two suggestions on how you could solve the problem, the other solution would be making new_sentence a list. then use .append() rather then string concatenating, the design of the program pretty much stays the same. Few small changes


#7

Here I got it all of the things summed up in the code below Thanks alot :smile:

for loop_word in split_word:
    if loop_word == word:
      new_sentence.append("*" * count)
    else:
      new_sentence.append(loop_word)
  str1 = "_".join(new_sentence) 
  lead_space = str1
  return lead_space

#8

here:

str1 = "_".join(new_sentence) 

why use a underscore as binding character? a space would make more sense

you can directly return str1, why would you do lead_space = str1??


#9

Hmm. I used “_” for checking the leading space. And yes, I could have returned str1 directly instead of assigning it into some other variable. Great :smile:


#10

fair enough

you could even put return and join at the same line if you want


#11

Yes, that is right !! Thanks alot for your pro guidance.


#12

After a lot of going back through exercises and looking around forums, I came across this really nice function called enumerate. Enumerate seems to walk through a list, returning each item and it’s corresponding index, this will allow the
code to pick up multiple occurrences of a specific word to be replaced in the text.

In the code below; the variable ‘index’ is the position within the list ‘work_text’ of each word within the list as it’s encountered, the variable ‘found word’ is the text at that index position. This means a simple comparison and replacement
can then be done to complete the function.

I really hope is helps! This one had me scratching my head for quite a while this afternoon :slight_smile:


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

#13

you could nearly achieve the same result by simply using range (a function you learned):

for index in range(len(work_text)):
    if work_text[index] == word:

yes, enumerate is a bit cleaner true.

This are the two common ways to solve the censor problem. coursepro94735 solution which involves a new list, then use if to determine if word need censoring, if so, append asterisks to new list, otherwise just the uncensored word

or activedataaus solution, which involves updating words which requires in the existing list.

the advantage of coursepro94735 solution is that we can refactor the code with list comprehension. (making the code much shorter), that is something for later


#14

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