2026-rff_mp/BrychkinKA/docs/source/hash_table.py
2026-05-25 14:37:27 +03:00

27 lines
733 B
Python

from linked_list import ll_insert, ll_find, ll_delete, ll_list_all
def make_table(size=1000):
return [None] * size
def _hash(name, size):
return sum(ord(c) for c in name) % size
def ht_insert(buckets, name, phone):
idx = _hash(name, len(buckets))
buckets[idx] = ll_insert(buckets[idx], name, phone)
def ht_find(buckets, name):
idx = _hash(name, len(buckets))
return ll_find(buckets[idx], name)
def ht_delete(buckets, name):
idx = _hash(name, len(buckets))
buckets[idx] = ll_delete(buckets[idx], name)
def ht_list_all(buckets):
result = []
for head in buckets:
if head is not None:
result.extend(ll_list_all(head))
result.sort(key=lambda x: x[0])
return result