From 66c2a4cfda032849639ee7c1db5034da5c92362c Mon Sep 17 00:00:00 2001 From: Sorokin Fedor Date: Mon, 18 May 2026 18:58:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=201:?= =?UTF-8?q?=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B2=D1=8B=D1=85=20=D0=B4=D0=B2=D1=83=D1=85=20=D0=BF?= =?UTF-8?q?=D1=83=D0=BD=D0=BA=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sorokinfi/427.md | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/sorokinfi/427.md b/sorokinfi/427.md index e69de29..64259e8 100644 --- a/sorokinfi/427.md +++ b/sorokinfi/427.md @@ -0,0 +1,92 @@ +import csv +import random +import sys +import time +from collections import defaultdict + +# увеличиваем лимит рекурсии +sys.setrecursionlimit(25000) + + +# 1. связный список, узел: {'name': 'Имя', 'phone': '123', 'next': None} + +# проходит до конца и добавляет в конец +def ll_insert(head, name, phone): + new_node = {'name': name, 'phone': phone, 'next': None} + if head is None: + return new_node + current = head + while current['next'] is not None: + current = current['next'] + current['next'] = new_node + return head + +# ищет узел, возвращает телефон или None +def ll_find(head, name): + current = head + while current is not None: + if current['name'] == name: + return current['phone'] + current = current['next'] + return None + +# удаляет узел, возвращает новую голову +def ll_delete(head, name): + if head is None: + return None + if head['name'] == name: + return head['next'] + + current = head + while current['next'] is not None: + if current['next']['name'] == name: + current['next'] = current['next']['next'] + return head + current = current['next'] + return head + +# собирает все записи в список и сортирует +def ll_list_all(head): + records = [] + current = head + while current is not None: + records.append((current['name'], current['phone'])) + current = current['next'] + records.sort(key=lambda x: x[0]) + return records + + +# 2. хеш-таблица + +# хеш-функция для вычисления бекета +def ht_hash(name, size): + return hash(name) % size + +# вычисляет индекс, вызывает ll_insert для соответствующего бакета +def ht_insert(buckets, name, phone): + size = len(buckets) + idx = ht_hash(name, size) + buckets[idx] = ll_insert(buckets[idx], name, phone) + +# поиск по хеш-таблице +def ht_find(buckets, name): + size = len(buckets) + idx = ht_hash(name, size) + return ll_find(buckets[idx], name) + +# удаление из хеш-таблицы +def ht_delete(buckets, name): + size = len(buckets) + idx = ht_hash(name, size) + buckets[idx] = ll_delete(buckets[idx], name) + +# собирает все записи из всех бакетов и сортирует +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 \ No newline at end of file