From 2052f2c4b83ae93f8c24c6adea1be46ee1d571af Mon Sep 17 00:00:00 2001 From: KislyuninED Date: Thu, 14 May 2026 00:59:11 +0000 Subject: [PATCH] [1] add hash table with fixed bucket count --- KislyuninED/docks/data/1-st-exercize/main.py | 40 +++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/KislyuninED/docks/data/1-st-exercize/main.py b/KislyuninED/docks/data/1-st-exercize/main.py index d86f523..1b05cd1 100644 --- a/KislyuninED/docks/data/1-st-exercize/main.py +++ b/KislyuninED/docks/data/1-st-exercize/main.py @@ -43,4 +43,42 @@ def ll_list_all(head): records.append((curr['n'], curr['p'])) curr = curr['nxt'] records.sort(key=lambda x: x[0]) - return records \ No newline at end of file + return records + + + +# хеш-функция – сумма ord(name) % size +def _hash(name, size): + h = 0 + for ch in name: + h += ord(ch) + return h % size + +SIZE = 13 # фиксированный размер таблицы + +def ht_create(): + return [None] * SIZE + +def ht_insert(buckets, name, phone): + idx = _hash(name, len(buckets)) + buckets[idx] = ll_insert(buckets[idx], name, phone) + return buckets + +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) + return buckets + +def ht_list_all(buckets): + all_records = [] + for head in buckets: + curr = head + while curr: + all_records.append((curr['n'], curr['p'])) + curr = curr['nxt'] + all_records.sort(key=lambda x: x[0]) + return all_records \ No newline at end of file