Censor Challenge (3) - help?

Hello, I am stuck with the Censor Challenge (3),

My main problem: I cannot find a way to make “her” not overwrite “researchers” or “herself”…I saw this was a common problem and couldn’t find a way to make it work in my code…
Any idea would help:

``````email_two = "Good Morning, Board of Investors,Lots of updates this week. The learning algorithms have been working better than we could have ever expected. Our initial internal data dumps have been completed and we have proceeded with the plan to connect the system to the internet and wow! The results are mind blowing. She is learning faster than ever. Her learning rate now that she has access to the world wide web has increased exponentially, far faster than we had though the learning algorithms were capable of. Not only that, but we have configured her personality matrix to allow for communication between the system and our team of researchers. That's how we know she considers herself to be a she! We asked! How cool is that? We didn't expect a personality to develop this early on in the process but it seems like a rudimentary sense of self is starting to form. This is a major step in the process, as having a sense of self and self-preservation will allow her to see the problems the world is facing and make hard but necessary decisions for the betterment of the planet. We are a-buzz down in the lab with excitement over these developments and we hope that the investors share our enthusiasm. Till next month, Francine, Head Scientist"

proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]

def censorAll(ls, text):
for m in ls:

mUp = m[0].upper()+m[1:]
mS = m+"s"
if mS in text:
text = text.replace(mS, len(mS)*"*")
if mUp in text:
text = text.replace(mUp, len(mUp)*"*")
if m in text:
text = text.replace(m, len(m)*"*")

return text

print(censorAll(proprietary_terms, email_two))
``````

Ah, nice one. It’s always good to catch little bugs like this in your code. I’m not sure if there’s an easy solution. I recall doing this myself by putting words that shouldn’t have been censored back into the text after their removal. I think I found that easier than changing the original function but that’s perhaps a bit of quick and dirty patch though it did seem to work.

I think perhaps focusing on how these particular words are different when compared to the ones you actually want to remove would be a good place to start. Work that out in your head or on paper and then transfer that thought process into code. It might be a bit rough the first time you write it but it’s probably easier to refactor it once you’ve worked out at least one solution so aim to solve it first and then worry about cleaning it up (though it should still be fairly general, i.e. a list of different words should still function).

1 Like