티스토리 뷰
https://www.acmicpc.net/problem/1159
1159번: 농구 경기
상근이는 농구의 세계에서 점차 영향력을 넓혀가고 있다. 처음에 그는 농구 경기를 좋아하는 사람이었다. 농구에 대한 열정은 그를 막을 수 없었고, 결국 상근이는 농구장을 청소하는 일을 시작
www.acmicpc.net
👩💻문제 이해
1. 첫 번째 알파벳이 5번 이상 나오는 경우 모두 출력
2. 출력형태 -> 사전정렬 형태로 알파벳 첫글자만 공백없이 출력
👩💻collections모듈의 Counter클래스 사용 : 성공🌈
from collections import Counter
n = int(input())
string_list = []
for i in range(n):
string_list.append(input()[0])
cnt = Counter(string_list)
res = []
for i,j in cnt.items():
if j >= 5:
res.append(i)
if len(res) >= 1:
print(''.join(sorted(res)))
else :
print("PREDAJA")
from collections import Counter
n = int(input())
string_list = []
for i in range(n):
string_list.append(input()[0])
# 입력하는 문자열의 첫 번째 글자만 string_list에 추가
cnt = Counter(string_list)
# Counter를 사용해 각각 몇번 나오는지 출력
Counter() 출력형태 예시
res = []
for i,j in cnt.items():
if j >= 5:
res.append(i)
# 5번 이상 등장한 알파벳들을 res 리스트에 추가
# 이 때 i는 알파벳이 되고, j는 counter한 결과값이 된다.
.items() 출력형태 예시
if len(res) >= 1:
print(''.join(sorted(res)))
# 5번 이상 등장하는 알파벳이 존재한다면 사전식 정렬 형태로 공백없이 출력
else :
print("PREDAJA")
''.join() 출력형태 비교 예시
✨Point✨
1. 문자열에서 문자의 개수 세기
-> from collections import Counter
2. dict.items() 형태 활용
-> dict_items([(i,j,k, ...), (i,j,k, ...), ...]) 반복문을 통해 원하는 위치의 변수만 추출 가능!
3. 공백없이 출력하는 형태
-> print(''.join(argument 1개))
Counter클래스에 대한 정보는
https://www.daleseo.com/python-collections-counter/
https://docs.python.org/3/library/collections.html#collections.Counter
파이썬 collections 모듈의 Counter 사용법
Engineering Blog by Dale Seo
www.daleseo.com
collections — Container datatypes — Python 3.10.6 documentation
collections — Container datatypes Source code: Lib/collections/__init__.py This module implements specialized container datatypes providing alternatives to Python’s general purpose built-in containers, dict, list, set, and tuple. namedtuple() factory f
docs.python.org
요기 두군데를 참고했다.
'🦖 Programming > Python' 카테고리의 다른 글
[Python] 백준 알고리즘 9375번 : 패션왕 신해빈 (0) | 2022.09.04 |
---|---|
[Python] 백준 알고리즘 14425번 : 문자열 집합 (0) | 2022.09.03 |
[Python] 백준 알고리즘 17478번 : 재귀함수가 뭔가요? (0) | 2022.09.01 |
[Python] 백준 알고리즘 10815번 : 숫자 카드 (0) | 2022.09.01 |
[Python] 백준 알고리즘 1193번 : 분수 찾기 (0) | 2022.08.31 |