๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ 132

[Python] ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ (Decorator)

๋ฐ์ฝ”๋ ˆ์ดํ„ฐ (Decorator) ? - ํ•จ์ˆ˜ ์•ž๋’ค์— ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด์„œ ์† ์‰ฝ๊ฒŒ ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋ฒ• - Closure function - ์—ฌ๋Ÿฌ ํ•จ์ˆ˜์— ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ @๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•˜๋‚˜๋กœ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ถ”๊ฐ€ Closure function ํ•จ์ˆ˜์™€ ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํ•จ๊ป˜ ๋ณต์‚ฌ, ์ €์žฅํ•ด์„œ ๋ณ„๋„ ํ•จ์ˆ˜๋กœ ํ™œ์šฉํ•˜๋Š” ๊ธฐ๋ฒ• ์™ธ๋ถ€ ํ•จ์ˆ˜๊ฐ€ ์†Œ๋ฉธํ•˜๋”๋ผ๋„, ์™ธ๋ถ€ ํ•จ์ˆ˜ ์•ˆ์— ์žˆ๋Š” ๋กœ์ปฌ ๋ณ€์ˆ˜ ๊ฐ’๊ณผ ์ค‘์ฒฉํ•จ์ˆ˜(๋‚ด๋ถ€ํ•จ์ˆ˜)๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ def outer_func(num): #์ค‘์ฒฉ ํ•จ์ˆ˜์—์„œ ์™ธ๋ถ€ ํ•จ์ˆ˜์˜ ๋ณ€์ˆ˜์— ์ ‘๊ทผ ๊ฐ€๋Šฅ def inner_func(): print(num) return 'hi' return inner_func #์ค‘์ฒฉ(๋‚ด๋ถ€) ํ•จ์ˆ˜ ์ด๋ฆ„์„ ๋ฆฌํ„ด closure_func = outer_func(10) #First-cl..

Python 2024.01.31

[Python] python comprehension

Python comprehension ? ๋‹ค๋ฅธ Sequence๋กœ๋ถ€ํ„ฐ ์ƒˆ๋กœ์šด Sequence (Iterable Object)๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ * comprehension : ์ดํ•ด๋ ฅ 01. List comprehension [ ์ถœ๋ ฅ ํ‘œํ˜„์‹ for ์š”์†Œ in ์ž…๋ ฅ Sequence [ if ์กฐ๊ฑด์‹ ] ] ์ž…๋ ฅ Sequence๋Š” Iteration์ด ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ Sequence ํ˜น์€ ์ปฌ๋ ‰์…˜ [if ์กฐ๊ฑด์‹]์€ ์˜ต์…˜ hash_table = list([0 for i in range(10)]) print(hash_table) #[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] #1. ์ข…๋ฅ˜๊ฐ€ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ์—์„œ ์ •์ˆ˜ ๋ฆฌ์ŠคํŠธ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ dataset = [4, True, 'dave', 2.1, 3] int_data ..

Python 2024.01.31

[Algorithm] ์žฌ๊ท€ ์šฉ๋ฒ• (recursive call, ์žฌ๊ท€ ํ˜ธ์ถœ)

์žฌ๊ท€ ์šฉ๋ฒ•(recursive call) ? - ํ•จ์ˆ˜ ์•ˆ์—์„œ ๋™์ผํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ˜•ํƒœ - ์—ฌ๋Ÿฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ์‚ฌ์šฉ ์žฌ๊ท€ํ˜ธ์ถœ์˜ ์ผ๋ฐ˜์ ์ธ ํ˜•ํƒœ ๋‚ด๋ถ€์ ์œผ๋กœ ์Šคํƒ(Stack)์ฒ˜๋Ÿผ ๊ด€๋ฆฌ๋œ๋‹ค #case1 def function(์ž…๋ ฅ): if ์ž…๋ ฅ > ์ผ์ • ๊ฐ’: # ์ž…๋ ฅ์ด ์ผ์ • ๊ฐ’ ์ด์ƒ์ด๋ฉด return function(์ž…๋ ฅ - 1) # ์ž…๋ ฅ ๊ฐ’๋ณด๋‹ค ์ž‘์€ ๊ฐ’ else: return ์ผ์ • ๊ฐ’, ์ž…๋ ฅ ๊ฐ’, ๋˜๋Š” ํŠน์ • ๊ฐ’ # ์žฌ๊ท€ํ˜ธ์ถœ ์ข…๋ฃŒ #case2 def function(์ž…๋ ฅ): if ์ž…๋ ฅ

[Algorithm] ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (Sort Algorithm)

์ •๋ ฌ (Sorting) ? - ์–ด๋–ค ๋ฐ์ดํ„ฐ๋“ค์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋ฅผ ์ •ํ•ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋‚˜์—ดํ•˜๋Š” ๊ฒƒ - ์ •๋ ฌ์€ ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ ์‹œ, ๋นˆ๋ฒˆํ•˜๊ฒŒ ํ•„์š” - ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๊ณ ์•ˆ๋˜์—ˆ์œผ๋ฉฐ, ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•™์Šต์˜ ํ•„์ˆ˜ ๋ฒ„๋ธ” ์ •๋ ฌ(bubble sort) : ๋‘ ์ธ์ ‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ตํ•ด์„œ, ์•ž์—์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋’ค์—์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ณด๋‹ค ํฌ๋ฉด ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟˆ def bubblesort(data): for index in range(len(data) - 1 ): swap = False for index2 in range(len(data) - index - 1): if data[index2] > data[index2 + 1]: data[index2], data[index2 + 1] = data[index2 + 1], data[index2] swap..

[Data Structure] ํž™ (Heap)์˜ ๊ตฌํ˜„ - ํž™๊ณผ ๋ฐฐ์—ด

ํž™๊ณผ ๋ฐฐ์—ด ์ผ๋ฐ˜์ ์œผ๋กœ ํž™ ๊ตฌํ˜„ ์‹œ, ๋ฐฐ์—ด ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค 0 ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์ง€๋งŒ, ํž™ ๊ตฌํ˜„์˜ ํŽธ์˜๋ฅผ ์œ„ํ•ด root ๋…ธ๋“œ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ 1๋กœ ์ง€์ • ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) = ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค(child node's index) //2 ์™ผ์ชฝ ์ž์‹ ๋…ธ๋“œ ์ธ๋ฑ์Šค(left child node's index) = ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) * 2 ์˜ค๋ฅธ์ชฝ ์ž์‹  ๋…ธ๋“œ ์ธ๋ฑ์Šค(right child node's index) = ๋ถ€๋ชจ ๋…ธ๋“œ ์ธ๋ฑ์Šค(parent node's index) * 2 + 1 ํž™๊ณผ ๋ฐ์ดํ„ฐ ์‚ฝ์ž…(Max Heap) class Heap: def __init__(self, data): self.heap_array = list() self.h..

[Data Structure] ํž™ (Heap)

ํž™ (Heap) ? ๋ฐ์ดํ„ฐ์—์„œ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ๋น ๋ฅด๊ฒŒ ์ฐพ๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋œ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ(Complete Binary Tree) - ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ : ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ๋•Œ, ์ตœํ•˜๋‹จ ์™ผ์ชฝ ๋…ธ๋“œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์‚ฝ์ž…ํ•˜๋Š” ํŠธ๋ฆฌ ํž™์„ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ  ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ , ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์œผ๋ ค๋ฉด O(n)์ด ๊ฑธ๋ฆผ

[Data Structure] ํŠธ๋ฆฌ (Tree)

ํŠธ๋ฆฌ (Tree) ๊ตฌ์กฐ Node์™€ Branch๋ฅผ ์ด์šฉํ•ด์„œ, ์‚ฌ์ดํด์„ ์ด๋ฃจ์ง€ ์•Š๋„๋ก ๊ตฌ์„ฑํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ์ด์ง„ ํŠธ๋ฆฌ(Binary Tree) ํ˜•ํƒœ์˜ ๊ตฌ์กฐ, ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์— ๋งŽ์ด ์‚ฌ์šฉ ํŠธ๋ฆฌ ๊ด€๋ จ ์šฉ์–ด Node : ํŠธ๋ฆฌ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๊ธฐ๋ณธ ์š”์†Œ(๋ฐ์ดํ„ฐ์™€ ๋‹ค๋ฅธ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์— ๋Œ€ํ•œ Branch ์ •๋ณด ํฌํ•จ) Root Node : ํŠธ๋ฆฌ ๋งจ ์œ„์— ์žˆ๋Š” ๋…ธ๋“œ Level : ์ตœ์ƒ์œ„ ๋…ธ๋“œ๋ฅผ Level 0 ์œผ๋กœ ํ–ˆ์„ ๋•Œ, ํ•˜์œ„ Branch๋กœ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ์˜ ๊นŠ์ด Parent Node : ์–ด๋–ค ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋ ˆ๋ฒจ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ Child Node : ์–ด๋–ค ๋…ธ๋“œ์˜ ์ƒ์œ„ ๋ ˆ๋ฒจ์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ Leaf Node(Terminal Node) : Child Node๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ๋…ธ๋“œ Sibling (Brother Node) : ๋™์ผ..

[Data Structure] hash() ํ•ด์‹œ ํ•จ์ˆ˜

SHA(Secure Hash Algorithm) - ์•ˆ์ „ํ•œ ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์–ด๋–ค ๋ฐ์ดํ„ฐ๋„ ์œ ์ผํ•œ ๊ณ ์ •๋œ ํฌ๊ธฐ์˜ ๊ณ ์ •๊ฐ’์„ ๋ฆฌํ„ด SHA-1 import hashlib data = 'test'.encode() hash_object = hashlib.sha1() hash_object.update(data) hex_dig = hash_object.hexdigest() print(hex_dig) #a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 SHA-256 import hashlib data = 'test'.encode() hash_object = hashlib.sha256() hash_object.update(data) hex_dig = hash_object.hexdigest() pr..

[Data Structure] ์ถฉ๋Œ(Collision) ํ•ด๊ฒฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜ : ํ•ด์‹œ ํ•จ์ˆ˜

# ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ๊ฐ€์žฅ ํฐ ๋ฌธ์ œ๋Š” ์ถฉ๋Œ(Collision) => Hash Collision 01. Chaining ๊ธฐ๋ฒ• ๊ฐœ๋ฐฉ ํ•ด์‹ฑ(Open Hashing) ๊ธฐ๋ฒ• ์ค‘ ํ•˜๋‚˜ -> ํ•ด์‹œ ํ…Œ์ด๋ธ”์˜ ์ €์žฅ๊ณต๊ฐ„ ์™ธ์˜ ๊ณต๊ฐ„์„ ํ™œ์šฉํ•˜๋Š” ๊ธฐ๋ฒ• ์ถฉ๋Œ ์‹œ, ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ๋’ค์— ์—ฐ๊ฒฐ์‹œ์ผœ ์ €์žฅํ•˜๋Š” ๊ธฐ๋ฒ• hash_table = list([0 for i in range(8)]) def get_key(data): return hash(data) def hash_function(key): return key % 8 def save_data(data, value): index_key = get_key(data) hash_address = hash_function(index_key) if hash_tab..

[Data Structure] ํ•ด์‹œ ํ…Œ์ด๋ธ” (Hash Table)

ํ•ด์‹œ ํ…Œ์ด๋ธ” (Hash Table) ? ํ‚ค(Key)์— ๋ฐ์ดํ„ฐ(Value)๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ Key๋ฅผ ํ†ตํ•ด ๋ฐ”๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ๋น ๋ฅธ ์†๋„ ํŒŒ์ด์ฌ์—์„œ๋Š” ๋”•์…”๋„ˆ๋ฆฌ(Dictionary) ํƒ€์ž…์œผ๋กœ ๊ตฌํ˜„ ๊ฐ€๋Šฅ ๋ณดํ†ต ๋ฐฐ์—ด๋กœ ๋ฏธ๋ฆฌ Hash Table ์‚ฌ์ด์ฆˆ๋งŒํผ ์ƒ์„ฑ ํ›„ ์‚ฌ์šฉ (๊ณต๊ฐ„๊ณผ ํƒ์ƒ‰ ์‹œ๊ฐ„ ๋งž๋ฐ”๊พธ๋Š” ๊ธฐ๋ฒ•) ๊ด€๋ จ ์šฉ์–ด ํ•ด์‹œ(Hash) : ์ž„์˜ ๊ฐ’์„ ๊ณ ์ • ๊ธธ์ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ฒƒ ํ•ด์‹œ ํ…Œ์ด๋ธ”(Hash Table) : ํ‚ค ๊ฐ’์˜ ์—ฐ์‚ฐ์— ์˜ํ•ด ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํ•ด์‹ฑ ํ•จ์ˆ˜(Hashing Function) : Key์— ๋Œ€ํ•ด ์‚ฐ์ˆ  ์—ฐ์‚ฐ์„ ์ด์šฉํ•ด, ๋ฐ์ดํ„ฐ ์œ„์น˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜ ํ•ด์‹œ ๊ฐ’(Hash Value), ํ•ด์‹œ ์ฃผ์†Œ(Hash Address) : Key๋ฅผ ํ•ด์‹ฑ ํ•จ์ˆ˜๋กœ ์—ฐ์‚ฐํ•ด์„œ, ํ•ด์‹œ ๊ฐ’์„ ์•Œ์•„๋‚ด..