문제 

In a land far, far away, three beautiful princes are trying to win the hand of a beautiful princess.
The princess, alas, wants to make sure that she chooses the prince with the greatest dowry, but she also wants to keep one of the princes from feeling too good about himself.
So she decides to marry the prince whose dowry is in the middle of the three.
Write a program to find the amount of the middle dowry.

해석

세 개의 서로 다른 정수가 한 줄에 하나씩 주어지면, 중간 크기의 정수를 출력하는 문제이다.

 


 

풀이 1: max()와 min() 제거

import sys
lines = sys.stdin.read().strip().split('\n')

intList = list(map(int, lines))

def findMiddleNum():
    intList.remove(max(intList))
    intList.remove(min(intList))
    return intList[0]

print(findMiddleNum())

 

풀이 2: 정렬 후 중간값 반환

import sys
lines = sys.stdin.read().strip().split('\n')

intList = list(map(int, lines))
intList.sort()

print(intList[1])

 

비교 요약

방식 실행 시간 메모리 사용 장점 단점
remove() 32ms 32412KB 직관적이고 구현이 간단함 중복 값이 있으면 안전하지 않음
sort() 32ms 32412KB 코드가 간결하고 중복에 강함 데이터가 많으면 정렬 연산에 시간이 걸림

 

 

 


 

 

 

끝!