
hash_table_code = ''


from linked_list import ll_insert, ll_find, ll_delete, ll_list_all


def ht_hash(name, size):
    
    h = 0
    for char in name:
        h = (h * 31 + ord(char)) % size
    return h


def ht_insert(buckets, name, phone):
    
    size = len(buckets)
    index = ht_hash(name, size)
    buckets[index] = ll_insert(buckets[index], name, phone)
    return buckets


def ht_find(buckets, name):
    
    size = len(buckets)
    index = ht_hash(name, size)
    return ll_find(buckets[index], name)


def ht_delete(buckets, name):
    
    size = len(buckets)
    index = ht_hash(name, size)
    buckets[index] = ll_delete(buckets[index], name)
    return buckets


def ht_list_all(buckets):
   
    result = []
    for bucket in buckets:
        current = bucket
        while current is not None:
            result.append((current['name'], current['phone']))
            current = current['next']
    result.sort(key=lambda x: x[0])
    return result


with open('/mnt/agents/output/lab1/src/hash_table.py', 'w', encoding='utf-8') as f:
    f.write(hash_table_code)

print("✅ hash_table.py создан")
