티스토리 뷰

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

 

요기 두군데를 참고했다.

댓글
최근에 올라온 글
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Total
Today
Yesterday