-
[백준] 1157 - 단어 공부 (python)Problem Solving/Python 2023. 7. 12. 10:57반응형
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
포인트
문제를 읽자 계수 정렬이 떠올라서 dictionary 를 활용했습니다. 대소문자를 구분하지 않는다고 하였으니 upper 를 활용하여 모두 대문자로 바꾼 다음 카운트했고 가장 많이 등장한 글자가 하나면 그 문자를, 여러개면 ? 를 출력하게 구현했습니다.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersfrom collections import defaultdict n = input() word_dict = defaultdict(int) for c in n: word_dict[c.upper()] += 1 max_value_key = '' for key, value in word_dict.items(): if value == max(word_dict.values()): if max_value_key == '': max_value_key = key else: max_value_key = '?' break print(max_value_key) 반응형'Problem Solving > Python' 카테고리의 다른 글
[백준] 1158 - 요세푸스 문제 (python) (0) 2023.07.17 [백준] 1110 - 더하기 사이클 (python) (0) 2023.07.12 [백준] 1094 - 막대기 (재귀 풀이) (python) (0) 2023.07.11 [백준] 1085 - 직사각형에서 탈출 (python) (0) 2023.07.11 [백준] 1076 - 저항 (python) (0) 2023.07.10