Skip to content

Instantly share code, notes, and snippets.

@thejoshualouis
Last active August 24, 2020 11:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thejoshualouis/0352fdcb6d3f5983101ff1928109ff63 to your computer and use it in GitHub Desktop.
Save thejoshualouis/0352fdcb6d3f5983101ff1928109ff63 to your computer and use it in GitHub Desktop.
My answer to Code Academy's Censor Dispenser challenge.
email_one = open("email_one.txt", "r").read()
email_two = open("email_two.txt", "r").read()
email_three = open("email_three.txt", "r").read()
email_four = open("email_four.txt", "r").read()
proprietary_terms = ["she", "personality matrix", "sense of self", "self-preservation", "learning algorithm", "her", "herself"]
negative_words = ["concerned", "behind", "danger", "dangerous", "alarming", "alarmed", "out of control", "help", "unhappy", "bad", "upset", "awful", "broken", "damage", "damaging", "dismal", "distressed", "distressed", "concerning", "horrible", "horribly", "questionable"]
def censor(email, words):
e = email.lower()
w = words.lower()
ind = e.find(w)
while ind > 0:
if (e[ind-1:ind].isspace() == False and e[ind-1:ind] not in ('(',',',')','"')):
ind = e.find(w,ind+1)
elif (e[ind+len(w)].isspace()==False and e[ind+len(w)] not in (')',',','"','.')):
ind = e.find(w,ind+1)
else:
email = email[:ind] + "*"*len(words) + email[ind+len(words):]
ind = e.find(w,ind+1)
return(email)
def censor2(email,words):
word_list = sorted(words,key=len,reverse=True)
for word in word_list:
email = censor(email,word)
return(email)
def word3(phrase,word):
p = phrase.lower()
w = word.lower()
loc1 = p.find(w)
loc2 = p.find(w,loc1+1)
ind = loc2
while ind > 0:
if p[ind-1:ind].isspace() == False:
ind = p.find(w,ind+1)
else:
phrase = phrase[:ind] + "*"*len(word) + phrase[ind+len(word):]
ind = p.find(w,ind+1)
return(phrase)
def censor3(email,proprietary_terms,negative_words):
for word in negative_words:
email = word3(email,word)
email = censor2(email,proprietary_terms)
return(email)
def censor4(email,proprietary_terms,negative_words):
email = censor3(email,proprietary_terms,negative_words)
e_l = email.split()
ind_list = []
r = range(len(e_l))
for i in r:
if '*' in e_l[i]:
ind_list.append(i)
for num in ind_list:
e_l[num-1]='*'*len(e_l[num-1])
e_l[num+1]='*'*len(e_l[num+1])
email = ' '.join(e_l)
return(email)
@Dyrits
Copy link

Dyrits commented Apr 9, 2020

The naming makes it very confusing and complicated to understand. You should add some comments to explain a bit.

@radek321123
Copy link

It doesn't censor first word from emails, because of while loop in censor function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment