프로그래머스. 실패율

minan·2021년 6월 19일
0

프로그래머스

목록 보기
1/92

문제 링크 https://programmers.co.kr/learn/courses/30/lessons/42889

Level 1. 2019 KAKAO BLIND RECRUITMENT 실패율

파이썬 풀이

개선전 코드

def solution(N, stages):
    answer = []

    result = []
    
    # 사람의 수
    k = len(stages)
    
    for i in range(1, N + 1):
        count = stages.count(i)
        if k == 0:
            result.append((i, 0))
            continue
        if count == 0:
            result.append((i, 0))
            continue
        result.append((i, count / k))
        k -= count

    result = sorted(result, key=lambda x: (-x[1], x[0]))

    for i in result:
        answer.append(i[0])

    return answer

result를 사용하지 않고 정렬하는 더 간단한 코드

def solution(N, stages):
    answer = []
    # 사람의 수
    k = len(stages)

    for i in range(1, N + 1):
        count = stages.count(i)
        if k == 0:
            answer.append((i, 0))
            continue
        if count == 0:
            answer.append((i, 0))
            continue
        answer.append((i, count / k))
        k -= count

    answer = sorted(answer, key=lambda x: (-x[1], x[0]))

    answer = [i[0] for i in answer]

    return answer
profile
https://github.com/minhaaan

0개의 댓글