From 8889f5420ff426b4c48d98539cedd376bc358406 Mon Sep 17 00:00:00 2001 From: IvanBud123 Date: Tue, 3 Mar 2026 22:21:22 +0300 Subject: [PATCH] [1] add hash func and tests of hash func --- BudakovIS/docs/LinkedListPhoneBook.py | 46 ++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/BudakovIS/docs/LinkedListPhoneBook.py b/BudakovIS/docs/LinkedListPhoneBook.py index 2e5284f..bab135a 100644 --- a/BudakovIS/docs/LinkedListPhoneBook.py +++ b/BudakovIS/docs/LinkedListPhoneBook.py @@ -155,5 +155,49 @@ print("====== TEST FIND HASH FUN ======") print("find by name Ivan: ",ht_find(buckets, "Ivan")) print("find by name Dima: ",ht_find(buckets, "Dima")) print("find by name Boris: ", ht_find(buckets, "Boris")) -print("====== END TEST ======") +print("====== END TEST ======\n\n\n") +def ht_list_all(buckets): + all_records = [] + for head in buckets: + current = head + while current is not None: + all_records.append((current['name'], current['phone'])) + current = current['next'] + all_records.sort(key=lambda x: x[0]) + return all_records + + +print("====== TEST FUNC LIST ALL ======") +print(ht_list_all(buckets)) +print("====== END TEST ======\n\n\n") + +def ht_delete(buckets, name): + index = hash_function(name, len(buckets)) + head = buckets[index] + new_head = ll_delete(head, name) + buckets[index] = new_head + + +print("====== GLOBAL TEST FOR HASH BASED FUN ======") +buckets = [None] * 10 + +ht_insert(buckets, "Ivan", "123-456") +print(buckets) +ht_insert(buckets, "Boris", "789-012") +print(buckets) +ht_insert(buckets, "Anna", "345-678") +print(buckets) +ht_insert(buckets, "Ivan", "111-222") # update +print(buckets) + +print("Find Ivan`s phone: ",ht_find(buckets, "Ivan")) # 111-222 +print("Find Petr`s phone: ",ht_find(buckets, "Petr")) # None + +# Удаляем +print("delite Boris from buckets") +ht_delete(buckets, "Boris") +print("search Boris = ",ht_find(buckets, "Boris")) # None + +# Все записи +print("list all records: ",ht_list_all(buckets))