Sofia has had a very stressful month and decided to take a vacation for a week.
To avoid any stress during her vacation she wants to forward emails with a stressful subject line to Stephen.
The function should recognise if a subject line is stressful.
A stressful subject line means that all letters are in uppercase,
and/or ends by at least 3 exclamation marks,
and/or contains at least one of the following “red” words: "help", "asap", "urgent".
Any of those "red" words can be spelled in different ways - "HELP", "help", "HeLp", "H!E!L!P!", "H-E-L-P",
even in a very loooong way "HHHEEEEEEEEELLP"
Input: Subject line as a string.
Output: Boolean.
Example:
is_stressful("Hi") == False
is_stressful("I neeed HELP") == True
Precondition: Subject can be up to 100 letters
索菲亚这一个月压力很大,决定去度假一周。为了避免在休假期间出现压力,她想把邮件转发给斯蒂芬。
该功能应该能识别出主题是否有压力。一个有压力的主题行意味着所有的字母都是大写的,
并且/或以至少3个感叹号结尾,
并且/或包含以下至少一个“红色”字:“帮助”、“尽快”、“紧急”。
任何“红色”的单词都可以用不同的方式拼写——“HELP”、“HELP”、“HELP”、“H!E!L!P!”,"H-E-L-P",
即使是很随意的" HHHEEEEEEELLP"
import re
def is_stressful(subject):
if subject.isupper():
return True
subject = subject.lower()
if subject[-3:] == '!!!':
return True
red_words = ["help", "asap", "urgent"]
if re.search(r"h\S*e\S*l\S*p\S*", subject):
return True
if re.search(r"a\S*s\S*a\S*p\S*", subject):
return True
if re.search(r"u\S*r\S*g\S*e\S*n\S*t\S*", subject):
return True
return False
def is_stressful2(s):
return any((s.endswith('!!!'), s.isupper(), re.search(r'h+[^\s]*e+[^\s]*l+[^\s]*p', s.lower()),
re.search(r'a+[^\s]*s+[^\s]*a+[^\s]*p', s.lower()),
re.search(r'u+[^\s]*r+[^\s]*g+[^\s]*e+[^\s]*n+[^\s]*t', s.lower())
))
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert is_stressful("Hi") == False, "First"
assert is_stressful("I neeed HELP") == True, "Second"
print('Done! Go Check it!')