[1] FINISH 1-st-exercise #148
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user