๋ฐ˜์‘ํ˜•

Python/์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 10

์ฝ”ํ…Œ ์‹œ๊ฐ„๋ณต์žก๋„ ์ •๋ณตํ•˜๊ธฐ (๋น…์˜ค cheat sheet)

https://github.com/jwasham/coding-interview-university?tab=readme-ov-file#algorithmic-complexity--big-o--asymptotic-analysis GitHub - jwasham/coding-interview-university: A complete computer science study plan to become a software engineer.A complete computer science study plan to become a software engineer. - jwasham/coding-interview-universitygithub.com์ฝ”๋”ฉํ…Œ์ŠคํŠธ์— ๊ด€ํ•œ ๊ฐœ์š”, ์ „๋ฐ˜์ ์œผ๋กœ ์ฐธ๊ณ ํ•˜๊ธฐ ์ข‹์€ ๊ธ€๋น…์˜ค ํ‘œ๊ธฐ๋ฒ• cheat..

[python] packing & unpacking ๋ฆฌ์ŠคํŠธ, ํŠœํ”Œ

ํŠœํ”Œ ์–ธํŒจํ‚น ์˜ˆ์‹œ - ๋ฆฌ์ŠคํŠธ๋Š” ()๋ฅผ []๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋ฆฌ์ŠคํŠธ ํŒจํ‚น, ์–ธํŒจํ‚น์ด ๊ฐ€๋Šฅํ•˜๋‹ค. a, b = (1, 10) print(a) print(b) 1 10 a, *b, c = (1, 2, 3, 4, 5) print(a) print(b) print(c) 1 [2,3,4] 5 *์€ ๋‚˜๋จธ์ง€๋ฅผ ๋ฌถ์–ด์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ํŽธํ•˜๋‹ค.

[python] ์‚ผํ•ญ์—ฐ์‚ฐ์ž(Ternary operators) - ์กฐ๊ฑด๋ฌธ

value1 if condition else value2 condition์ด ์ฐธ์ด๋ฉด value1, ๊ฑฐ์ง“์ด๋ฉด value2๋ฅผ ๋ฐ˜ํ™˜. if ์™€ else๋กœ ๋‚˜๋‰˜๋Š” ์กฐ๊ฑด๋ฌธ์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ! ์˜ˆ์‹œ value = 10 "odd" if value%2 else "even" ๊ฒฐ๊ณผ "even"

[python] ํŒŒ์ด์ฌ ๋‚ด์žฅํ•จ์ˆ˜ ๋ชฉ๋ก

https://docs.python.org/ko/3/library/functions.html ๋‚ด์žฅ ํ•จ์ˆ˜ — Python 3.11.0 ๋ฌธ์„œ ๋‚ด์žฅ ํ•จ์ˆ˜ ํŒŒ์ด์ฌ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์—๋Š” ํ•ญ์ƒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋งŽ์€ ํ•จ์ˆ˜์™€ ํ˜•์ด ๋‚ด์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•ฉ๋‹ˆ๋‹ค. abs(x, /) ์ˆซ์ž์˜ ์ ˆ๋Œ“๊ฐ’์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. ์ธ์ž๋Š” ์ •์ˆ˜, ์‹ค์ˆ˜ ๋˜๋Š” docs.python.org

[python ์ž๋ฃŒ๊ตฌ์กฐ] ์ง‘ํ•ฉ set

- ๋”•์…”๋„ˆ๋ฆฌ์˜ ํ‚ค๋งŒ ๋ชจ์—ฌ์žˆ๋Š” ํ˜•ํƒœ ๋นˆ ์ง‘ํ•ฉ ์„ ์–ธ s = set() s=set([1,2,3,'set']) print(s) s.add(4) s.add('set') print(s)#์ค‘๋ณตx s.remove(2) #s.remove(99)#์—๋Ÿฌ๋ฐœ์ƒ s.discard(99)#์—๋Ÿฌ ๋ฌด์‹œ print(s) s.update([3,99,True,None]) print(s) s.clear() print(s) result {1, 2, 3, 'set'} {1, 2, 3, 4, 'set'} {1, 3, 4, 'set'} {None, 1, 3, 4, 99, 'set'} set() ํ•ฉ์ง‘ํ•ฉ, ๊ต์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ, ๋ฐฐํƒ€์  ํ•ฉ์ง‘ํ•ฉ ์—ฐ์‚ฐ ๊ฐ€๋Šฅ s1 &s2 s1 | s2 s1 - s2 s1 ^ s2

[python ์ž๋ฃŒ๊ตฌ์กฐ] ๋”•์…”๋„ˆ๋ฆฌ dictionary

- ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•จ - key์™€ value๋กœ ์ด๋ฃจ์–ด์ง {key1: Value1,key2:Value2,,,} key์—๋Š” hashableํ•œ ๊ฒƒ๋“ค์„ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ value๋Š” ์ œํ•œ์ด ์—†์Œ ๋นˆ ๋”•์…”๋„ˆ๋ฆฌ ์„ ์–ธ dict={} ์‹คํ–‰ ์˜ˆ์‹œ dictionary={} dictionary['first']=1 dictionary['tuple']=(1,2,3) dictionary[(4,'tuple')]=1.5 print(dictionary) ###{'first': 1, 'tuple': (1, 2, 3), (4, 'tuple'): 1.5} print(dictionary['tuple'])#(1, 2, 3) print(dictionary[4,'tuple'])#1.5 dictionary['first']='one' #์ค‘๋ณต์€ ๋ฎ์–ด์”Œ์›Œ์ง p..

[python] ํŒŒ์ด์ฌ ํŠน์ง•๊ณผ ์žฅ์ 

1. ์ธํ„ฐํ”„๋ฆฌํ„ฐ ์–ธ์–ด์ด๋‹ค. - ์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ์ปดํŒŒ์ผ๊ณผ ๋Œ€๋น„๋˜๋Š” ๊ฐœ๋…์œผ๋กœ, ์ฝ”๋“œ ํŒŒ์ผ(.py)๋ฅผ ์ปดํ“จํ„ฐ์— ์ž‘๋™ํ•˜๊ฒŒ๋” ๋„ฃ์„ ๋•Œ ์ปดํ“จํ„ฐ ์–ธ์–ด์ธ 0๊ณผ 1๋กœ ๋ณ€ํ™˜ํ•ด์•ผํ•œ๋‹ค. ์ด ๊ณผ์ •์—์„œ ์“ฐ๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์ปดํŒŒ์ผ๊ณผ ์ธํ„ฐํ”„๋ฆฌํ„ฐ์ด๋‹ค. ๋Œ€ํ‘œ์ ์ธ ์ปดํŒŒ์ผ ์–ธ์–ด๋Š” C์ด๊ณ , ์ธํ„ฐํ”„๋ฆฌํ„ฐ์–ธ์–ด๋Š” python, js ๋“ฑ์ด ์žˆ๋‹ค. ์ปดํŒŒ์ผ ์–ธ์–ด๋Š” ๊ณ ๋Œ€๋กœ ์“ฐ๋ฉด ๋˜์ง€๋งŒ, ์ธํ„ฐํ”„๋ฆฌํ„ฐ์–ธ์–ด๋Š” ์ฝ”๋“œ์™€ ์ธํ„ฐํ”„๋ฆฌํ„ฐ๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค. 2. ์™„์ „ ๊ฐ์ฒด ์ง€ํ–ฅ ์–ธ์–ด์ด๋‹ค. int,, float,,, ๋“ฑ๋“ฑ ๋ชจ๋‘๊ฐ€ ๊ฐ์ฒด์ด๋‹ค. 3. ๋™์  ํƒ€์ดํ•‘ ์–ธ์–ด์ด๋‹ค. - ์ž๋ฐ”์˜ ๊ฒฝ์šฐ ๋ณ€์ˆ˜๋งˆ๋‹ค ์ž๋ฃŒํ˜•์„ ๋ช…์‹œํ•ด์•ผํ•˜์ง€๋งŒ, ํŒŒ์ด์ฌ์€ a=3์ด๋ผ๊ณ  ์ž…๋ ฅํ•ด๋„ ์ž๋™์œผ๋กœ int๋กœ ์ธ์‹ํ•œ๋‹ค. 4. ์‰ฌ์šด ๋ฌธ๋ฒ•๊ณผ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. 5. ๋‹ค์–‘ํ•œ ๊ณณ์—์„œ ๋„๋ฆฌ ์“ฐ์ธ๋‹ค. 6. ๋ฐ์ดํ„ฐ ๊ฐ€๊ณต์—์„œ ๋‘๊ฐ์„..

ํž™ heap, max heap, min heap ๊ฐœ๋…

ํž™์€ ์ด์ง„ํŠธ๋ฆฌ์˜ ํ•œ ์ข…๋ฅ˜๋กœ, ๊ฐ’์ด ์ตœ๋Œ€ ํ˜น์€ ์ตœ์†Œ ๋…ธ๋“œ์— ๋น ๋ฅด๊ฒŒ ์ ‘๊ทผํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ํž™ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์„ค๊ณ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์—๋Š” 2๊ฐ€์ง€, max heap(์ตœ๋Œ€ํž™)๊ณผ min heap(์ตœ์†Œ ํž™)์ด ์žˆ๋‹ค. ์ตœ๋Œ€ํž™์€ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ํž™์—์„œ ๊ฐ€์žฅ ํฌ๊ณ , ๋…ธ๋“œ์˜ ๊ฐ ๊ฐ’์€ ๋ถ€๋ชจ๋…ธ๋“œ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค ์ตœ์†Œํž™์€ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ํž™์—์„œ ๊ฐ€์žฅ ์ž‘๊ณ , ๋…ธ๋“œ์˜ ๊ฐ ๊ฐ’์€ ๋ถ€๋ชจ๋…ธ๋“œ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค

์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ

์ด์ง„ํŠธ๋ฆฌ - ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ, ๊ฐ ๋ถ€๋ชจ๋…ธ๋“œ๊ฐ€ ํ•ญ์ƒ ์ตœ๋Œ€ 2๊ฐœ์˜ ์ž์‹๋…ธ๋“œ์™€ ๋ถ™์–ด ์žˆ์Œ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ : ์ด์ง„ ํŠธ๋ฆฌ์˜ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ ์œ ํ˜•, ๋…ธ๋“œ์˜ key ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ์ƒํƒœ - ์ •๋ ฌ ๊ธฐ์ค€ 1. ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์—๋Š” ๋…ธ๋“œ์˜ ํ‚ค๋ณด๋‹ค ์ž‘์€ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๋งŒ! 2. ๋…ธ๋“œ์˜ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ๋Š” ๋…ธ๋“œ์˜ ํ‚ค๋ณด๋‹ค ํฐ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๋งŒ! 3. ์ขŒ์šฐ ์„œ๋ธŒ ํŠธ๋ฆฌ๋„ ๊ฐ๊ฐ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ 4. ๊ฐ ๋…ธ๋“œ์— ์ค‘๋ณต ํ‚ค(key)๋Š” ์—†์Œ! ๊ทธ๋ž˜์„œ.. ๊ฐ€์žฅ ํฐ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๋Š” ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ๋ง๋‹จ(80) ๊ฐ€์žฅ ์ž‘์€ ํ‚ค๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๋Š” ๊ฐ€์žฅ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ๋ง๋‹จ(1) ์ด์ง„ํƒ์ƒ‰ํŠธ๋ฆฌ๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ๋™์ž‘์€ 1. ํŠธ๋ฆฌ์— ๋…ธ๋“œ ์ถ”๊ฐ€ 2. ๋…ธ๋“œ ์‚ญ์ œ 3. ๋…ธํŠธ ์„ ํƒํ•ด ํƒ์ƒ‰ํ•˜๋Š” ํ‚ค๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ

์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ์™€ ๋น„์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์ฐจ์ด (Linear, NonLinear data structure)

์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ์™€ ๋น„์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋‚˜๋‰œ๋‹ค. ์„ ํ˜•์ž๋ฃŒ๊ตฌ์กฐ์™€ ๋น„์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋ฌด์—‡์ผ๊นŒ? ์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๋ž€ ํ•˜๋‚˜์˜ ์ž๋ฃŒ ๋’ค์— ํ•˜๋‚˜์˜ ์ž๋ฃŒ๊ฐ€ ์กด์žฌ,์ฆ‰ ์ˆœ์ฐจ์ ์œผ๋กœ ์ด์–ด์ง„ ํ˜•ํƒœ๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. 1:1์˜ ์„ ํ˜•๊ด€๊ณ„๋ฅผ ์ด๋ฃจ๊ณ  ์žˆ๋‹ค ๋น„์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๋ž€ ํ•˜๋‚˜/์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž๋ฃŒ ๋’ค์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž๋ฃŒ๊ฐ€ ์กด์žฌํ•˜๋Š” ํ˜•ํƒœ๋กœ, 1:n, ๋˜๋Š” n:n ์˜ ๊ด€๊ณ„๋ฅผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.(์ฃผ๋กœ ๊ณ„์ธต ํ˜•ํƒœ)

๋ฐ˜์‘ํ˜•