코드숨 알고리즘 day03 딕셔너리

코드숨 알고리즘 for 코딩 테스트 - DAY3 : 딕셔너리

파이썬에서 딕셔너리는 해쉬맵이라고 생각하면된다. key-value로 되어있는 자료구조로 검색때 유용하다.

숫자 카드2

N = input()
cards = list(map(int, input().split()))

M = input()
numbers = list(map(int, input().split()))

# 딕셔너리 만들기
# 카드를 순회하면서 딕셔너리에 해당 키가 있으면 value를 올려줌
dictionary = dict()
for card in cards:
    if card in dictionary:
        dictionary[card] += 1
    else:
        dictionary[card] = 1

for i in numbers:
    if i in dictionary:
        print(dictionary[i], end=' ')
    else:
        print(0, end=' ')

듣보잡

N, M = map(int, input().split())
dictionary = dict()
result = []

for i in range(N):
    dictionary[input()] = 0

for i in range(M):
    name = input()
    if name in dictionary:
        result.append(name)

sorted_result = sorted(result)
print(len(sorted_result))

for s in sorted_result:
    print(s)

패션왕 신해빈

k = int(input())

for _ in range(k):
    n = int(input())
    clothes = {}

    for _ in range(n):
        name, cloth = input().split()
        if cloth in clothes:
            clothes[cloth] += 1
        else:
            clothes[cloth] = 1

    count = 1

    for value in clothes.values():
        count *= value + 1

    print(count - 1)

후기

딕셔너리는 비교적 다른 문제들보다 쉬웠다.

results matching ""

    No results matching ""