⚡️algorithm

[프로그래머스] dictionary 한줄로 구성하기

남남이루 2022. 6. 23. 18:01

내 코드

from collections import Counter

def solution(id_list, report, k):
    report = set(report)
    dic = {}
    for id in id_list:
        dic[id] = 0

    reported = [x for x,y in Counter([r.split()[1] for r in report]).items() if y>= k]
    print(reported)
    answer = [0] * len(id_list)
    for r in report:
        a, v = r.split()
        if v in reported:
            idx = id_list.index(a)
            answer[idx] += 1

    return answer

다른 사람 코드

def solution(id_list, report, k):
    answer = [0] * len(id_list)    
    reports = {x : 0 for x in id_list}   # dictionary 키에 이름 넣고, 횟수 0으로 세팅

    for r in set(report):
        reports[r.split()[1]] += 1       # 나올때마다 +1

    for r in set(report):
        if reports[r.split()[1]] >= k:   # 횟수 조건 추가 
            answer[id_list.index(r.split()[0])] += 1

    return answer

기억하기, 한줄 딕셔너리

reports = {x : 0 for x in id_list}