From edcc503f469e1356fff8984c6f4caf513cf642fe Mon Sep 17 00:00:00 2001 From: IvanBud123 Date: Tue, 3 Mar 2026 19:55:43 +0300 Subject: [PATCH] [1] modyfi LinkedListBook for 2-nd exersize --- BudakovIS/docs/LinkedListPhoneBook.py | 55 +++++++++++++++++++++++++++ BudakovIS/docs/hash.py | 3 -- 2 files changed, 55 insertions(+), 3 deletions(-) delete mode 100644 BudakovIS/docs/hash.py diff --git a/BudakovIS/docs/LinkedListPhoneBook.py b/BudakovIS/docs/LinkedListPhoneBook.py index d7529fc..e3e849f 100644 --- a/BudakovIS/docs/LinkedListPhoneBook.py +++ b/BudakovIS/docs/LinkedListPhoneBook.py @@ -117,3 +117,58 @@ print("====== TESTING ll_list_all FUNC ======") print(ll_list_all(head)) print("====== END ======") + + + +# 2.hash tables + + +SIZE = 1000 +buckets = [None] * SIZE + +def hash_func(name): + return hash(name) % SIZE + +def ht_insert(buckets, name, phone): + idx = hash_func(name) + buckets[idx] = ll_insert(buckets[idx], name, phone) + return buckets + +def ht_find(buckets, name): + idx = hash_func(name) + return ll_find(buckets[idx], name) + + +def ht_delete(buckets, name): + idx = hash_func(name) + buckets[idx] = ll_delete(buckets[idx], name) + return buckets + +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 pair: pair[0]) + return all_records + + + +print("---- TESTING BUCKETS ----") + +buckets = ht_insert(buckets, 'Alice', '111-111') +buckets = ht_insert(buckets, 'Bob', '222-222') +buckets = ht_insert(buckets, 'Charlie', '333-333') +buckets = ht_insert(buckets, 'Alice', '999-999') + +print(ht_find(buckets, 'Bob')) + +print(ht_find(buckets, 'David')) + +buckets = ht_delete(buckets, 'Bob') + +print(ht_find(buckets, 'Bob')) + +print(ht_list_all(buckets)) diff --git a/BudakovIS/docs/hash.py b/BudakovIS/docs/hash.py deleted file mode 100644 index 6ad2222..0000000 --- a/BudakovIS/docs/hash.py +++ /dev/null @@ -1,3 +0,0 @@ -SIZE = 1000 -buckets = [None] * SIZE -