honey_pot

[python] 로또의 최고 순위와 최저 순위 본문

문제 풀이

[python] 로또의 최고 순위와 최저 순위

_tera_ 2021. 9. 9. 21:56

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

 

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

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

programmers.co.kr

 

def solution(lottos, win_nums):
    answer = []
    match = 0
    zero = lottos.count(0)
    for i in lottos:
        if i in win_nums:
            match += 1
    
    if match==0 and zero==0:
        answer.append(6)
    else:
        answer.append(6-(zero+match)+1)
    
    if match == 0 :
        answer.append(6)
    else:
        answer.append(6-match+1)
    return answer

 

최고순위는 6-0의 개수-맞는 번호 +1

최저순위는 6 - 맞는 번호 +1

match와 zero 가 하나도 없을 경우, match만 없는 경우를 if 문으로 조건을 주고 출력했다.

 

다른 방법으로 아예 순위를 리스트에 넣고 인덱스로 순위를 뽑는 방법이 있던데 이게 더 직관적이고 편한것같다.

 

def solution(lottos,win_nums):
	answer = [0,0]
    rank = [6,65,4,3,2,1]
    
    cnt =0
    zero = lottos.count(0)
    
    for i in lottos:
	if i in win_nums:
    	cnt += 1
        
    answer[0],answer[1] = rank[cnt+ zero], rank[cnt]
    
   	return answer

'문제 풀이' 카테고리의 다른 글

[python] 체육복  (0) 2021.09.09
[python] 크레인 인형뽑기 게임  (0) 2021.09.09
[python] 비밀지도  (0) 2021.09.09
[python] 폰켓몬  (0) 2021.09.08
[python] 키패드 누르기  (0) 2021.09.02
Comments