forked from UNN/2026-rff_mp
[1] хэш-таблица
This commit is contained in:
parent
054035deac
commit
673c18cfaf
|
|
@ -64,6 +64,46 @@ def ll_list_all(head: Optional[Dict]) -> List[Tuple[str, str]]:
|
||||||
while current is not None:
|
while current is not None:
|
||||||
records.append((current['name'], current['phone']))
|
records.append((current['name'], current['phone']))
|
||||||
current = current['next']
|
current = current['next']
|
||||||
|
def hash_function(name: str, size: int) -> int:
|
||||||
|
"""Простая хеш-функция"""
|
||||||
|
return sum(ord(c) for c in name) % size
|
||||||
|
|
||||||
|
|
||||||
|
def ht_create(size: int = 1000) -> List[Optional[Dict]]:
|
||||||
|
"""Создание хеш-таблицы"""
|
||||||
|
return [None] * size
|
||||||
|
|
||||||
|
|
||||||
|
def ht_insert(buckets: List[Optional[Dict]], name: str, phone: str) -> None:
|
||||||
|
"""Вставка в хеш-таблицу"""
|
||||||
|
index = hash_function(name, len(buckets))
|
||||||
|
buckets[index] = ll_insert(buckets[index], name, phone)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_find(buckets: List[Optional[Dict]], name: str) -> Optional[str]:
|
||||||
|
"""Поиск в хеш-таблице"""
|
||||||
|
index = hash_function(name, len(buckets))
|
||||||
|
return ll_find(buckets[index], name)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_delete(buckets: List[Optional[Dict]], name: str) -> None:
|
||||||
|
"""Удаление из хеш-таблицы"""
|
||||||
|
index = hash_function(name, len(buckets))
|
||||||
|
buckets[index] = ll_delete(buckets[index], name)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_list_all(buckets: List[Optional[Dict]]) -> List[Tuple[str, str]]:
|
||||||
|
"""Сбор всех записей из хеш-таблицы с сортировкой"""
|
||||||
|
records = []
|
||||||
|
for head in buckets:
|
||||||
|
current = head
|
||||||
|
while current is not None:
|
||||||
|
records.append((current['name'], current['phone']))
|
||||||
|
current = current['next']
|
||||||
|
|
||||||
|
records.sort(key=lambda x: x[0])
|
||||||
|
return records
|
||||||
|
|
||||||
|
|
||||||
records.sort(key=lambda x: x[0])
|
records.sort(key=lambda x: x[0])
|
||||||
return records
|
return records
|
||||||
Loading…
Reference in New Issue
Block a user