728x90

PYTHON 26

1๋กœ๋งŒ๋“ค๊ธฐ (๋ฐฑ์ค€1463)

https://www.acmicpc.net/problem/1463 1463๋ฒˆ: 1๋กœ ๋งŒ๋“ค๊ธฐ ์ฒซ์งธ ์ค„์— 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 106๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. www.acmicpc.net Dnamic Programming(๋™์ ๊ณ„ํš๋ฒ•)์€ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ†ตํ•ด ์ค‘๋ณต๊ณ„์‚ฐ๊ฐ’์„ ์ €์žฅํ•ด ํšจ์œจ์„ ๋†’์—ฌ์ค€๋‹ค. 1) Python ์ด์šฉ # DP --> ๋ฉ”๋ชจ์ด์ œ์ด์…˜: ์ค‘๋ณต๊ณ„์‚ฐ๊ฐ’์„ ์ค„์—ฌ ํšจ์œจ ๋†’์—ฌ์คŒ n = int(input()) d = [0] * (n + 1) # d์— ๊ณ„์‚ฐ๊ฐ’ ์ €์žฅ # d[0] = 0, d[1] = 0 for i in range(2, n + 1): d[i] = d[i - 1] + 1 if i % 3 == 0: d[i] = min(d[i], d[i // 3] + 1) if i % 2 == 0: d[i] = ..

์ˆœ์—ด ์‚ฌ์ดํด (๋ฐฑ์ค€ 10451)

https://www.acmicpc.net/problem/10451 10451๋ฒˆ: ์ˆœ์—ด ์‚ฌ์ดํด 1๋ถ€ํ„ฐ N๊นŒ์ง€ ์ •์ˆ˜ N๊ฐœ๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆœ์—ด์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, 8๊ฐœ์˜ ์ˆ˜๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆœ์—ด (3, 2, 7, 8, 1, 4, 5, 6)์„ ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ํ‘œํ˜„ํ•˜๋ฉด \(\begin{pmatrix} 1 & 2 &3&4&5&6&7&8 \\ 3 www.acmicpc.net ์ฒ˜์Œ์— ๊ทธ๋ž˜ํ”„๋ฅผ ๋งŒ๋“ค์–ด์„œ ์žฌ๊ท€๋ฅผ ํ–ˆ๋Š”๋ฐ ์‹œ๊ฐ„ ์ œํ•œ์œผ๋กœ ์‹คํŒจ! ์‹คํŒจํ•œ ์ฝ”๋“œ import sys sys.setrecursionlimit(1000000) input = sys.stdin.readline t = int(input()) def dfs(v): visited[v] = 1 for i in range(n + 1): if vi..

์—ฐ๊ฒฐ์š”์†Œ์˜๊ฐœ์ˆ˜ (๋ฐฑ์ค€ 11724)

์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜ ๋ฌธ์ œ ๋ฐฉํ–ฅ ์—†๋Š” ๊ทธ๋ž˜ํ”„๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์—ฐ๊ฒฐ ์š”์†Œ (Connected Component)์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ž…๋ ฅ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์— ๊ฐ„์„ ์˜ ์–‘ ๋์  u์™€ v๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ u, v ≤ N, u ≠ v) ๊ฐ™์€ ๊ฐ„์„ ์€ ํ•œ ๋ฒˆ๋งŒ ์ฃผ์–ด์ง„๋‹ค. ์ถœ๋ ฅ ์ฒซ์งธ ์ค„์— ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์˜ˆ์ œ ์ž…๋ ฅ 1 6 5 1 2 2 5 5 1 3 4 4 6 ์˜ˆ์ œ ์ถœ๋ ฅ 1 2 https://www.acmicpc.net/problem/11724 11724๋ฒˆ: ์—ฐ๊ฒฐ ์š”์†Œ์˜ ๊ฐœ์ˆ˜ ์ฒซ์งธ ์ค„์— ์ •์ ์˜ ๊ฐœ์ˆ˜ N๊ณผ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 1,000,..

์ด๋ถ„ ๊ทธ๋ž˜ํ”„ (๋ฐฑ์ค€ 1707)

์ด๋ถ„๊ทธ๋ž˜ํ”„ ๋ชจ๋“  ๊ผญ์ง“์ ์„ ๋นจ๊ฐ•๊ณผ ํŒŒ๋ž‘์œผ๋กœ ์ƒ‰์น ํ•˜๋˜, ๋ชจ๋“  ๋ณ€์ด ๋นจ๊ฐ•๊ณผ ํŒŒ๋ž‘ ๊ผญ์ง“์ ์„ ํฌํ•จํ•˜๋„๋ก ์ƒ‰์น ํ•  ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ž˜ํ”„ ์ด๋ถ„ ๊ทธ๋ž˜ํ”„์ธ์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ• ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰(BFS), ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(DFS)์„ ์ด์šฉ → ์„œ๋กœ ์ธ์ ‘ํ•œ ์ •์ ์ด ๊ฐ™์€ ์ƒ‰์ด๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„ X 1. BFS, DFS๋กœ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ •์ ์„ ๋ฐฉ๋ฌธํ•  ๋•Œ๋งˆ๋‹ค ๋‘ ๊ฐ€์ง€ ์ƒ‰ ์ค‘ ํ•˜๋‚˜๋ฅผ ์น ํ•œ๋‹ค. 2. ๋‹ค์Œ ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋ฉด์„œ ์ž์‹ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์€ ์ž์‹ ๊ณผ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•œ๋‹ค. 3. ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•  ๋•Œ ์ž์‹ ๊ณผ ์ธ์ ‘ํ•œ ์ •์ ์˜ ์ƒ‰์ด ์ž์‹ ๊ณผ ๋™์ผํ•˜๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋‹ค. 4. BFS์˜ ๊ฒฝ์šฐ ์ •์ ์„ ๋ฐฉ๋ฌธํ•˜๋‹ค๊ฐ€ ๋งŒ์•ฝ ๊ฐ™์€ ๋ ˆ๋ฒจ์—์„œ ์ •์ ์„ ๋‹ค๋ฅธ ์ƒ‰์œผ๋กœ ์น ํ•ด์•ผ ํ•œ๋‹ค๋ฉด ๋ฌด์กฐ๊ฑด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„๊ฐ€ ์•„๋‹ˆ๋‹ค. ๋ชจ๋“  ์ •์ ์„ ๋‹ค ๋ฐฉ๋ฌธํ–ˆ๋Š”๋ฐ ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๊ฐ€ ์—†๋‹ค๋ฉด ์ด๋ถ„ ๊ทธ๋ž˜ํ”„์ด๋‹ค. โ€ป ์ด..

[Python] set ์ž๋ฃŒํ˜• ์ด์šฉํ•ด ๋ฆฌ์ŠคํŠธ ์ค‘๋ณต ์ œ๊ฑฐ + ๋ฆฌ์ŠคํŠธ ์ฐจ์ง‘ํ•ฉ

๋‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์žˆ์„ ๋•Œ ์„œ๋กœ ์ค‘๋ณต๋˜๋Š” ๊ฐ’์„ ์ œ์™ธํ•œ ๊ฐ’๋“ค์„ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด set ์ž๋ฃŒํ˜•์„ ์ด์šฉ a = [1, 2, 3, 4] b = [2, 3, 5, 6, 7] 2, 3์ด ๊ฒน์น˜๋Š” ์›์†Œ [x for x in a if x not in set(b)]โ€‹ ์ˆœ์„œ๋ฅผ ๋ณด์กดํ•ด ์ฐจ์ง‘ํ•ฉ ์ถœ๋ ฅ set(a) - set(b) ์ˆœ์„œ ๋ณด์กด x ์ฐจ์ง‘ํ•ฉ ์ถœ๋ ฅ ๋ฆฌ์ŠคํŠธ ์ฐจ์ง‘ํ•ฉ a = ['abc', 'abcd', 'abcde'] b = ['bc', 'abc', 'abcd'] print([x for x in a if x not in b])

[Python] ํŒ๋‹ค์Šค ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„(Pandas DataFrame) sys:1: DtypeWarning: Columns have mixed types.Specify dtype option on import or set low_memory=False. ๋ฌด์‹œํ•˜๊ธฐ

column์— NaN๊ฐ’์ด๋‚˜ ์—ฌ๋Ÿฌ type์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์„ž์—ฌ ์žˆ์œผ๋ฉด ์ด์™€ ๊ฐ™์€ ๊ฒฝ๊ณ ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ด ๋•Œ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ์•Œ๋ ค์ฃผ๋Š” ๋Œ€๋กœ dtype option์œผ๋กœ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ๊ฑฐ๋‚˜ low_memory = False๋กœ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋˜์ง€ ์•Š๋Š”๋‹ค. pd.read_csv('[ํŒŒ์ผ๋ช…].txt', delimiter = '\t', low_memory=False)

[Python] ํŒ๋‹ค์Šค ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„(pandas dataframe) SettingWithCopyWarning ํ•ด๊ฒฐ

๊ธฐ์กด ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„ ์ผ๋ถ€๋ฅผ ๋ณต์‚ฌํ•˜๊ฑฐ๋‚˜ ์ธ๋ฑ์‹ฑ ํ›„ ๊ฐ’์„ ์ˆ˜์ •ํ•  ๋•Œ ์ข…์ข… ๋ฐœ์ƒํ•œ๋‹ค. ๊ธฐ์กด ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๊ฐ€์ ธ์™€(๋ณต์‚ฌ) ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๋งŒ๋“ค ๋•Œ ์›๋ณธ์„ ์ˆ˜์ •ํ•  ์ง€ ๋ณต์‚ฌ๋ณธ์„ ์ˆ˜์ •ํ•  ์ง€ ๋ชฐ๋ผ์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜๋ผ๊ณ  ํ•œ๋‹ค. ๋‘ ๊ฐ€์ง€ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜๋Š” ๊ฒฝ๊ณ ๋ฅผ ๋ฌด์‹œํ•˜๋Š” ๊ฒƒ์ด๊ณ  ํ•˜๋‚˜๋Š” copy๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ! 1. ๊ฒฝ๊ณ  ๋ฌด์‹œ pd.set_option์„ ์‚ฌ์šฉํ•œ ๊ฒฝ๊ณ ๋ฌธ ์ œ๊ฑฐ # SettingWithCopyError --> ์˜ค๋ฅ˜ raise ๋กœ ์ฝ”๋“œ ์‹คํ–‰ X pd.set_option('mode.chained_assignment', 'raise') # SettingWithCopyWarning --> ์‹คํ–‰์€ ๋˜์ง€๋งŒ ๊ฒฝ๊ณ ๋ฌธ ๋œธ pd.set_option('mode.chained_assignment', 'warn') # err..

[Python] ํŒ๋‹ค์Šค concat, append, join, merge ์ฐจ์ด

Pandas concat vs append vs join vs merge Concat gives the flexibility to join based on the axis( all rows or all columns) Append is the specific case(axis=0, join='outer') of concat Join is based on the indexes (set by set_index) on how variable =['left','right','inner','couter'] Merge is based on any particular column each of the two dataframes, this columns are variables on like 'left_on', 'ri..

728x90