๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

4๏ธโƒฃ Python56

[Python] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์‹œ ํ•„์š”ํ•œ ์ฃผ์š” ํŒ (sys.setrecursionlimit(), sys.stdin.readline()) ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ํ’€์–ด๋ณด๋ฉด์„œ ๋ชฐ๋ž๋˜,,! ์‰ฝ์ง€๋งŒ ์ค‘์š”ํ•œ ํŒ๋“ค์„ ๊ฐ„๋‹จํžˆ ์ •๋ฆฌํ•ด๋ณด๊ณ  ์žˆ๋‹ค. ๐Ÿ“ ์žฌ๊ท€ ๊นŠ์ด ์ œํ•œ - sys.setrecursionlimit() ํŒŒ์ด์ฌ์€ ๊ธฐ๋ณธ์œผ๋กœ ์žฌ๊ท€ ๊นŠ์ด๋ฅผ 1000์œผ๋กœ ์ œํ•œํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์žฌ๊ท€๋ฅผ ๋” ๊นŠ๊ฒŒ ๋Œ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด ์ œํ•œ์„ ํ’€์–ด์ค˜์•ผ ํ•œ๋‹ค. ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค. sys ๋ชจ๋“ˆ์„ importํ•˜๊ณ , setrecursionlimit ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. import sys sys.setrecursionlimit(10000) ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ ์ด์œ  ์—†์ด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค๋ฉด ์ด๊ฒƒ์„ ์ƒ๊ฐํ•ด๋ด์•ผ ํ•  ๋“ฏ ํ•˜๋‹ค. ๐Ÿ“ ์ž…๋ ฅ - input() / sys.stdin.readline() ์ž…๋ ฅ ๋ฐ›์„ ๋‚ด์šฉ์ด ๋ณ„๋กœ ์—†๋‹ค๋ฉด input ํ•จ์ˆ˜๋กœ๋„ ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, ์—ฌ๋Ÿฌ ์ค„์˜ ์ž…๋ ฅ์„ ๋งŽ์ด ๋ฐ›์•„์•ผ ํ•œ๋‹ค๋ฉด sys.. 2022. 1. 30.
[Python] ์–•์€ ๋ณต์‚ฌ vs ๊นŠ์€ ๋ณต์‚ฌ (feat. mutable, immutable ๊ฐ์ฒด) ๋ฐฑ์ค€์„ ํ’€๋‹ค๊ฐ€ ํŒŒ์ด์ฌ์€ ๋ณ€์ˆ˜๋ฅผ ๋Œ€์ž…ํ•  ๋•Œ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๊ฐ€ ์ข€ ๋‹ค๋ฅด๊ฒŒ ํ• ๋‹น๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์•Œ์•„๋ณด๋‹ˆ mutable, immutable ๊ฐ์ฒด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์ ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„,, ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์“ฐ๋Š” ๊ธ€ ใ€ฐ๏ธ ๐Ÿ“ mutable ๊ฐ์ฒด์™€ immutable ๊ฐ์ฒด์˜ ๋น„๊ต ํŒŒ์ด์ฌ์—์„œ ๊ฐ์ฒด๋Š” mutable, immutable๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š”๋ฐ, list / set / dict ๋“ฑ์€ mutable ๊ฐ์ฒด, bool / int / str ๋“ฑ์€ immutable ๊ฐ์ฒด์ด๋‹ค. ๐Ÿ‘‰ ๊ตฌ๋ถ„ ๋ฐฉ๋ฒ• list ์ฒ˜๋Ÿผ ํ•˜๋‚˜ ํ•˜๋‚˜์˜ ์š”์†Œ๋ฅผ ๋ฐ”๊ฟ”์„œ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋Š” mutable, str์ฒ˜๋Ÿผ ํ•˜๋‚˜ ํ•˜๋‚˜์˜ ์š”์†Œ๋งŒ์„ ๋ฐ”๊ฟ” ์ €์žฅํ•  ์ˆ˜ ์—†๋Š” ๊ฐ์ฒด๋Š” immutable์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์‰ฝ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ์— list, str์„ ๋Œ€ํ‘œ๋กœ mutable .. 2022. 1. 30.
[Python Algorithm] ์—ฐ๊ตฌ์†Œ BOJ #14502 ๋ฌธ์ œ https://www.acmicpc.net/problem/14502 14502๋ฒˆ: ์—ฐ๊ตฌ์†Œ ์ธ์ฒด์— ์น˜๋ช…์ ์ธ ๋ฐ”์ด๋Ÿฌ์Šค๋ฅผ ์—ฐ๊ตฌํ•˜๋˜ ์—ฐ๊ตฌ์†Œ์—์„œ ๋ฐ”์ด๋Ÿฌ์Šค๊ฐ€ ์œ ์ถœ๋˜์—ˆ๋‹ค. ๋‹คํ–‰ํžˆ ๋ฐ”์ด๋Ÿฌ์Šค๋Š” ์•„์ง ํผ์ง€์ง€ ์•Š์•˜๊ณ , ๋ฐ”์ด๋Ÿฌ์Šค์˜ ํ™•์‚ฐ์„ ๋ง‰๊ธฐ ์œ„ํ•ด์„œ ์—ฐ๊ตฌ์†Œ์— ๋ฒฝ์„ ์„ธ์šฐ๋ ค๊ณ  ํ•œ๋‹ค. ์—ฐ๊ตฌ์†Œ๋Š” ํฌ www.acmicpc.net ๋‚ด ๋ฌธ์ œ ํ’€์ด import copy n, m = map(int, input().split()) space = [] # ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์ง€๋„ virusSpace = [] # ๋ฐ”์ด๋Ÿฌ์Šค์˜ ์œ„์น˜ """ ์ž…๋ ฅ """ for i in range(n): space.append(list(map(lambda x: int(x), input().split()))) for j in range(m): if space[i][j].. 2022. 1. 30.
[Python Algorithm] ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜ BOJ #11724 ๋ฌธ์ œ https://www.acmicpc.net/problem/11724 11724๋ฒˆ: ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฐ„์„ ์˜ ์–‘ ๋์  u์™€ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ u, v ≤ N, u ≠ v) ๊ฐ™์€ ๊ฐ„์„ ์€ ํ•œ ๋ฒˆ๋งŒ ์ฃผ www.acmicpc.net ๋‚ด ๋ฌธ์ œ ํ’€์ด # N์ด 10000๊นŒ์ง€ ์ž…๋ ฅ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•จ import sys sys.setrecursionlimit(10000) input = sys.stdin.readline n, m = map(int, input().split()) connected = [[] for _ in range(n)] vis.. 2022. 1. 29.
[Python Algorithm] ๋ฐ”์ด๋Ÿฌ์Šค BOJ #2606 ๋ฌธ์ œ https://www.acmicpc.net/problem/2606 2606๋ฒˆ: ๋ฐ”์ด๋Ÿฌ์Šค ์ฒซ์งธ ์ค„์—๋Š” ์ปดํ“จํ„ฐ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋Š” 100 ์ดํ•˜์ด๊ณ  ๊ฐ ์ปดํ“จํ„ฐ์—๋Š” 1๋ฒˆ ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋ฒˆํ˜ธ๊ฐ€ ๋งค๊ฒจ์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ์ง์ ‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์ปดํ“จํ„ฐ ์Œ์˜ ์ˆ˜๊ฐ€ ์ฃผ์–ด www.acmicpc.net ๋‚ด ๋ฌธ์ œ ํ’€์ด n = int(input()) c = int(input()) network = [[] for _ in range(n)] queue = [] visited = [False] * n count = 0 for _ in range(c): a, b = map(int, input().split()) network[a-1].append(b-1) network[b-1].append(a-1) def.. 2022. 1. 28.