Hello everyone! I've been working through this section's assignments, and found this one to be a little more difficult than those that came before. This was the first time I needed to grab a pen and paper to work out exactly how the logic needed to work before I could really make any progress.
Below is my (functioning) code. The exercise accepted a simpler version, but I wasn't happy with the way it handled near-matches for the censor word. This code doesn't use any special functions, really, only basic logical structures. I approached and tackled this problem all on my own, but I came here to see how I could improve my solution and perhaps even problem-solving abilities for the future.
I've seen different function solutions on this board that were a bit shorter than mine, which clocks in at 20 SLOC. I'm looking for tips to improve my efficiency and help me tackle problems like this more easily in the future.
And as an exercise, perhaps we can search for the shortest possible function that accomplishes the same task!
def censor(text, word): stop = len(word) count = 0 result = "" holder = "" for letter in text: if (letter == word[count]) & (count < stop): count += 1 holder += letter if count == stop: result += '*' * stop count = 0 holder = "" else: if count != 0: result += holder holder = "" result += letter count = 0 return result print censor("A man who does another mans dirty work is no man at all", "man")