๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 11723 - ์ง‘ํ•ฉ

by YWTechIT 2021. 6. 16.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 11723 - ์ง‘ํ•ฉ

๋ฐฑ์ค€ 11723 - ์ง‘ํ•ฉ


โšก๏ธ ๋‚˜์˜ ํ’€์ด

add์™€ remove ์กฐ๊ฑด์—์„œ ์ค‘๋ณต๋œ ๊ฒฝ์šฐ๋Š” ๋ฌด์‹œํ•œ๋‹ค๋Š” ์กฐ๊ฑด์„ ๋ณด์•„ set์ž๋ฃŒํ˜•์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ, ์ •๋‹ต์„ ์ œ์ถœํ•˜๋‹ˆ๊นŒ keyError๊ฐ€ ๋–ด์—ˆ๋‹ค ์›์ธ์„ ์ฐพ์•„๋ณด๋‹ค๊ฐ€ remove ํ•จ์ˆ˜ ๋•Œ๋ฌธ์ด์—ˆ๋Š”๋ฐ, ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ์— remove๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด keyError๊ฐ€ ๋‚œ๋‹ค.

 

์ด๋Ÿด ๋•Œ๋Š” discardํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์ž. ๊ฐ’์ด ์—†๋Š” ์ƒํƒœ์—์„œ discard๋ฅผ ์‚ฌ์šฉํ•ด๋„ ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š๋Š”๋‹ค. ๋˜, empty๋Š” ๋นˆ ์ง‘ํ•ฉ์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š”๋ฐ ๊ทธ๋ƒฅ set()์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค˜๋„ ๋˜์ง€๋งŒ clear ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์ค˜๋„ ๋œ๋‹ค. ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ํฌ๊ฒŒ ์ฐจ์ด๊ฐ€ ์—†์—ˆ๋‹ค.

 

import sys
input = sys.stdin.readline

empty_set = set()

def add(x):
    empty_set.add(x)

def remove(x):
    empty_set.discard(x)

def check(x):
    if x in empty_set:
        return 1
    return 0

def toggle(x):
    if x in empty_set:
        empty_set.discard(x)
    else:
        empty_set.add(x)

def set_all():
    global empty_set
    empty_set = {i for i in range(1, 21)}

def empty():
    empty_set.clear()

for _ in range(int(input())):
    command = input().split()
    if command[0] == 'add':
        add(int(command[1]))
    elif command[0] == 'remove':
        remove(int(command[1]))
    elif command[0] == 'check':
        print(check(int(command[1])))
    elif command[0] == 'toggle':
        toggle(int(command[1]))
    elif command[0] == 'all':
        set_all()
    else:
        empty()
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€