[파이썬 알고리즘 인터뷰] 가장 흔한 단어

2020. 12. 24. 17:48노트/Algorithm : 알고리즘

 

# 4. 가장 흔한 단어 

금지된 단어를 제외한 가장 흔하게 등장하는 단어를 출력하라. 대소문자 구분을 하지 않으며, 구두점(마침표, 수미표 등) 또한 무시한다 

* 입력: 
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]

* 출력
"ball"

 

paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]

import re
import collections 

def common_word(paragraph):
    words=[word for word in re.sub(r'[^\w]'," ",paragraph).lower().split(" ") if word not in banned]
    counts = collections.Counter(words)
    # 가장 흔하게 등장하는 단어의 첫번째 인덱스 리턴 
    return counts.most_common(1)[0][0]
    
    
common_word(paragraph)
>>> "hit"