[Day_28] 백준 1157 단어 공부_파이썬

LUNA·2023년 3월 7일
0

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

코드

import sys
from collections import Counter
word=input()
word=word.upper() 
count=Counter(word)

most=count.most_common() #most.common() 인자 없는건 내림차순 정렬
maximum=most[0][1]
high=count.most_common(1) #최빈값



modes=[]
for num in most:
  if num[1]==maximum:
    modes.append(num[0])

if len(modes)>1:
  print("?")
else:
  print(high[0][0])코드를 입력하세요

대소문자로 바꾸기

word.upper()
word. lower()

 

counter함수

입력값이 Aaabbks 일때
count=Counter(word)
print(count)

->Counter({'A': 3, 'B': 2, 'K': 1, 'S': 1})

 

내림차순 정렬

most=count.most_common()

maximum=most[0][1]
print(maximum)

-> 3
maximum=most[0][1]
0번째 덩어리(?)의 value값
만약 most[0][0]이라면 -> 'A'
만약 most[2][1]이라면 -> 1

 

for문 분석

for num in most:
if num[1]==maximum:
modes.append(num[0])

num[1] : 3,2,1,1 즉 value값을 모두 도는것
num[0] : A,B,K,S 즉 Key 값을 모두 도는것
(for num in most 이기 때문에)

profile
Happiness

0개의 댓글