-
[백준 1157번] - 기본 / PythonData Engineering/알고리즘 - Python 2021. 6. 30. 21:29
[백준 1157번] - 기본
https://www.acmicpc.net/problem/1157
1157번: 단어 공부
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
www.acmicpc.net
(오답)
x=input() arr=[x[i].upper() for i in range(len(x))] mySet=list(set(arr)) dict={} for i in range(len(mySet)): a=arr.count(mySet[i]) dict[mySet[i]]=a key,value = dict.items() if len(max(dict.values()))==1: print(key) else: print('?')
(정답)
word=input().upper() alphabet=list(set(word)) cnt=[] for a in alphabet: c=word.count(a) cnt.append(c) m=max(cnt) #가장 많은 알파벳 개수 m_cnt=cnt.count(m) #알파벳 개수가 중복되는 지 확인 print(alphabet[cnt.index(m)] if m_cnt==1 else '?')
📍 딕셔너리 Key, Dict 접근 방법
#Key, Dict 사용 dict={} dict['A']=1 dict['Z']=2 print(dict.items()) #dict_items([('A', 1), ('Z', 2)]) print(dict.keys()) #dict_keys(['A', 'Z']) print(dict.values()) #dict_values([1, 2])