Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

NISSO

[프로그래머스 Lv1][해시] 완주하지 못한 선수 본문

Coding Test

[프로그래머스 Lv1][해시] 완주하지 못한 선수

oniss 2021. 7. 2. 14:01
def solution(p, c):
    p.sort()
    c.sort()
    for i in range(len(c)):
        if p[i] != c[i]:
            return p[i]
    return p[-1]

내 풀이.

for i,j in zip(p,c):
	if i!=j:
		return i

for문을 이렇게 바꾸면 더 깔끔해진다.

 

그리고 알게 된 건, Counter 객체끼리는 뺄셈이 가능하다는 것. 리스트끼리는 왜 뺄셈이 안 되냐고 생각했었는데 Counter객체가 가능한지는 몰랐다.

import collections

def solution(participant, completion):
    answer = collections.Counter(participant) - collections.Counter(completion)
    return list(answer.keys())[0]

아주 간단해진다.

Comments