From 247e9a12248ecaa9c9b904a128809e333bc90b5d Mon Sep 17 00:00:00 2001 From: 4eker <423785z@gmail.com> Date: Tue, 5 May 2026 22:20:24 +0300 Subject: [PATCH] Bag fix and add test for LinkedList --- pomelovsd/DataStruct/HashTable.py | 2 +- pomelovsd/DataStruct/LinkedList.py | 9 +- pomelovsd/DataStruct/data_structures.ipynb | 210 ++++++++++++++++++++- pomelovsd/HashTable.py | 0 4 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 pomelovsd/HashTable.py diff --git a/pomelovsd/DataStruct/HashTable.py b/pomelovsd/DataStruct/HashTable.py index e781068..85b268e 100644 --- a/pomelovsd/DataStruct/HashTable.py +++ b/pomelovsd/DataStruct/HashTable.py @@ -1,6 +1,6 @@ from LinkedList import ll_insert, ll_find, ll_delete, ll_list_all -def ht_create(size = 1000): +def create_ht(size = 1000): return[None] * size def hash_function(name, size = 1000): diff --git a/pomelovsd/DataStruct/LinkedList.py b/pomelovsd/DataStruct/LinkedList.py index 43ee319..7498576 100644 --- a/pomelovsd/DataStruct/LinkedList.py +++ b/pomelovsd/DataStruct/LinkedList.py @@ -10,7 +10,7 @@ def ll_insert(head, name, phone): return node # Случай если надо перезаписать имя - current = head + current = head while current: if current["name"] == name: current["phone"] = phone @@ -21,7 +21,8 @@ def ll_insert(head, name, phone): current = head while current["next"]: current = current["next"] - current["head"] = "node" + current["next"] = node + return head def ll_find(head, name): current = head @@ -29,7 +30,7 @@ def ll_find(head, name): if current["name"] == name: return current["phone"] current = current["next"] - return None + return "Нет данных" def ll_delete(head, name): # Случай для пустого списка @@ -42,7 +43,7 @@ def ll_delete(head, name): # Случай для поиска элемента current = head - while current: + while current["next"]: if current["next"]["name"] == name: current['next'] = current["next"]["next"] return head diff --git a/pomelovsd/DataStruct/data_structures.ipynb b/pomelovsd/DataStruct/data_structures.ipynb index ea7e458..94d6967 100644 --- a/pomelovsd/DataStruct/data_structures.ipynb +++ b/pomelovsd/DataStruct/data_structures.ipynb @@ -2,21 +2,217 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "id": "c533959c", "metadata": {}, + "outputs": [], + "source": [ + "import LinkedList as ll\n", + "import HashTable as ht\n", + "import BinaryTree as bt\n", + "import time \n", + "import random as rand\n", + "import csv\n", + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "id": "15cd6183", + "metadata": {}, + "source": [ + "## Данные для обработки" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "88611f78", + "metadata": {}, + "outputs": [], + "source": [ + "N = 10000\n", + "records_sorted = [(f\"User_{i:05d}\", f\"+7999{i:07d}\") for i in range(N)] \n", + "records_shuffled = records_sorted.copy()\n", + "rand.shuffle(records_shuffled) " + ] + }, + { + "cell_type": "markdown", + "id": "9fd1b8cd", + "metadata": {}, + "source": [ + "## Исследование для LinkedList " + ] + }, + { + "cell_type": "markdown", + "id": "083d49d0", + "metadata": {}, + "source": [ + "### Добавление всех элементов произвольного кортежа\n", + "- **data_ll_sh** - структура произвольных данных (только последний замер)\n", + "- **time_ll_insert_sh** - Замер времени работы 10000 элементов (5 замеров) " + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "11634fa4", + "metadata": {}, + "outputs": [], + "source": [ + "time_ll_insert_sh = [] \n", + "for n in range(5):\n", + " head = None\n", + " data_ll_sh = []\n", + " start = time.perf_counter()\n", + " for i in range(N):\n", + " head = ll.ll_insert(head, records_shuffled[i][0], records_shuffled[i][1])\n", + " data_ll_sh.append(head)\n", + " end = time.perf_counter()\n", + " time_ll_insert_sh.append(end - start)" + ] + }, + { + "cell_type": "markdown", + "id": "0a5f161e", + "metadata": {}, + "source": [ + "### Добавление всех элементов сортированного кортежа\n", + "- **data_ll_so** - Структура отсортированных данных (только последний замер)\n", + "- **time_ll_insert_so** - Замер времени работы 10000 элементов (5 замеров) " + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "9eab4641", + "metadata": {}, + "outputs": [], + "source": [ + "time_ll_insert_so = [] \n", + "for n in range(5):\n", + " head = None\n", + " data_ll_so = []\n", + " start = time.perf_counter()\n", + " for i in range(N):\n", + " head = ll.ll_insert(head, records_sorted[i][0], records_sorted[i][1])\n", + " data_ll_so.append(head)\n", + " end = time.perf_counter()\n", + " time_ll_insert_so.append(end - start)" + ] + }, + { + "cell_type": "markdown", + "id": "5862d31b", + "metadata": {}, + "source": [ + "### Поиск элементов в произвольном массиве\n", + "- **time_ll_find_sh** - Време поиска в произвольном массиве (для 5 замеров)\n", + "- **find_ll_sh** - массив найденных данных в произвольном массиве (только последний замер)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "aac6cd23", + "metadata": {}, + "outputs": [], + "source": [ + "time_ll_find_sh = []\n", + "for n in range(5):\n", + " find_ll_sh = []\n", + " start = time.perf_counter()\n", + " for m in range(100): # замер для 100 случайных узлов \n", + " i = rand.randint(0, N-1)\n", + " str_find = records_shuffled[i][0]\n", + " find_ll_sh.append(ll.ll_find(data_ll_sh[0], str_find))\n", + " for m in range(10): # недоступные данные\n", + " str_find = f\"Node_{m}\"\n", + " find_ll_sh.append(ll.ll_find(data_ll_sh[0], str_find))\n", + " end = time.perf_counter()\n", + " time_ll_find_sh.append(end - start)" + ] + }, + { + "cell_type": "markdown", + "id": "651aac23", + "metadata": {}, + "source": [ + "### Поиск элементов в отсортированном массиве\n", + "- **time_ll_find_so** - Време поиска в отсортированном массиве (для 5 замеров)\n", + "- **find_ll_so** - Массив найденных данных в отсортированном массиве (только последний замер)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "5e5ae537", + "metadata": {}, + "outputs": [], + "source": [ + "time_ll_find_so = []\n", + "for n in range(5):\n", + " find_ll_so = []\n", + " start = time.perf_counter()\n", + " for m in range(100): # замер для 100 случайных узлов \n", + " i = rand.randint(0, N-1)\n", + " str_find = records_sorted[i][0]\n", + " find_ll_so.append(ll.ll_find(data_ll_sh[0], str_find))\n", + " for m in range(10): # недоступные данные \n", + " str_find = f\"Node_{m}\"\n", + " find_ll_so.append(ll.ll_find(data_ll_sh[0], str_find))\n", + " end = time.perf_counter()\n", + " time_ll_find_so.append(end - start)" + ] + }, + { + "cell_type": "markdown", + "id": "a1f70be9", + "metadata": {}, + "source": [ + "### Удаление элементов в произвольном массиве\n", + "- **time_ll_delete_sh** - Време поиска в произвольном массиве (для 5 замеров)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6cdf8a70", + "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "a\n" + "ename": "KeyError", + "evalue": "9873", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[59], line 7\u001b[0m\n\u001b[1;32m 5\u001b[0m i \u001b[38;5;241m=\u001b[39m rand\u001b[38;5;241m.\u001b[39mrandint(\u001b[38;5;241m0\u001b[39m, N\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 6\u001b[0m str_delete \u001b[38;5;241m=\u001b[39m records_shuffled[i][\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m----> 7\u001b[0m data_ll_sh \u001b[38;5;241m=\u001b[39m ll\u001b[38;5;241m.\u001b[39mll_delete(\u001b[43mdata_ll_sh\u001b[49m\u001b[43m[\u001b[49m\u001b[43mi\u001b[49m\u001b[43m]\u001b[49m[\u001b[38;5;241m2\u001b[39m], str_delete)\n\u001b[1;32m 8\u001b[0m end \u001b[38;5;241m=\u001b[39m time\u001b[38;5;241m.\u001b[39mperf_counter()\n\u001b[1;32m 9\u001b[0m time_ll_delete_sh\u001b[38;5;241m.\u001b[39mappend(end \u001b[38;5;241m-\u001b[39m start)\n", + "\u001b[0;31mKeyError\u001b[0m: 9873" ] } ], "source": [ - "import LinkedList\n", - "import HashTable\n" + "time_ll_delete_sh = []\n", + "for n in range(5):\n", + " start = time.perf_counter()\n", + " for m in range(50): \n", + " i = rand.randint(0, N-1)\n", + " str_delete = records_shuffled[i][0]\n", + " data_ll_sh = ll.ll_delete(data_ll_sh[i][2], str_delete)\n", + " end = time.perf_counter()\n", + " time_ll_delete_sh.append(end - start)" + ] + }, + { + "cell_type": "markdown", + "id": "8d6156e9", + "metadata": {}, + "source": [ + "### Удаление элементов в произвольном массиве\n", + "- **time_ll_delete_sh** - Време поиска в произвольном массиве (для 5 замеров)" ] } ], diff --git a/pomelovsd/HashTable.py b/pomelovsd/HashTable.py new file mode 100644 index 0000000..e69de29