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.
내가 쓴 거
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
# make a banned word list
banned = set(word.lower() for word in banned)
# get rid of special characters while lowering cases and spliting
lowered_s = re.sub(r'[^A-za-z0-9]', ' ', paragraph).lower().split()
# get rid of banned words
preprocessed = []
for i in lowered_s:
if i not in banned:
preprocessed.append(i)
# count words
word_count = {}
for words in preprocessed:
if words not in word_count:
word_count[words] = 0
word_count[words] += 1
# pick the most frequent word and then return it
return max(word_count, key = word_count.get)
솔루션
import re
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
# convert to lower case and split string into words by spaces and punctuation
a = re.split(r'\W+', paragraph.lower())
# make new list consisitng of words not in banned list (remove banned words)
b = [w for w in a if w not in banned]
# return value that counted max times in the new list
return max(b, key = b.count)
처음 내가 푼 것의 경우 일단 banned를 하드코딩(내 경우 'hit'이라고 리스트 안에 따로 저장)했는데 오류가 나서 저렇게 했다. 어떻게 할지 몰라서 결국 챗지피티한테 물어보니 저렇게 함..list comprehension을 사용하고 그걸 lower() 시킨 다음 set()으로 씌워서 해당 word를 unique하게 하는 방식이었다. 왜냐하면 banned word 개개는 독립적인 거니깐.
복습 겸 알아둘 것들은 다음과 같다.
단어 세기
get()과 count()
이 함수는 딕셔너리에서 어떤 key의 value를 리턴한다. 안 넣으면 인자 없다고 오류남(None으로 처리한다는 뜻). count는 함수이름곧내. 마찬가지로 안 넣으면 인자 없다고 오류남.
max()
함수제목곧내
return이 뭘 하는지는 알겠는데 왜 max()에 get()과 count()에 괄호가 없나여?
왜냐하면 max()의 key로서 x.get 혹은 x.count로 통과시키면 메소드를 불러온 결과를 부르는 게 아니라, 메소드 그 자체를 불러오는 방식이기 때문이다. 즉, 어떠한 참조값을 해당 메소드에 통과시킴으로서 max()가 딕셔너리 (혹은 리스트)의 각각의 item들을 내부적으로 불러오는 거다. 따라서 실제 max()는 다음과 같은 방식으로 작동된다.
끝.