Python/코딩 테스트

알고리즘 먼데이 2주차 3번 출석부 문제 해결했다 이말이야

Young_Metal 2022. 10. 18. 16:48
import sys
from functools import cmp_to_key
N, k = map(int, sys.stdin.readline().split())

def cmp(a, b):
    if a[0] == b[0]:
        return a[1] < b[1]
    else:
        return a[0] < b[0]
arr = [] # [[string1, int1], [string2, int2], ...]

for _ in range(N):
	tmp = list(sys.stdin.readline().split())
	arr.append(tmp)
	
arr = sorted(arr,key=cmp_to_key(cmp))
arr = sorted(arr,key = lambda x: (x[0],x[1]))
print(arr[k-1][0]+' '+arr[k-1][1])

처음에 하나 가지고 sort하는 건 오케이인데, 나중에 두개를 sort 하는 것을 몰랐다. 

아니 그냥 arr를 저장해서 첫번째 기준으로 하나 sort하고 

다음 줄에 다시 arr를 두번째 기준으로 sort하면 되는 것이었다. 

cmp_to_key(cmp)는 굉장히 낮선 라이브러리라 조큼 탐구가 필요해보인다.