[1] FINISH 1-st-exercise #148

Merged
git_admin merged 23 commits from IvanBud123/2026-rff_mp:1-st-exercise into develop 2026-03-15 06:42:24 +00:00
2 changed files with 55 additions and 3 deletions
Showing only changes of commit 0e84534109 - Show all commits

View File

@ -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))

View File

@ -1,3 +0,0 @@
SIZE = 1000
buckets = [None] * SIZE