[programmers] 로또의 최고 순위와 최저 순위(Python)

한은기·2021년 7월 25일
0

Problem

출처 : 2021 Dev-Matching: 웹 백엔드 개발자(상반기) 문제 모음


Solution

"""
나의 풀이
"""
def solution(lottos, win_nums):
  lottos = list(filter(lambda x : x!=0, sorted(lottos)))
  correct = len([num for num in lottos if num in sorted(win_nums)])
  answer = [correct + (6-len(lottos)), correct]
  return list(map(lambda c : 7-c if c>=2 else 6, answer))


"""
다른 사람의 풀이(1) : cont() 함수 사용
"""
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]
  

"""
다른 사람의 풀이(2) : dictionary와 set() 함수, & 연산자 사용
"""
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))]]

Review

for문을 최소화하고 리스트 내포, filter(), lambda함수, map()의 사용법을 익혀보려 작성한 코드이다. 다른 이들의 코드를 살펴보니 더 어렵게 구현한 것 같은 생각이 든다.


GitHub Link

AlgorithmStudy/programmers_로또의 최고순위와 최저순위

profile
🏫Inha Univ. Naval Architecture and Ocean Engineering & Computer Engineering (Undergraduate) / 🚢Autonomous Vehicles, 💡Machine Learning

0개의 댓글