diff --git a/romanovpv/task 1/hash_table.py b/romanovpv/task 1/hash_table.py index 3618220..ef452a3 100644 --- a/romanovpv/task 1/hash_table.py +++ b/romanovpv/task 1/hash_table.py @@ -1,24 +1,29 @@ import linked_list as ll -def ht_create(size = 100): - return[None] * size +def ht_create(size=100): + return [None] * size -def _hash(name, size): - return hash(name) % size +def ht_get_hash(buckets, name): + return hash(name) % len(buckets) def ht_insert(buckets, name, phone): - index = _hash(name, len(buckets)) - buckets[index] = ll.ll_insert(buckets[index], name, phone) - return buckets + idx = ht_get_hash(buckets, name) + buckets[idx] = ll.ll_insert(buckets[idx], name, phone) def ht_find(buckets, name): - index = hash(name, len(buckets)) - return ll.ll_find(buckets[index], name) - + idx = ht_get_hash(buckets, name) + return ll.ll_find(buckets[idx], name) + def ht_delete(buckets, name): - index = _hash(name, len(buckets)) - buckets[index] = ll.ll_delete(buckets[index], name) - return buckets + idx = ht_get_hash(buckets, name) + buckets[idx] = ll.ll_delete(buckets[idx], name) def ht_list_all(buckets): - pass \ No newline at end of file + all_entries = [] + for bucket in buckets: + if bucket: + current = bucket + while current: + all_entries.append((current['name'], current['phone'])) + current = current['next'] + return sorted(all_entries, key=lambda x: x[0]) \ No newline at end of file