리미로그

[programmers] 프로그래머스 신고 결과 받기 (lv1, python) 본문

algorithm/programmers

[programmers] 프로그래머스 신고 결과 받기 (lv1, python)

멍발자 2022. 7. 4. 18:33

https://programmers.co.kr/learn/courses/30/lessons/92334

 

코딩테스트 연습 - 신고 결과 받기

문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의

programmers.co.kr

 


파이썬의 dictionary를 이용하여 문제를 해결했다

다 풀고 나서 다른 사람들의 코드를 공부해봤는데 default dictionary 라는 좋은 방법을 알게 되었다

이것을 이용하면 나의 코드보다 훨씬 간단하게 작성할 수 있다

def find(id_list, dic, x, answer): # x를 신고한 아이디를 찾아 메일 발송 횟수 변경
    for i in dic: 
        if dic[i].count(x) > 0 : 
            answer[id_list.index(i)] += 1 
            

def solution(id_list, report, k):
    answer = [0] * len(id_list) # 각 아이디별 메일 받을 횟수
    
    dic = {} # 해당 아이디가 신고한 아이디 기록
    for i in id_list: 
        dic[i] = [0] # 해당 아이디가 신고 받은 횟수를 저장
    
    for rep in report:
        a,b = rep.split() 
        dic[a].append(b)
        dic[a] = list(set(dic[a])) # 중복 방지
    
    for x in dic:
        for name in dic[x]:
            if str(name).isdigit(): 
                continue
            dic[name][0] += 1
    
    for x in dic:
        if dic[x][0] >= k: # k번 신고 받은 경우 메일 보내기 위함
            find(id_list,dic,x,answer) 
    
    return answer
Comments