눈송이의 개발생활

[Programmers Lv.1]로또의 최고 순위와 최저 순위(Python) 본문

Algorithm/Programmers

[Programmers Lv.1]로또의 최고 순위와 최저 순위(Python)

꾸지새미언니

문제

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

내 코드 

def solution(lottos, win_nums):
    answer = []
    match = 0
    nomatch = 0
    for item in lottos:
        if item in win_nums:
            match += 1 # number of matching numbers
        elif item == 0 :
            nomatch += 1
    answer.append(7 - (match + nomatch if match+nomatch > 1 else 1))
    answer.append(7 - (match if match >= 2 else 1))
    return answer

다른 사람 풀이 

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개일 때와 0개일 때 모두 6등이라서 조건을 주었다. 

 

다른 사람이 푼 풀이를 보니 등수를 미리 배열로 만들어서 각 등수의 index를 일치하는 숫자의 수로 했었다. 정말 똑똑하다... 어떻게 이런 방법을 생각해 내는거지? 와우 대단해 

 

✅ 이미 정해진(?) 수가 있고, index와 연결시킬 수 있으면 배열을 미리 만들어 두자. 

 

Comments