Algorithm/Implement
20291λ²/ νμΌμ 리/ λμ λ리
μ νΈλ
2025. 1. 5. 23:11
π λ¬Έμ
https://www.acmicpc.net/problem/20291
π λ΄κ° μκ°ν νμ΄
λμ λ리 μ¬μ© μκ° λͺ»ν¨
π» λ΄ μ½λ
# νμ₯μ λ³λ‘ νμ₯μ μ΄λ¦κ³Ό κ°―μ μΆλ ₯
# νμ₯μ μ΄λ¦ μ¬μ μμΌλ‘ μΆλ ₯
# λμ
λ리 μ΄μ©νκΈ°
import sys
n = int(input())
# λμ
λ리 μμ±, key: νμ₯μ, value : νμ₯μ λμ¨ νμ
cnt = {}
for _ in range(n):
file = sys.stdin.readline().rstrip()
# μ€λ₯Έμͺ½λΆν° .μ κΈ°μ€μΌλ‘ μ΅λ 1κ° λΆλ¦¬ν΄μ νμ₯μ μΆμΆ
_,extension = file.rsplit('.', 1)
if extension in cnt:
cnt[extension] += 1
else:
cnt[extension] = 1
for extension in sorted(cnt.keys()):
print(extension, cnt[extension])
π μ°Έκ³ μλ£
βΊοΈ μλ‘ μκ² λ μ§μ
1. λμ λ리μ κ° μΆκ°
my_dict = {} # λΉ λμ
λ리 μμ±
# κ° μΆκ°
my_dict['key1'] = 10
my_dict['key2'] = 20
# κ° μ
λ°μ΄νΈ
my_dict['key1'] = 15
print(my_dict)
{'key1': 15, 'key2': 20}
2. keys()ν¨μ : λμ λ리μ λͺ¨λ ν€ λ°ν
리μ€νΈμ²λΌ μ¬μ© κ°λ₯
my_dict = {'a': 1, 'b': 2, 'c': 3}
# keys() μ¬μ©
keys = my_dict.keys()
print(keys)
dict_keys(['a', 'b', 'c'])
3. rsplit() ν¨μ
rsplit('.' , 1 )
μ€λ₯Έμͺ½μμλΆν° νμΈν΄μ . κΈ°μ€μΌλ‘ λ¬Έμμ΄ λΆλ¦¬νκΈ°, μ΅λ 1λ²κΉμ§
file.rsplit('.', 1)
#μμ)'file.txt' → ['file', 'txt'].
file = "example.txt"
_, extension = file.rsplit('.', 1)
print(extension) # 'txt'
- _λ 'example' κ°μ λ°μμ€μ§λ§ μ¬μ©νμ§ μμ΅λλ€. μ¬μ©νμ§ μμ κ°μ΄λ€
-> νμΌλͺ μμ νμ₯μλ₯Ό μΆμΆνλ©΄μ νμΌλͺ μλΆλΆμ 무μνκΈ° μν μ½λμ λλ€
- extensionμ 'txt' κ°μ λ°μμ νμ₯μλ‘ μ¬μ©ν©λλ€.
π 리뷰