Home [Python] 백준 1157번 : 단어 공부
Post
Cancel

[Python] 백준 1157번 : 단어 공부

Problem

https://www.acmicpc.net/problem/1157

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
word_list = list(input().upper())

# list에서의 요소를 key로, value는 0으로 하는 dictionary 정의
word_count = {string : 0 for string in word_list}

for i in word_list: # 빈도 카운팅
    word_count[i]+=1

# value가 최대인 key를 리스트에 저장
mode = [k for k,v in word_count.items() if max(word_count.values()) == v]

if len(mode) == 1:
    print(mode[0])
else: # 최빈값이 여러개인 경우 
    print('?')

Memo

중복을 제거한 후에 카운트하기 위해 딕셔너리 사용함. 최대값이 여러개인 경우에 max(word_count,key=word_count.get)의 경우는 하나밖에 인식하지 못해서 위와 같이 구함.

Ref.

dictionary 최대값 구하는 방법

This post is licensed under CC BY 4.0 by the author.

[Python] 백준 1065번 : 한수

TIL 220225