[프로그래머스] 1단계_로또의 최고 순위와 최저 순위 (python)

juyeon·2022년 6월 14일
0

코딩테스트(python)

목록 보기
2/22

프로그래머스

1단계_로또의 최고 순위와 최저 순위

나의 풀이

def solution(lottos, win_nums):
    answer = [0] * 2 #answer 2 길이만큼 0으로 세팅
    lottos_set = set(lottos)
    win_nums = set(win_nums)
    result = len(lottos_set & win_nums) #교집합
    zero_count = list(lottos).count(0)  
    #set은 0 중복이 허용 안되니까 그냥 lottos 쓰기
    -> 생각해보니, 이미 lottos는 list라서 list(lottos) 할 필요가 없네..?
    
    if result >= 1:
        answer[0] = range(6,0,-1)[result + zero_count - 1]
        #최고순위 개수 = 최저순위 교집합 개수 + 0의 개수
        answer[1] = range(6,0,-1)[result - 1]
    elif zero_count >= 1: #교집합이 0인경우 
        answer[0] = 1
        answer[1] = 6
    else: #교집합도 없고 0도 없는 경우, 즉 6개 다 틀림
        answer[0] = answer[1] = 6
        
    return answer

다른 사람 풀이(프로그래머스)

def solution(lottos, win_nums):

    rank=[6,6,5,4,3,2,1]
    #깔끔하게 range 대신에 그냥 list 나열

    cnt_0 = lottos.count(0)
    ans = 0
    for x in win_nums:
        if x in lottos: 
        #set 변환 후 교집합 셀 필요 없이, for과 if로 해결
            ans += 1
    return rank[cnt_0 + ans],rank[ans]

: 등수 list에서 6이 두번 들어가는걸 어떻게 해야하나 고민했는데.. 이게 짧으니까 그냥 list를 만드는게 더 편하구나ㅠ그리고 교집합!! 그냥 for과 if로 만들자~

def solution(lottos, win_nums):
    rank = {
        0: 6,
        1: 6,
        2: 5,
        3: 4,
        4: 3,
        5: 2,
        6: 1
    }
    #set를 이용
    
    return [rank[len(set(lottos) & set(win_nums)) + lottos.count(0)], rank[len(set(lottos) & set(win_nums))]]
    #교집합+0 개수, 교집합 이라는 아이디어는 나와 같다

: 기본 아이디어는 나랑 같은데, 훠얼씬 간단하다.

profile
내 인생의 주연

0개의 댓글