코딩테스트/🕊️프로그래머스

LEVEL 1. 로또의 최고 순위와 최저 순위

@soohh 2022. 8. 4. 23:56

 

🟪 나의 구현 스케치

당첨번호(win_nums)를 민우의 로또 번호(lottos)와 비교하며 일치하는 개수를 저장한다. 

lottos의 0은 당첨가능한 숫자이므로 최저순위는 0이 모두 당첨번호일 때, 최고순위는 0이 모두 당첨번호일 때이다.

 

🟪 나의 파이썬 구현 코드

def solution(lottos, win_nums):
    answer = []
    zero_count = lottos.count(0) #lottos의 0 개수
    count = 0
    for i in range(len(win_nums)):
        for j in range(len(lottos)):
            if win_nums[i] == lottos[j]: #win_nums를 lottos와 비교
                count += 1
                
    answer.append(7-count-zero_count) #최고 순위
    answer.append(7-count) #최저 순위
    if answer[0]>6: #낙첨인 경우
        answer[0]=6
    if answer[1]>6:
        answer[1]=6
    return answer

 

🟪 다른 사람의 구현 코드 

엄청 깔끔하고 쉬운 코드 발견!!! 나도 더 발전하길...

def solution(lottos, win_nums):

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

    cnt_0 = lottos.count(0) #lottos의 0의 개수
    ans = 0 #일치하는 복권 개수
    for x in win_nums:
        if x in lottos:
            ans += 1
    return [rank[cnt_0 + ans],rank[ans]]

 

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr