[프로그래머스] 1단계_신고 결과 받기 (python)

juyeon·2022년 6월 14일
0

코딩테스트(python)

목록 보기
1/22

프로그래머스

1단계_신고 결과 받기

(https://programmers.co.kr/learn/courses/30/lessons/92334?language=python3#)

내 풀이

: 프로그래머스 1단계 첫 문제라서.. 구글링으로 답을 찾아본 후, 이를 참고하여 풀어보았다. 결론적으로는 찾아본 답에 95% 유사하게 작성했지만, 그래도 계속..생각하면서..작성했다 ㅜㅜ

def solution(id_list, report, k):
    report = set(report) #set으로 중복 제거
    answer = [0] * len(id_list) 
    #신고한 사람. len만큼 초기화하여 list 생성
    report_b = [0] * len(id_list) #신고당한 사람
    
    for i in report:
        report_b[id_list.index(i.split()[1])] += 1
        #i를 split 한 후 [1]번째, 즉 신고당한 사람을 id_list에서 index로 순서 찾은 후에 이를 report_b의 순서에 맞게 신고당한 갯수 누적
        
    for i in report:
        if report_b[id_list.index(i.split()[1])] >= k: 
        #신고 당한 갯수가 k번 이상일 경우
            answer[id_list.index(i.split()[0])] += 1 
            #신고한 사람 list에서 index를 사용하여 맞는 순서를 찾은 후에 누적
            
    return answer

다른 사람 풀이(프로그래머스)

def solution(id_list, report, k):
    answer = [0] * len(id_list)
    #신고한 사람 list를 len만큼 초기화
    reports = {x : 0 for x in id_list}
    #이렇게 간단하게 value=0인 dictionary를 만들 수 있다니..! 감탄ㅜㅜ 

    for r in set(report):
        reports[r.split()[1]] += 1
        #report의 i를 split 한 후 -> [1]번째, 즉 신고당한 사람을 -> report에서 찾아서 -> +1 누적

    for r in set(report):
        if reports[r.split()[1]] >= k:
            answer[id_list.index(r.split()[0])] += 1
            #(신고한 사람을 index로 순서 찾은 후) -> answer에서 찾아서 -> +1 누적

    return answer

: 진짜.. 이렇게 심플하게 만들 수 있구나! 감탄이다ㅠㅠ 두번째 문제부터는 혼자 풀어보고 구글링 해야지!

profile
내 인생의 주연

0개의 댓글