819. Most Common Word

kukudas·2022년 2월 15일
0

Algorithm

목록 보기
2/46

내 정답:

class Solution:
    def mostCommonWord(self, paragraph, banned):

        # 일단 잡 문자들 다 공백으로 치환
        for char in paragraph:
            if char.isalpha() == False:
                paragraph = paragraph.replace(char, ' ') # replace() Return a copy of the string

        paragraph = paragraph.lower() # 소문자로 변경
        words = paragraph.split() # 단어들 포함한 리스트 생성

        removed = []
        for word in words:
            if word not in banned:
                removed.append(word)

        dict = collections.Counter(removed) # counter 객체는 아이템에 대한 개수를 계산해 딕셔너리로 리턴함
        pre_results = dict.most_common(1) # 가장 많이 있는거 1개
        return pre_results[0][0]

입력값

paragraph = "Bob. hIt, baLl"
banned = ["bob", "hit"]

처음에는 아래처럼 했는데 이렇게 하면 위 처럼 값이 들어올 때 처음에 bob이 words에서 지워지고 words에는 ['hit', 'ball']이 남게 되는데 for문을 타면서 words의 맨 처음을 방문했으니 다음 word는 ball이 되면서 hit을 지우지 못하게 돼서 틀렸음.

for word in words:
	if word in banned:
		words.remove(word)

따라서 리스트를 하나 더 만들어서 wordbanned에 들어가 있지 않으면 그 리스트에 집어 넣은 후에 해당 리스트를 기준으로 많이 나온 단어를 구해서 해결함.

 removed = []
 	for word in words:
		if word not in banned:
			removed.append(word)

책 정답:

class Solution:
    def mostCommonWord(self, paragraph, banned):

        # \w - 단어 문자(word character)를 뜻하며 문자+숫자(alphanumeric)와 매치, [a-zA-Z0-9_]와 동일한 표현식임.
        # ^는 not을 의미함.
        # re.sub(pattern, repl, string, count=0, flags=0)
        # pattern은 정규식이고, repl은 바꿀것, pattern에 해당하는게 안보이면 string이 리턴됨.
        # 따라서 re.sub를 사용해서 정규식으로 단어가 아닌것을 공백으로 바꿔줌.
        # 이제 공백이 아닌 단어들이 나오는데 여기서 전부 소문자로 바꾸고 공백 단위로 단어들을 나눈후에 banned에 없는 단어들인 word로 리스트 생성함.
        # 리스트 컴프리헨션 https://wikidocs.net/22805
        words = [word for word in re.sub(r'[^\w]', ' ', paragraph)
                    .lower().split()
                        if word not in banned]

        # 가장 흔하게 등장하는 단어의 첫 번째 인덱스 리턴
        counts = collections.Counter(words)
        # most_common([n])     
        # Return a list of the n most common elements and their counts from the most common to the least. 
        # If n is omitted or None, most_common() returns all elements in the counter. 
        # Elements with equal counts are ordered in the order first encountered:
        return counts.most_common(1)[0][0]

most_common

0개의 댓글