Algorithm

[프로그래머스 / 파이썬] 신고 결과 받기

임쁘엔 2023. 7. 29. 19:58
 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

정답 코드

def solution(id_list, report, k):
    answer = []
    
    # id별 mail 받을 횟수
    id_dict = {}
    for i in id_list:
        id_dict.setdefault(i, 0)
            
    # 신고 받은 횟수
    report_count_dict = id_dict.copy()
    
    # 신고자, 신고받은 사람
    report_person_dict = {}
    
    # 중복 신고 삭제
    report = list(set(report))
    for i in report:
        reporter, reported = i.split()
        report_count_dict[f'{reported}'] += 1
        if report_person_dict.get(f'{reporter}'):
            report_person_dict[f'{reporter}'].append(f'{reported}')
        else:
            report_person_dict.setdefault(f'{reporter}', [f'{reported}'])
    
    # 정지
    ban_list = []
    
    for key, value in report_count_dict.items():
        if value >= k:
            ban_list.append(key)
            
    for key, value in report_person_dict.items():
        for i in ban_list:
            if i in value:
                id_dict[key] += 1
    
    for value in id_dict.values():
        answer.append(value)
                
    return answer

문제 풀이

  1. `id_dict` 생성: 빈도수를 기록할 딕셔너리를 생성하고, `id_list`의 각 아이디를 키로 넣어 초기값을 0으로 설정합니다.
  2. `report_count_dict` 생성: `id_dict`를 복사하여 사용자별로 받은 신고 횟수를 기록할 딕셔너리를 생성합니다.
  3. `report_person_dict` 생성: 신고자별로 신고받은 사용자들의 아이디를 기록할 딕셔너리를 생성합니다.
  4. 중복 신고 제거: `report` 리스트를 `set`으로 변환하여 중복된 신고를 제거합니다.
  5. 각 신고에 대한 처리: `report` 리스트를 반복하며, 각 신고 정보를 파싱하여 `report_count_dict`와 `report_person_dict`에 정보를 업데이트합니다. `report_count_dict`는 신고받은 횟수를 기록하고, `report_person_dict`는 신고자별로 신고받은 사용자를 기록합니다.
  6. 정지 대상 생성: `report_count_dict`를 반복하면서, 정지 기준인 `k`번 이상 신고받은 사용자를 `ban_list`에 기록합니다.
  7. 정지 처리: `report_person_dict`를 반복하면서, 각 신고자가 `ban_list`에 포함된 사용자를 신고했는지 확인하여 정지 처리합니다. 신고자가 정지된 사용자를 신고한 경우 해당 신고자의 정지 횟수를 증가시킵니다.
  8. 결과 생성: `id_dict`를 반복하면서, 각 사용자의 정지 횟수를 `answer` 리스트에 추가합니다.
  9. `answer` 반환: `answer` 리스트를 반환하여 정지된 사용자의 수를 담아줍니다.