From 798e9ae052ff07195f4524e215d819b9ef8bfa62 Mon Sep 17 00:00:00 2001 From: konnovaea Date: Sun, 19 Apr 2026 20:19:23 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=85=D0=B5=D1=88-=D1=82=D0=B0=D0=B1=D0=BB?= =?UTF-8?q?=D0=B8=D1=86=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- konnovaea/phonebook.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/konnovaea/phonebook.py b/konnovaea/phonebook.py index 2064305..46461f9 100644 --- a/konnovaea/phonebook.py +++ b/konnovaea/phonebook.py @@ -29,6 +29,7 @@ def ll_delete(head, name): return head current = current['next'] return head + def ll_list_all(head): records = [] current = head @@ -38,3 +39,37 @@ def ll_list_all(head): current = current['next'] records.sort(key=lambda x: x[0]) return records + +def hash_function(name, table_size): + total = 0 + for ch in name: + total = (total*31 + ord(ch)) % table_size + return total + +def ht_create(size=1000): + return [None]*size + +def ht_insert(buckets, name, phone): + idx = hash_function(name, len(buckets)) + buckets[idx] = ll_insert(buckets[idx], name, phone) + return buckets + +def ht_find(buckets, name): + idx = hash_function(name, len(buckets)) + return ll_find(buckets[idx], name) + +def ht_delete(buckets, name): + idx = hash_function(name, len(buckets)) + buckets[idx] = ll_delete(buckets[idx], name) + return buckets + +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 +