U7
This commit is contained in:
parent
6460db51cc
commit
f208885163
@ -26,7 +26,7 @@ class Garden:
|
|||||||
len_top = len(top)
|
len_top = len(top)
|
||||||
len_bot = len(bot)
|
len_bot = len(bot)
|
||||||
for i in range(0, len_top, 2):
|
for i in range(0, len_top, 2):
|
||||||
top_part = top[i:i + 2] #беремо звеху 2(не індекс а к-сть елементів) і знизу 2
|
top_part = top[i:i + 2] #беремо звеху 2(не індекс а к-сть елементів) і знизу 2 якшо б хотіли 4 матрицю то треба добавити ше 2 рядка але загалом вся логіка буде така сама
|
||||||
bot_part = bot[i:i + 2]
|
bot_part = bot[i:i + 2]
|
||||||
cup = top_part + bot_part
|
cup = top_part + bot_part
|
||||||
cups.append(cup)
|
cups.append(cup)
|
||||||
|
|||||||
74
u7/hash_table.py
Normal file
74
u7/hash_table.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
"""Hash tabuľka s lineárnym sondovaním a reťazcovými kľúčmi."""
|
||||||
|
from unittest import result
|
||||||
|
|
||||||
|
|
||||||
|
class HashTable:
|
||||||
|
def __init__(self, capacity=10):
|
||||||
|
"""Vytvor prázdnu tabuľku s kladnou celočíselnou kapacitou."""
|
||||||
|
self.empty = object()
|
||||||
|
self.deleted = object()
|
||||||
|
self.slots = [self.empty] * capacity
|
||||||
|
self._capacity = capacity
|
||||||
|
self.size = 0
|
||||||
|
self.table = []
|
||||||
|
for i in range(capacity):
|
||||||
|
self.table.append([])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def capacity(self):
|
||||||
|
"""Vráť aktuálny počet pozícií v poli."""
|
||||||
|
return self._capacity
|
||||||
|
|
||||||
|
def hash(self, key):
|
||||||
|
"""Vráť polynomiálny hash kľúča modulo aktuálna kapacita."""
|
||||||
|
result = 0
|
||||||
|
for char in str(key):
|
||||||
|
result = result * 31 + ord(char)
|
||||||
|
return result % self.capacity
|
||||||
|
|
||||||
|
def probe(self, key):
|
||||||
|
"""Vráť index kľúča alebo miesta na vloženie bez zmeny tabuľky."""
|
||||||
|
i = 0
|
||||||
|
while i < self.capacity:
|
||||||
|
index = (self.hash(key) + i) % self.capacity
|
||||||
|
if self.table[index] is self.empty:
|
||||||
|
return index
|
||||||
|
elif self.table[index] is not self.deleted and self.table[index][0] == key:
|
||||||
|
return index
|
||||||
|
i += 1
|
||||||
|
raise KeyError(key)
|
||||||
|
|
||||||
|
def load_factor(self):
|
||||||
|
"""Vráť podiel počtu aktívnych položiek a kapacity."""
|
||||||
|
return self.size / self.capacity if self._capacity > 0 else 0
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
index = self.probe(key)
|
||||||
|
return self.table[index][1]
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
index = self.probe(key)
|
||||||
|
if self.table[index] is self.empty:
|
||||||
|
self.size += 1
|
||||||
|
self.table[index] = (key, value)
|
||||||
|
|
||||||
|
def __delitem__(self, key):
|
||||||
|
index = self.probe(key)
|
||||||
|
if self.table[index] is not self.empty:
|
||||||
|
self.table[index] = self.deleted
|
||||||
|
self.size -= 1
|
||||||
|
|
||||||
|
def __contains__(self, key):
|
||||||
|
try:
|
||||||
|
self.probe(key)
|
||||||
|
return True
|
||||||
|
except KeyError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return self.size
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
for bucket in self.table:
|
||||||
|
if bucket is not self.empty:
|
||||||
|
yield bucket[0]
|
||||||
Loading…
Reference in New Issue
Block a user