print censor("I had a cat, his name was Matt, and he was, as I said, a cat.", "cat") def censor(text, word): length = len(word) ch_locs = [ [] for _ in range(length)] i = 0 for ch_w in word: j = 0 for ch_t in text: if ch_w == ch_t: ch_locs[i].append(j) j += 1 i += 1 poss_word_locs = [ [] for _ in range(len(ch_locs[0]))] i = 0 for loc in ch_locs[0]: poss_word_locs[i] = range(loc, loc+length) i += 1 i = 0 for w_loc in poss_word_locs: j = 0 ok = 1 for ch_loc in w_loc: try: ch_locs[j].index(ch_loc) j += 1 except ValueError: poss_word_locs[i] = None break i += 1 word_locs = [] for word_loc in poss_word_locs: if word_loc != None: word_locs += word_loc censored = list(text) for loc in word_locs: censored[loc] = "*" return "".join(censored)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question