본문 바로가기
TIL - 프로그래밍/Python 알고리즘

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

by chaemj97 2022. 6. 4.
728x90

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

 

코딩테스트 연습 - 로또의 최고 순위와 최저 순위

로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호

programmers.co.kr


  • Code1
def solution(lottos, win_nums):
    rank = {0:6,1:6,2:5,3:4,4:3,5:2,6:1}
    
    # 오른차순 정렬
    lottos.sort()
    win_nums.sort()
    
    # 당첨 갯수 세기
    cnt = 0
    for i in win_nums:
        for j in lottos:
            if i == j:
                cnt += 1
                break
                
    # 0이 모두 다 당첨번호라고 생각
    luck = lottos.count(0)
    
    return rank[cnt+luck],rank[cnt]

  • Code2
def solution(lottos, win_nums):
    rank = {0:6,1:6,2:5,3:4,4:3,5:2,6:1}
    return rank[len(set(lottos) & set(win_nums)) + lottos.count(0)], rank[len(set(lottos) & set(win_nums))]
  • 풀면서 얻은 점
    • set & set : 교집합
      • 교집합으로 푸는 거 똑똑한 거 같다. 
728x90
반응형

댓글