Development

[프로그래머스] 로또의 최고 순위와 최저 순위 본문

코딩테스트/Python

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

yo~og 2021. 8. 4. 23:01
반응형

내 코드

def solution(lottos, win_nums):
    answer = []
    dic = {6:1,5:2,4:3,3:4,2:5,1:6,0:6}
    
    cnt=0
    
    for num in win_nums:
        if num in lottos:
            cnt+=1
    
    cnt0 = lottos.count(0)

    if cnt+cnt0 > 6:
        answer.append(1)
    else:
        answer.append(dic[cnt+cnt0])
    
    answer.append(dic[cnt])
    
    
    return answer

먼저 lottos 리스트와 win_nums가 일치하는 횟수를 세어준다. 여기서 0의 갯수를 더하면 최고순위, 더하지 않으면 최저 순위이다. ( 0이 로또번호일때가 최고순위, 아닐때가 최저순위이기때문임)

 

1. 일치하는 횟수를 for문을 사용하여 세어주었다. 

2. 로또에 0의 갯수를 count해주어 cnt0에 넣어준다.

3. cnt와 cnt0의 합이 6이 넘으면 1등이기 때문에 answer에 1을 넣어준다.

4. 넘지 않으면 미리 만들어놓은 딕셔너리(순위 딕셔너리)에 값을 넣어 answer에 답을 넣어준다.

5. dic[cnt] (최저 순위)를 넣어준다.

 

 

다른사람 코드

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]

비슷하게 푼거같은데.. 왜캐 짧지?? append안쓰고 저렇게 return 해도 되는지 처음알았다!! 신기,, 

이코드 보면서 생각해보니까 cnt_0이랑 ans합이 6을 넘을수가없는데 난 왜 if else를 썻는지 모르겠다 바본가..ㅋㅋㅋㅋㅋㅋ 다른건 비슷하니까 설명은 생락하겠다!

 

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))]]

 

이사람이 대박이다.. 어캐 2줄만에 가능하지??ㅋㅋㅋㅋㅋㅋ 

set으로 바꿔서 중복을 제거한후 두 리스트에서 겹치는 것만 두고 길이를 구한다. 하나는 0 개수를 더하고 하나는 안더한다! 대박이다ㅠ

반응형
Comments