Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.6 kB
2
Indexable
Never
THIS ONE IS WITHOUT .REPLACE() BUT GETS 1.9/2 BECAUSE DOESN'T REPLACE THE SUBSTRINGS

#####################################################################################

#!/usr/bin/env python3

# The signatures of this class and its public methods are required for the automated grading to work. 
# You must not change the names or the list of parameters. 
# You may introduce private/protected utility methods though.
class ProfanityFilter:

    def __init__(self, keywords, template):
        self.keywords=keywords
        self.template=template

    def censor(self, word):
        self.word=word
        clean_msg=""
        for i in range(len(self.word)):
            clean_msg+=self.template[i%len(self.template)]
        return clean_msg

    def filter(self, msg):
        self.msg=msg
        self.msg=self.msg.split(" ")
        clean_msg=[]
        words_to_ban=[]
        words_to_ban = [ word.lower() for word in self.keywords]
        for word in self.msg:
            if word.lower() in words_to_ban:
                clean_msg.append(self.censor(word))
            else:
                clean_msg.append(word)
        return " ".join(clean_msg)


# You can play around with your implementation in the body of the following 'if'.
# The contained statements will be ignored while evaluating your solution.
if __name__ == '__main__':
    f = ProfanityFilter(["duck", "shot", "batch", "mastard"], "?#$")
    offensive_msg = "abc defghi mastard jklmno"
    clean_msg = f.filter(offensive_msg)
    print(clean_msg)  # abc defghi ?#$?#$? jklmno