https://app.codesignal.com/arcade/python-arcade/meet-python/pLsMG462nzEh3axHN
Mex Function | CodeSignal
def solution(s, upperBound): found = -1 for i in range(upperBound): if not i in s: found = i break else: ... return found
app.codesignal.com
✔️문제 요약
- 주어진 집합 s에 대해, s에 없는 음이 아닌 최소 정수 찾기
- 단, UpperBound가 집합에 속해있을 경우 , UpperBound를 반환하라
* 참고로 Mex는 부분집합에 속하지 않는 전체 집합에서의 가장 작은 값
✔️풀이 방법
1. i를 upperBound 까지 쭈욱 검사하면서, i가 집합에 없으면 found 에다가 i를 넣고 끝 (다 찾음, 즉 s에 없는 최소 정수 찾기)
2.[... ]부분에는 집합에 i가 있는 경우이니, 그냥 found에다가 upperbound값을 넣어주면 끝나는 간단한 문제이다.
def solution(s, upperBound):
found = -1
for i in range(upperBound):
if not i in s:
found = i
break
else:
found=upperBound
return found
✔️고찰
else는 보통 if문에 붙는 줄 알았는데 이번 문제에선 for문과 함께 쓰였다.
for문과 함께쓰는 else는,for문이 중간에 break등으로 끊기지 않고, 끝까지 수행 되었을 때 수행하는 코드를 담고 있다고 함!
'코딩테스트 > 코딩테스트 Python' 카테고리의 다른 글
[CodeSignal] singleNumber (Python) (0) | 2022.09.05 |
---|---|
[CodeSignal] amendTheSentence (Python) (0) | 2022.09.05 |
[CodeSignal] List Beautifier (Python) (0) | 2022.09.05 |
[CodeSignal] Collections Truthness (Python) (0) | 2022.09.02 |
[CodeSignal] traverseTree (Python) (0) | 2022.08.26 |
댓글