需辨别“red”词语,删除特定标点,识别大小写,识别特定词组
我的写法:【find_repeat这个函数写的太繁琐了,set又报错。。。需要考虑有没有其它删除重复字符的方法】
def find_repeat(subj):
list2 = list(subj)
list1 = []
for u in range(0,len(subj)-1):
if list2[u] != list2[u+1]:
list1.append(list2[u])
return ''.join(list1)
def is_stressful(subj):
mark = False
red = ["help","asap","urgent"]
str = ""
if subj.isupper():
mark = True
elif subj[-3:] == "!!!":
mark = True
else:
subj = subj.lower()
subj = list(filter(lambda c: c.isalpha(), subj))
seq = str.join(subj)
print(seq)
for i in red:
if seq.find(i) != -1:
mark = True
else:
subj1 = find_repeat(subj)
for i in red:
if i in subj1:
mark = True
return mark
其它大神的Random Solution
(正则表达式)
def is_stressful(subj):
return subj.isupper() or \
bool(re.search('!{3}$', subj)) or \
bool(re.search('(h+)(e+)(l+)(p+)|(a+)(s+)(a+)(p+)|(u+)(r+)(g+)(e+)(n+)(t+)', \
re.sub('[^a-z\s]', '', subj.lower())))
Creative(使用交集):
for word in stresful:
kume= {"h","e","l","p"}
if set(word.lower()).intersection(kume)== kume:
check += 1
#return True
One-liner写法(正则表达式,用好了可以省很多fliter啊QAQ)
def is_stressful(subj):
pat = '|'.join('(' + '+[^A-Za-z]*'.join(p) + ')' for p in ('help', 'asap', 'urgent'))
return subj.isupper() or re.search(pat, subj.lower()) is not None or subj.endswith('!!!')