Python:每日一题 335
今天的题目:给定一个英文段落 paragraph 和一组限定词 banned,返回最出现次数最多的非限定单词。
说明:
1. 至少有一个单词是非限定的。
2. 保证答案唯一。
3. 限定词都是以小写字母给出,paragraph 中的单词大小写不敏感。结果请返回小写字母。
4. 英文段落 paragraph 仅由字母、空格、标点 !?',;. 组成。
示例 1:
输入:paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." 和 banned = ["hit"]
输出:"ball"
解释:
"hit" 出现 3 次但是限定词。
"ball" 出现 2 次,是出现次数最多的非限定词。
注意段落中大小写不敏感,标点符号忽略。
示例 2:
输入:paragraph = "a a a b b c c d" 和 banned = ["a","b"]
输出:c"
解释:
"a" 和 "b" 都是限定词
"c" 出现了 2 次,而 "d" 只出现过一次
所以输出 "c"
{:10_298:}欢迎大家一起答题!{:10_298:} def func(paragraph, banned):
list1 =
list2 = list(set(list1))
list2.sort(key = lambda x: list1.count(x))
return list2[-1] def solve(s:str, ban):
d, e = {}, ''
for i in s:
if i not in [' ','!','?',',',';','.',"'"]:
e += i
else:
e = e.lower()
if d.get(e):
d += 1
elif not d.get(e) and e not in ban:
d = 1
e = ''
res = sorted(d.items(), key = lambda x : -x)
return res
print(solve( "a a a b b c c d",["a","b"]))
同时敲两种语言太累了,我老想末尾打分号 本帖最后由 fan1993423 于 2020-2-19 22:57 编辑
from collections import Counter
from re import split as sp
def fun335(paragraph,banned):
paragraph=paragraph.lower()
each_word=sp(" |!|\?|'|,|;|\.",paragraph)
for k,v in Counter(each_word).most_common(len(banned)+2):
if k=='':continue
elif k not in banned:return k def func335(paragraph, banned):
paragraph = paragraph.lower()
for each in banned + list("!?',;."):
paragraph = paragraph.replace(each.lower(), '')
result = paragraph.split()
return max(result, key=result.count)
def fic(para,each):
length = len(para)
for i in range(length):
if para == each:
return 1
return 0
def func(paragraph,banned):
paragraph1 = paragraph.lower()
para = paragraph1.split(' ')
for i in range(len(para)):
for e in list("!?',;."):
para = para.replace(e,'')
i = 0
for each in banned:
while True:
if fic(para,each):
para.remove(each)
else:
break
a = max(para, key=para.count)
return a from collections import Counter
def p335(p,b):
lt =
return Counter(lt).most_common(1)
import collections
def fc335(paragraph,banned):
return collections.Counter().most_common(1)
import string
def func(paragraph, banned):
L1 =
L2 = list(set(L1))
L3 =
return L2 def solution(pre_str , banned):
pre_str = str(pre_str)
pre_str = pre_str.lower()
char = ["!","?","'",",",";","."]
for each in char:
pre_str = pre_str.replace(each," ")
temp = pre_str.split(" ")
dict_r = {}
for each in temp:
dict_r.setdefault(each,0)
for each in temp:
dict_r += 1
for each in banned:
dict_r.pop(each)
i = 0
temp_list = []
for each in dict_r.keys():
if i<dict_r:
temp_list.append(each)
i = dict_r
result = temp_list.pop()
print(result) 字符串的函数除了split其他的用得不多,记不住,自己去编辑吧,锻炼一下自己的逻辑分析能力 {:5_96:}
def fun335(paragraph,banned):
def bigToSmall(string):#忘了大小写函数了,惭愧;此函数用的不多
score = ord(string)#string 这里限定为字母
if score > 90:
return string
else:
return chr(score+32)
dictionary = dict()
M = len(paragraph)
index = 0
preAlpha = ''
word = ''
while index < M:
temp = paragraph
if temp.isalpha():
temp = bigToSmall(temp)
word += temp
preAlpha = temp
else:
if preAlpha != '':
if word not in banned:
try:
dictionary += 1
except Exception:
dictionary = 1
preAlpha = ''
word = ''
else:
preAlpha = ''
word = ''
else:
pass
index += 1
else:
if preAlpha != '':
if word not in banned:
try:
dictionary += 1
except Exception:
dictionary = 1
else:
pass
sorted_items = sorted(dictionary.items(),key = lambda x:-x)
return sorted_items 本帖最后由 一个账号 于 2020-2-20 14:55 编辑
def fun(paragraph : str, banned : list) -> str:
paragraph = paragraph.lower()
paragraph = list(paragraph)
pun = "!?',;."
list1 = []
list2 = []
count = {}
for each in paragraph:
if each not in pun:
list1.append(each)
list1 = "".join(list1)
paragraph = list1.split(" ")
for word in banned:
for i in range(paragraph.count(word)):
paragraph.remove(word)
for each in paragraph:
if each not in list2:
list2.append(each)
for each in list2:
count = each
return count.get(max(count)) 一个账号 发表于 2020-2-20 13:14
考虑一下效率的问题,看你的答案十有八九会超时 zltzlt 发表于 2020-2-20 13:58
考虑一下效率的问题,看你的答案十有八九会超时
你没测试怎么知道 一个账号 发表于 2020-2-20 13:59
你没测试怎么知道
for each in paragraph:
if "." in each:
each
你这一句什么意思? 本帖最后由 zltzlt 于 2020-2-20 14:10 编辑
一个账号 发表于 2020-2-20 13:14
输入 paragraph = " ".join(['a', 'c', 'b'] * 100000), banned = ["a", "b"] 超时 冬雪雪冬 发表于 2020-2-19 21:40
67 ms wanting-for 发表于 2020-2-19 22:07
同时敲两种语言太累了,我老想末尾打分号
输入 paragraph = "Bob", banned = [] 出错 fan1993423 发表于 2020-2-19 22:40
60 ms wcshds 发表于 2020-2-19 22:49
解答错误
输入:paragraph = "abc abc? abcd the jeff!", banned = ["abc","abcd","jeff"]
输出:"d"
预期结果:"the"