2023年9月3日 星期日

9/2 每日一提(返回除了被BAND的單字中重複最多的單字)

 Given a string paragraph and a string array of the banned words banned, return the most frequent word that is not banned. It is guaranteed there is at least one word that is not banned, and that the answer is unique.

The words in paragraph are case-insensitive and the answer should be returned in lowercase.

 

Example 1:

Input: paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.", banned = ["hit"]
Output: "ball"
Explanation: 
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"), 
and that "hit" isn't the answer even though it occurs more because it is banned.

Example 2:

Input: paragraph = "a.", banned = []
Output: "a"

 

Constraints:

  • 1 <= paragraph.length <= 1000
  • paragraph consists of English letters, space ' ', or one of the symbols: "!?',;.".
  • 0 <= banned.length <= 100
  • 1 <= banned[i].length <= 10
  • banned[i] consists of only lowercase English letters.
-----------------

import re
class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        paragraph=paragraph.lower()
        for i in banned :
            paragraph=paragraph.replace(i,'')
        paragraph_list=re.findall(r'[a-z]+',paragraph)
        p_set=set(paragraph_list)
        my_dict={}
        for i in p_set:
            my_dict[i]=paragraph_list.count(i)
        max_v=max(my_dict.values())
        for k,v in my_dict.items():
            if v == max_v:
                return k
---------------------------------------

class Solution:
    def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
        banset = set(banned)
        for c in "!?',;.":
            paragraph = paragraph.replace(c, " ")
        count = collections.Counter(word for word in paragraph.lower().split() if word not in banset)
        return count.most_common(1)[0][0]
        

#在(word for word in paragraph.lower().split() if word not in banset)
會產生一個a=<generator object>
用List代出來為長類似a=['bob', 'a', 'ball', 'the', 'ball', 'flew', 'far', 'after', 'it', 'was']

count.most_common(1)[0][0]
#a_count.most_commo[('ball', 2)]
意思是返回重複次數最多的

   
       

標籤:

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁