forked from UNN/2026-rff_mp
[1] modyfi LinkedListBook for 2-nd exersize
This commit is contained in:
parent
057c7188bd
commit
edcc503f46
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -1,3 +0,0 @@
|
|||
SIZE = 1000
|
||||
buckets = [None] * SIZE
|
||||
|
||||
Loading…
Reference in New Issue
Block a user