Computer Knowledge/코테 대비 오답노트

[python] 4개 숫자들 중 중복된 숫자마다 처리 - count(), index()

l._.been 2023. 5. 14. 23:41
728x90
문제

https://school.programmers.co.kr/learn/courses/30/lessons/181916#qna

 

프로그래머스

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

programmers.co.kr

핵심 능력

python 메소드 중 count()와 index()를 알면 쉽게 풀 수 있는 문제였다.

  • count(): 리스트 중에서 얼마나 중복되는지 나타냄
  • index(): 매개변수로 넘어온 수의 인덱스를 반환
인상적인 풀이
def solution(a, b, c, d):
    arr = [a,b,c,d]
    counts = [arr.count(i) for i in arr] # 중복 리스트 생성
    
    # all same number
    if max(counts) == 4:
        return 1111*a
    
    # three same numbers and the other
    elif max(counts) == 3:
        p = arr[counts.index(3)] # 같은 수 3개의 인덱스
        q = arr[counts.index(1)] # 다른 수 1개의 인덱스
        return (10*p+q)**2
    
    # two pair of two same numbers
    elif max(counts) == 2: # 같은 수 2개 
        if min(counts) == 2: # 같은 수 2개가 2쌍 있다면
            return (a+c)*abs(a-c) if a==b else (a+b)*abs(a-b)
        
    # two same numbers and another and the other
        else:
            p = arr[counts.index(2)] # 같은 수 2개 있는 애의 인덱스
            return (a*b*c*d)/p**2 # 서로 다른 수끼리 곱(같은 수는 분모로 빼줌)
        
   # differenct numbers:
    else:
        return min(arr) # 가장 작은 수