Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
regexp for repeating characters solution in Clear category for Stressful Subject by igor.vaiman
import re
def is_stressful(subj):
"""
recognize stressful subject
"""
if subj.isupper() :
# check if subject is in uppercase
return True
elif len(subj)>3 and subj[-3:]=='!!!' :
# check if subject ends with '!!!'
return True
else :
# check if there are some redword variations in string
redwords = ["help", "asap", "urgent"]
# prepare for re processing
subj = subj.lower().replace(' ', '_')
# remove characters that are not letters or '_'
subj = re.compile(r'[^\w]').sub('', subj)
# create re that detects repeating characters and replace it with first
# character of the group
repeat_pattern = re.compile(r'(\w)\1*')
subj = repeat_pattern.sub(r'\1', subj)
subj = subj.split('_')
for w in redwords :
if w in subj :
return True
return False
if __name__ == '__main__':
#These "asserts" are only for self-checking and not necessarily for auto-testing
assert is_stressful("Hi") == False, "First"
assert is_stressful("I neeed HELP") == True, "Second"
print('Done! Go Check it!')
May 22, 2020