2026-rff_mp/romanovpv/task 1/hash_table.py
2026-05-03 23:26:35 +03:00

29 lines
822 B
Python

import linked_list as ll
def ht_create(size=100):
return [None] * size
def ht_get_hash(buckets, name):
return hash(name) % len(buckets)
def ht_insert(buckets, name, phone):
idx = ht_get_hash(buckets, name)
buckets[idx] = ll.ll_insert(buckets[idx], name, phone)
def ht_find(buckets, name):
idx = ht_get_hash(buckets, name)
return ll.ll_find(buckets[idx], name)
def ht_delete(buckets, name):
idx = ht_get_hash(buckets, name)
buckets[idx] = ll.ll_delete(buckets[idx], name)
def ht_list_all(buckets):
all_entries = []
for bucket in buckets:
if bucket:
current = bucket
while current:
all_entries.append((current['name'], current['phone']))
current = current['next']
return sorted(all_entries, key=lambda x: x[0])