now with hash😎
This commit is contained in:
parent
191fc23b52
commit
d51f3fe51e
|
|
@ -58,4 +58,42 @@ def ll_list_all(head):
|
||||||
data_list.append({'name': current['name'], 'phone': current['phone']})
|
data_list.append({'name': current['name'], 'phone': current['phone']})
|
||||||
current = current['next']
|
current = current['next']
|
||||||
data_list.sort(key=lambda x: x['name'])
|
data_list.sort(key=lambda x: x['name'])
|
||||||
return data_list
|
return data_list
|
||||||
|
|
||||||
|
|
||||||
|
def hash_function(name, size):
|
||||||
|
return hash(name) % size
|
||||||
|
|
||||||
|
|
||||||
|
def ht_insert(buckets, name, phone):
|
||||||
|
index = hash_function(name, len(buckets))
|
||||||
|
head = buckets[index]
|
||||||
|
new_head = ll_insert(head, name, phone)
|
||||||
|
buckets[index] = new_head
|
||||||
|
return buckets
|
||||||
|
|
||||||
|
|
||||||
|
def ht_find(buckets, name):
|
||||||
|
index = hash_function(name, len(buckets))
|
||||||
|
head = buckets[index]
|
||||||
|
return ll_find(head, name)
|
||||||
|
|
||||||
|
|
||||||
|
def ht_delete(buckets, name):
|
||||||
|
index = hash_function(name, len(buckets))
|
||||||
|
head = buckets[index]
|
||||||
|
new_head = ll_delete(head, name)
|
||||||
|
buckets[index] = new_head
|
||||||
|
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 x: x[0])
|
||||||
|
return all_records
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user