1
0
forked from UNN/2026-rff_mp
2026-rff_mp/KorotkinSE/task1/hashtable.py
2026-09-23 11:53:32 +03:00

39 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from linkedlist import Node, ll_insert, ll_find, ll_delete
def hash_function(name, size):
"""Полиномиальный хэш для уменьшения коллизий"""
h = 0
for char in name:
h = (h * 31 + ord(char)) % size
return h
def ht_create(size):
"""Создание массива корзин (buckets)"""
return [None] * size
def ht_insert(buckets, name, phone):
"""Вставка записи в соответствующую корзину"""
index = hash_function(name, len(buckets))
buckets[index] = ll_insert(buckets[index], name, phone)
def ht_find(buckets, name):
"""Поиск записи в корзине"""
index = hash_function(name, len(buckets))
return ll_find(buckets[index], name)
def ht_delete(buckets, name):
"""Удаление записи из корзины"""
index = hash_function(name, len(buckets))
buckets[index] = ll_delete(buckets[index], name)
def ht_list_all(buckets):
"""Сбор всех записей из всех корзин и сортировка по имени"""
records = []
for bucket in buckets:
current = bucket
while current is not None:
records.append((current.name, current.phone))
current = current.next
records.sort(key=lambda x: x[0])
return records