diff --git a/lomakinae/docs/data/01/src/ht.py b/lomakinae/docs/data/01/src/ht.py new file mode 100644 index 0000000..03247b4 --- /dev/null +++ b/lomakinae/docs/data/01/src/ht.py @@ -0,0 +1,34 @@ +from ll import ll_insert, ll_find, ll_delete, ll_list_all + + +DEFAULT_SIZE = 128 + + +def ht_new(size=DEFAULT_SIZE): + return [None] * size + + +def _ht_index(buckets, name): + return hash(name) % len(buckets) + + +def ht_insert(buckets, name, phone): + i = _ht_index(buckets, name) + buckets[i] = ll_insert(buckets[i], name, phone) + + +def ht_find(buckets, name): + i = _ht_index(buckets, name) + return ll_find(buckets[i], name) + + +def ht_delete(buckets, name): + i = _ht_index(buckets, name) + buckets[i] = ll_delete(buckets[i], name) + + +def ht_list_all(buckets): + records = [] + for head in buckets: + records.extend(ll_list_all(head)) + return sorted(records) \ No newline at end of file