def solution(n, lost, reserve):
students = [1] * (n)
for i in lost:
students[i-1] -= 1
for i in reserve:
students[i-1] += 1
for idx in range(n):
if idx == 0 and students[idx] == 2 and students[idx+1] == 0:
students[idx], students[idx+1] = 1, 1
elif idx == n-1 and students[idx-1] == 0 and students[idx] == 2:
students[idx-1], students[idx] = 1, 1
# 조건식은 아주 꼼꼼해야 한다.
# 내가 모르는 오류가 발생할 수 있음.
elif idx != 0 and idx != n-1 and students[idx] == 2 and students[idx-1] == 0:
students[idx-1], students[idx] = 1, 1
elif idx != 0 and idx != n-1 and students[idx] == 2 and students[idx+1] == 0:
students[idx], students[idx+1] = 1, 1
students = [x for x in students if x >= 1]
return len(students)
if-elif 문을 사용할 때 조건문을 꼼꼼하게 작성해야 한다. 그렇지 않으면 의도치 않게 작동하여 실패한다.