Censor


#1

Thanks a lot for the help.
I wrote the code and in screens seems to be working as expected. However, the validation process does not.
CODE;

def censor(text, word):
  n = text.split()
  full = ""
  for tex in n:
    if tex == word:
      size = len(tex)
      tex = "*" * size
    full= full + " "+ tex
  print full
  return full

Could you please let me know if i am in the right path?


How can I use split( ) to make censor?
#2

you have a leading space, we can see this when replacing the spaces with underscores:

def censor(text, word):
  n = text.split()
  full = ""
  for tex in n:
    if tex == word:
      size = len(tex)
      tex = "*" * size
    full= full + "_"+ tex
  print full
  return full
print censor("hey hey hey", "hey")

its why the exercise is rejecting your code


#3

Yeah, the first time you call full = full + " " + tex , full is empty, so you’re putting a space at the beginning of the line. You might want to use join() to put a space between each word.

Tbh, I don’t know why everyone seems to be doing this with looping, I think something like this would work too:

def censor(text, word):
  parts = text.split(word)
  stars = '*' * len(word)
  ntext = stars.join(parts)
  return ntext

#4

look at the code below:

def censor(text, word):
  parts = text.split(word)
  stars = '*' * len(word)
  ntext = stars.join(parts)
  return ntext

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

this will give:

th** hack ** a wack hack

i don’t think this should be partial censored, but the lesson doesn’t test this. So its a corner case you could discuss.


#5

Hmm, true. I guess it depends on what you’d expect a censor to do.
I feel like on most of the chat/message board censors I’ve seen, they actually do filter out strings even if embedded in longer words. (Get those mother****in snakes off this mother****in plane, or somesuch)

Still could probably do it without looping, though it’d be a lot easier with RegEx


#6

then you get two layers, because you don’t want to partial censor ■■■■ (h e l l) of hello.


#7

Thanks a lot for the reply! I understand the join is the programmer why to resolve it, but i also wanted to get it work with my logic. Thanks


#8

that is possible, do you now understand why your code is rejected? If you do, you can see if you can figure out a way to solve the problem, or was your reply a question to me for a hint?


#9

I understand now the issue why it was rejected. Thanks! Don ‘t need for a hint


#10

so then you solved it? Nicely done :slight_smile:


#11

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