명령의 길이를 0과 1에 따라서 구분한 다음 어떤 명령을 수행할 것인지 분류하여 코드를 작성했다.
import sys
M = int(input())
data_set = set()
for _ in range(M):
input_command = list(map(str, sys.stdin.readline().strip().split()))
if len(input_command) == 2:
command, data = input_command[0], int(input_command[1])
if command == "add":
if data in data_set:
continue
else:
data_set.add(data)
elif command == "remove":
if data in data_set:
data_set.remove(data)
else:
continue
elif command == "check":
if data in data_set:
print(1)
else:
print(0)
elif command == "toggle":
if data in data_set:
data_set.remove(data)
else:
data_set.add(data)
else:
command = input_command[0]
if command == "all":
data_set = set([i for i in range(1, 21)])
elif command == "empty":
data_set.clear()