[Python] 프로그래머스 - Level1 - 로또의 최고 순위와 최저 순위

강주형·2022년 8월 7일
0

https://school.programmers.co.kr/learn/courses/30/lessons/77484

2021 Dev-Matching: 웹 백엔드 개발자(상반기)


from collections import Counter

def solution(lottos, win_nums):
    if 0 in lottos:
        zero_count = sorted(Counter(lottos).most_common())[0][1]
    else:
        zero_count = 0

    win_count = len(set(lottos).intersection(win_nums))
    answer = [7 - zero_count - win_count, 7-win_count]
    if answer[0] == 7:
        answer[0] = 6
    if answer[1] == 7:
        answer[1] = 6
    return answer

처음에는 0의 개수를 셀 때 most_common()을 이용함

근데 다른 사람들 코드 보니까 그냥 count() 써도 됐음
그리고 answer 부분도 굳이 저렇게 안 하고 max()를 사용하면 됐음

최종 코드

def solution(lottos, win_nums):
    zero_count = lottos.count(0)
    win_count = len(set(lottos).intersection(win_nums))
    answer = [7 - max(zero_count+win_count, 1), 7-max(win_count, 1)]
    return answer

프로그래머스 타인 코드를 보면 이렇게 풀기 보다는
rank라는 리스트를 만들고 푼 게 더 많은 것 같음

아래 타인 코드

def solution(lottos, win_nums):

    rank=[6,6,5,4,3,2,1]

    cnt_0 = lottos.count(0)
    ans = 0
    for x in win_nums:
        if x in lottos:
            ans += 1
    return rank[cnt_0 + ans],rank[ans]

  1. 리스트(집합)간 중복 확인은 set.intersection()로 가능
  2. count()와 max() 사용 생각하기
profile
Statistics & Data Science

0개의 댓글