zltzlt 发表于 2020-2-20 14:13:56

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"] 超时

zltzlt 发表于 2020-2-20 14:14:59

kinkon 发表于 2020-2-20 01:16


解答错误

输入:paragraph = "Bob. hIt, baLl", banned = ["bob", "hit"]
输出:"bob"
预期结果:"ball"

zltzlt 发表于 2020-2-20 14:15:54

拉了盏灯 发表于 2020-2-20 01:55


厉害{:10_275:}

55 ms

zltzlt 发表于 2020-2-20 14:16:56

ouyunfu 发表于 2020-2-20 02:49


解答错误

输入:paragraph = "Bob. hIt, baLl", banned = ["bob", "hit"]
输出:"hit"
预期结果:"ball"

zltzlt 发表于 2020-2-20 14:18:07

牧木学编程 发表于 2020-2-20 09:53


输入 paragraph = "Bob!", banned = ["hit"] 出错

zltzlt 发表于 2020-2-20 14:19:13

本帖最后由 zltzlt 于 2020-2-20 21:14 编辑

TJBEST 发表于 2020-2-20 11:19
字符串的函数除了split其他的用得不多,记不住,自己去编辑吧,锻炼一下自己的逻辑分析能力

代码真够长的{:10_277:}

不过效率还好{:10_256:}159 ms

qq614704680 发表于 2020-2-20 14:29:51

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)

qq614704680 发表于 2020-2-20 14:32:32

ouyunfu 发表于 2020-2-20 02:49


表示没看懂sort括号里的东西。。

qq614704680 发表于 2020-2-20 14:33:30

冬雪雪冬 发表于 2020-2-19 21:40


这个sort括号里的是个啥。

冬雪雪冬 发表于 2020-2-20 14:36:39

qq614704680 发表于 2020-2-20 14:33
这个sort括号里的是个啥。

是匿名函数,告诉程序把list2以list1的元素数量counts排序

kinkon 发表于 2020-2-20 14:44:48

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)

zltzlt 发表于 2020-2-20 14:52:32

kinkon 发表于 2020-2-20 14:44
缩成一行时犯了个小错误,改正了

可以了,56 ms

一个账号 发表于 2020-2-20 14:55:29

zltzlt 发表于 2020-2-20 14:05
你这一句什么意思?

搞错了,那一句是没有的{:10_245:}

zltzlt 发表于 2020-2-20 14:57:18

一个账号 发表于 2020-2-20 14:55
搞错了,那一句是没有的

嗯,解决一下超时的问题吧

一个账号 发表于 2020-2-20 15:00:25

zltzlt 发表于 2020-2-20 14:08
输入 paragraph = " ".join(['a', 'c', 'b'] * 100000), banned = ["a", "b"] 超时

搞不懂为什么会超时{:10_266:}

一个账号 发表于 2020-2-20 15:30:30

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))

wcshds 发表于 2020-2-20 15:30:53

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)

zltzlt 发表于 2020-2-20 15:31:41

一个账号 发表于 2020-2-20 15:30
原来 remove() 效率这么低,改了一下代码:

解答错误

输入:paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." 和 banned = ["hit"]
输出:"hit."
预期结果:"ball"

zltzlt 发表于 2020-2-20 15:32:41

wcshds 发表于 2020-2-20 15:30
越写越纠结

输入 paragraph = " ".join(['a', 'c', 'b'] * 100000), banned = ["a", "b"] 超时

wanting-for 发表于 2020-2-20 15:34:29

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应该是字符串啊,
感觉我这个效率应该不算高
页: 1 [2] 3
查看完整版本: Python:每日一题 335