set을 이용하는 문제로 set 삽입, 삭제 메소드인 add remove discard만 알면 무난한 코드로 풀린다.
import sys
input = sys.stdin.readline
m = int(input())
S = set()
for _ in range(m):
    commands = input().strip().split()
    
    # split()을 통해 띄어씌기가 있으면 튜플인 commands
    # 없으면 그냥 commands
    if len(commands) == 1:
        if commands[0] == "all":
            S = set([i for i in range(1, 21)])
        else:
            S = set()
    # set은 add remove discard(discard는 없는거 없애려해도 오류아냄.)
    else:
        command, num = commands[0], commands[1]
        num = int(num)
        if command == "add":
            S.add(num)
        elif command == "remove":
            S.discard(num)
        elif command == "check":
            print(1 if num in S else 0)
        elif command == "toggle":
            if num in S:
                S.discard(num)
            else:
                S.add(num)'practivceAlgorithm > 백준' 카테고리의 다른 글
| [백준][Python] 5014 스타트링크 (0) | 2021.07.22 | 
|---|---|
| [백준][Python] 11725 트리의 부모찾기. (0) | 2021.07.22 | 
| [백준][Python] 2263 트리의 순회 (0) | 2021.07.21 | 
| [백준][Python] 1759 암호만들기 (0) | 2021.07.21 | 
| [백준][Python] 11725 트리의 부모찾기 (0) | 2021.07.21 |