[프로그래머스] 신고 결과 받기(Python)

vvo_ter·2022년 9월 20일
0

프로그래머스

목록 보기
3/28
post-thumbnail

💻 문제 - Lv.1

2022 KAKAO BLIND RECRUITMENT


👉 제출 코드

해시(dictionary)를 사용하여 각 user가 누구에게 신고 당했는지를 저장한다.

def solution(id_list, report, k):
	# (1)
    report_list = {}
    for i in id_list:
        report_list[i] = []
    
    for i in range(len(report)):
        user_id = report[i].split(" ")[0]
        report_id = report[i].split(" ")[1]
        if user_id not in report_list[report_id]: # => 중복 제거
            report_list[report_id].append(user_id)
            
    # (2)
    check = []
    for i in id_list:
        if len(report_list[i]) >= k: # 신고 당한 횟수 확인
            check += report_list[i]
    
    answer = []
    for i in id_list:
        answer.append(check.count(i))
            
    return answer

(1) 각 report마다 중복을 제거하면서 각 user 별로 신고 당한 user를 추가한다.

report_list는 다음과 같다.

유저 ID신고한 유저 ID
"muzi""apeach"
"frodo""muzi", "apeach"
"apeach"없음
"neo""frodo", "muzi"

(2) 이용 정지인 user의 경우 해당 user를 신고한 user를 check에 추가하고,check의 개수를 세서 answer를 리턴한다.


🙏 다른 사람의 풀이 보기

def solution(id_list, report, k):
    answer = [0] * len(id_list)    
    reports = {x : 0 for x in id_list}

    for r in set(report):
        reports[r.split()[1]] += 1

    for r in set(report):
        if reports[r.split()[1]] >= k:
            answer[id_list.index(r.split()[0])] += 1

    return answer
  • 코드 길이 줄이기

    report_list = {}
    for i in id_list:
        report_list[i] = []
    			
              🔽 
    
    reports = {x : 0 for x in id_list}
  • set을 사용한 리스트 중복 제거

  • 인덱스 값이 일치하는 곳에 접근: index()

    answer[id_list.index(r.split()[0])] += 1
profile
's Coding Memory

0개의 댓글