20291번: 파일 정리
친구로부터 노트북을 중고로 산 스브러스는 노트북을 켜자마자 경악할 수밖에 없었다. 바탕화면에 온갖 파일들이 정리도 안 된 채 가득했기 때문이다. 그리고 화면의 구석에서 친구의 메시지를
www.acmicpc.net
import sys
from collections import Counter
input = sys.stdin.readline
n = int(input().rstrip())
texts = [input().rstrip().split(".")[1] for _ in range(n)]
for name, count in sorted(list(dict(Counter(texts)).items()),key=lambda x: x[0]):
print(f"{name} {count}")
이번 문제는 확장자 별로 파일의 개수를 구하여 출력하는 간단한 문제이다.
[Python] collections 모듈의 Counter 사용법
1) Counter 기본 사용법 이번에는 데이터의 개수를 셀 때 매우 유용한 파이썬의 collections 모듈의 Counter 클래스에 대해서 알아보겠습니다. from collections import Counter collections 모듈의 Counter 클래스는 별
woohyun-king.tistory.com
먼저 입력된 파일들을 '.'를 기준으로 분리(split)하여 확장자 목록을 저장하고, Python에서 제공하는 라이브러리인 Counter를 사용하여 정답을 도출했다.
lambda 함수를 활용한 정렬, Counter의 사용법을 복습해볼 수 있었던 문제였다 :)
'Algorithm 💡 > Implementation' 카테고리의 다른 글
[백준 1388번] 바닥 장식 (0) | 2024.03.04 |
---|---|
[백준 14719번] 빗물 (0) | 2024.01.15 |
[백준 16918번] 봄버맨 (1) | 2024.01.04 |
[백준 15721번] 번데기 (1) | 2023.12.28 |
[프로그래머스 PCCP 모의고사 2-1번] 붕대 감기 (0) | 2023.11.18 |