https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
freq = [0]*26 #개수
alpha = input()
#소문자 대문자로 변환
alpha = alpha.upper()
for i in alpha:
freq[ord(i)-65] += 1
result_index = freq.index(max(freq))
#중복 확인
check = True
for i in range(26):
if i != result_index and freq[i] == freq[result_index]:
check = False
break
if check:
print(chr(65+result_index))
else:
print("?")
ord(문자) : 아스키코드에 맞는 숫자 반환
chr(숫자) : 숫자에 맞는 아스키코드를 반환
그냥 막짜봤더니 이런식으로 나왔고 이를 파이썬 답게 짜면 다음과 같다.
words = input().upper()
unique_words = list(set(words)) # 입력받은 문자열에서 중복값을 제거
cnt_list = []
for x in unique_words :
cnt = words.count(x)
cnt_list.append(cnt) # count 숫자를 리스트에 append
if cnt_list.count(max(cnt_list)) > 1 : # count 숫자 최대값이 중복되면
print('?')
else :
max_index = cnt_list.index(max(cnt_list)) # count 숫자 최대값 인덱스(위치)
print(unique_words[max_index])
출처 : https://ooyoung.tistory.com/70
백준 1157번 [파이썬 알고리즘] 단어 공부
[Python] 백준 알고리즘 온라인 저지 1157번 : 단어 공부 Python3 코드 words = input().upper() unique_words = list(set(words)) # 입력받은 문자열에서 중복값을 제거 cnt_list = [] for x in unique_words..
ooyoung.tistory.com
count 함수에 대해서 잘 안 다뤄봐서 생각이 안났다.
반응형
'PS > 백준 문제' 카테고리의 다른 글
백준 2775번파이썬 문제풀이 (0) | 2021.10.27 |
---|---|
백준 1316번 파이썬 문제풀이 (0) | 2021.10.09 |
백준 2941번 파이썬 문제풀이 (0) | 2021.10.09 |
백준 1932번 파이썬 문제풀이 (0) | 2021.09.11 |
백준 4673번 파이썬 문제 풀이 (0) | 2021.08.10 |