My brain is so fried from this one I can’t tell if I did this correctly, or completely overthought it. I think this returns the correct string, but if someone can point me in the right direction that would be great. For some reason I can’t find the solution online to compare myself to. Thanks!
def censor_email_three(email, terms):
email_split = email.split(" ")
censored_terms = []
count = 0
for term in terms:
censored_term = ""
for i in range(len(term)):
if term[i] == " ":
censored_term = censored_term + " "
else:
censored_term = censored_term + "*"
censored_terms.append(censored_term)
for i in range(len(email_split)):
for x in range(len(terms)):
if email_split[i] == terms[x]:
count += 1
if count == 3:
for y in range(len(terms)):
for b in range(i, len(email_split)):
if email_split[b] == terms[y]:
email_split[b] = censored_terms[y]
email_final = " ".join(email_split)
censored_email_final = censor_email_two(email_final, proprietary_terms)
return censored_email_final
I’ve already seen some issues, forgot to include a .lower() check for uppercase matching words…and I removed the billions of nested for loops and changed it to:
for i in range(len(email_split)):
for x in range(len(terms)):
if email_split[i] == terms[x]:
count += 1
if count == 3:
index = i
for y in range(len(terms)):
for b in range(index, len(email_split)):
if email_split[b] == terms[y]:
email_split[b] = censored_terms[y]
I’ll go figure out the uppercase issues now.
Edit: Also realized in the previous setup should have check if count was >= 3
I figured it all out…I totally overthought everything lol…
def censor_email_three(email, terms):
email_split = email.split(" ")
email_split_lower = []
for word in email_split:
email_split_lower.append(word.lower())
count = 0
# sets the first index to start replacing terms after seeing any one of the terms twice
for i in range(len(email_split)):
for x in range(len(terms)):
if email_split_lower[i] == terms[x]:
count += 1
if count == 3:
word = email_split_lower[I]
email_final = " ".join(email_split)
word_index = email_final.find(word)
email_first_half = email_final[:word_index]
email_second_half = email_final[word_index:]
for term in terms:
censor_word = ""
for i in range(len(term)):
if term[i] == " ":
censor_word = censor_word + " "
else:
censor_word = censor_word + "*"
email_second_half = email_second_half.replace(term, censor_word).replace(term.title(), censor_word)
censored_email_final = email_first_half + email_second_half
censored_email_final = censor_email_two(censored_email_final, proprietary_terms)
return censored_email_final
This turned out to be a simpler way to work through this rather than the way I was trying to. I could probably clean up my variable names a little but at this point I’m just glad I worked it out.