2026-rff_mp/volkovim/task1/structures/HashTable.py

52 lines
1.1 KiB
Python
Raw Normal View History

2026-04-30 13:20:23 +00:00
from structures.LinkedList import *
def hash_func(name: str, size: int) -> int:
total = 0
for i, ch in enumerate(name):
total += ord(ch) * (i + 1)
return total % size
def ht_insert(buckets: list | None, name: str, phone: str, size: int = 50) -> list:
if buckets is None:
buckets = [None] * size
index = hash_func(name, len(buckets))
buckets[index] = ll_insert(buckets[index], name, phone)
return buckets
def ht_find(buckets: list | None, name: str) -> str | None:
if not buckets:
return None
index = hash_func(name, len(buckets))
return ll_find(buckets[index], name)
def ht_delete(buckets: list | None, name: str) -> list | None:
if not buckets:
return buckets
index = hash_func(name, len(buckets))
buckets[index] = ll_delete(buckets[index], name)
return buckets
def ht_list_all(buckets: list | None) -> list:
if not buckets:
return []
result = []
for bucket in buckets:
if bucket is not None:
result.extend(ll_list_all(bucket))
return sorted(result, key=lambda x: x[0])