whosyourdaddy 发表于 2020-2-20 00:45
def fic(para,each):
length = len(para)
for i in range(length):
输入 paragraph = " ".join(['a', 'c', 'b'] * 100000), banned = ["a", "b"] 超时
kinkon 发表于 2020-2-20 01:16
解答错误
输入:paragraph = "Bob. hIt, baLl", banned = ["bob", "hit"]
输出:"bob"
预期结果:"ball"
拉了盏灯 发表于 2020-2-20 01:55
厉害{:10_275:}
55 ms
ouyunfu 发表于 2020-2-20 02:49
解答错误
输入:paragraph = "Bob. hIt, baLl", banned = ["bob", "hit"]
输出:"hit"
预期结果:"ball"
牧木学编程 发表于 2020-2-20 09:53
输入 paragraph = "Bob!", banned = ["hit"] 出错
本帖最后由 zltzlt 于 2020-2-20 21:14 编辑
TJBEST 发表于 2020-2-20 11:19
字符串的函数除了split其他的用得不多,记不住,自己去编辑吧,锻炼一下自己的逻辑分析能力
代码真够长的{:10_277:}
不过效率还好{:10_256:}159 ms
def nums(par, ban):
dic = {}
count = 0
ban = ban.split(' ')
par = par.split(' ')
for word in par:
for sym in '!?\',;.':
if sym in word:
word = word.replace(sym,'')
if word in ban:
continue
else:
word = word.lower()
if word not in dic:
dic = dic.get(word,count+1)
else:
dic += 1
return max(dic,key=dic.get)
ouyunfu 发表于 2020-2-20 02:49
表示没看懂sort括号里的东西。。
冬雪雪冬 发表于 2020-2-19 21:40
这个sort括号里的是个啥。
qq614704680 发表于 2020-2-20 14:33
这个sort括号里的是个啥。
是匿名函数,告诉程序把list2以list1的元素数量counts排序
zltzlt 发表于 2020-2-20 14:14
解答错误
输入:paragraph = "Bob. hIt, baLl", banned = ["bob", "hit"]
缩成一行时犯了个小错误,改正了
from collections import Counter
def p335(p,b):
lt =
return Counter(lt).most_common(1)
kinkon 发表于 2020-2-20 14:44
缩成一行时犯了个小错误,改正了
可以了,56 ms
zltzlt 发表于 2020-2-20 14:05
你这一句什么意思?
搞错了,那一句是没有的{:10_245:}
一个账号 发表于 2020-2-20 14:55
搞错了,那一句是没有的
嗯,解决一下超时的问题吧
zltzlt 发表于 2020-2-20 14:08
输入 paragraph = " ".join(['a', 'c', 'b'] * 100000), banned = ["a", "b"] 超时
搞不懂为什么会超时{:10_266:}
zltzlt 发表于 2020-2-20 14:57
嗯,解决一下超时的问题吧
原来 remove() 效率这么低,改了一下代码:
def fun(paragraph : str, banned : list) -> str:
paragraph = paragraph.lower()
pun = "!?',;."
list1 = []
count = {}
paragraph = paragraph.split(" ")
for each in paragraph:
if each not in list1 and each not in banned:
list1.append(each)
for each in list1:
count = each
return count.get(max(count))
zltzlt 发表于 2020-2-20 14:12
解答错误
输入:paragraph = "abc abc? abcd the jeff!", banned = ["abc","abcd","jeff"]
越写越纠结{:10_266:}
import re
def func335(paragraph, banned):
paragraph = re.sub(r"[!|?|'|,|;|.]", '', paragraph.lower())
for each in banned:
p = re.compile(r'\b%s\b' % each)
paragraph = p.sub('', paragraph)
result = paragraph.split()
return max(result, key=result.count)
一个账号 发表于 2020-2-20 15:30
原来 remove() 效率这么低,改了一下代码:
解答错误
输入:paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." 和 banned = ["hit"]
输出:"hit."
预期结果:"ball"
wcshds 发表于 2020-2-20 15:30
越写越纠结
输入 paragraph = " ".join(['a', 'c', 'b'] * 100000), banned = ["a", "b"] 超时
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) and e:
d += 1
elif not d.get(e) and e not in ban and e:
d = 1
e = ''
if e and e not in ban:
d = 1
res = sorted(d.items(), key = lambda x : -x)
return res if res else ''
print(solve("Bob. hIt, baLl", ["bob", "hit"]))
paragraph = " ".join(['a', 'c', 'b'] * 100000), banned = ["a", "b"]
这个测试用例不正确吧,paragaph应该是字符串啊,
感觉我这个效率应该不算高