diff options
Diffstat (limited to 'notebooks/sorting_algorithms.ipynb')
-rw-r--r-- | notebooks/sorting_algorithms.ipynb | 201 |
1 files changed, 201 insertions, 0 deletions
diff --git a/notebooks/sorting_algorithms.ipynb b/notebooks/sorting_algorithms.ipynb new file mode 100644 index 0000000..2e6326d --- /dev/null +++ b/notebooks/sorting_algorithms.ipynb @@ -0,0 +1,201 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "def generate_test_data(length: int, _range: tuple):\n", + " arr = []\n", + " for i in range(length):\n", + " arr.append(random.randint(int(_range[0]), int(_range[1])))\n", + " return arr\n", + "def binsearch(arr, item):\n", + " arr = sorted(arr)\n", + " print(arr)\n", + " ubound, lbound = len(arr) - 1, 0\n", + " found = False\n", + " while not found and lbound <= ubound:\n", + " index = (lbound + ubound) // 2\n", + " if arr[index] == item: \n", + " found = True\n", + " print(\"Found here\")\n", + " return found\n", + " if arr[index] > item:\n", + " ubound = index - 1\n", + " if arr[index] < item:\n", + " lbound = index + 1\n", + " print(\"not found\")\n", + " return found\n", + "\n", + "\n", + "def linear_search_while(arr: list, item):\n", + " found: bool = False\n", + " index = 0\n", + " while not found and index < len(arr):\n", + " if (arr[index] == item): \n", + " found = True\n", + " break\n", + " index += 1\n", + " return (found, index)\n", + "def linear_search(arr: list, item):\n", + " index = 0\n", + " for _item in arr:\n", + " if _item == item:\n", + " return (True, index) \n", + " index += 1\n", + " return False\n", + "def test_existsness(samples: int):\n", + " yes = 0\n", + " no = 0\n", + " for i in range(samples):\n", + " query = random.randint(0, 100)\n", + " array = generate_test_data(100, (0, 100))\n", + " found = linear_search(array, query)\n", + " if found:\n", + " yes += 1 \n", + " else:\n", + " no += 1\n", + " print(f\"In {samples} samples, {yes} match, {no} don't\")\n", + "def main():\n", + " narr = sorted([\"A\", \"B\", \"C\", \"D\", \"E\"])\n", + " print(\"Hello world\")\n", + " array = generate_test_data(10, (0, 100))\n", + " print(array)\n", + " query = int(input(\"Your query for item: \"))\n", + " qstring = input(\"String item: \")\n", + " binsearch(narr, query)\n", + " found1 = linear_search(array, query)\n", + " found2 = linear_search_while(array, query)\n", + " if found1[0]:\n", + " print(\"Found by for loop method \", found1[1]) \n", + " else: print(\"Not found.\")\n", + " if found2[0]:\n", + " print(\"Found by while loop method \", found2[1])\n", + " else: \n", + " print(\"Not found.\")\n", + " test_existsness(100)\n", + "if __name__ == \"__main__\":\n", + " main()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\n", + "100 checks passed!\n", + "['qibuvxrvcf', 'nmbxubyowk', 'ntszdjhhwf', 'xhjsoifdno', 'kbdwllyfmo', 'ndmngppihm', 'footfyjpfd', 'ridhoqmalu', 'nxlubshvcj', 'fjnnntfxur']\n", + "['fjnnntfxur', 'footfyjpfd', 'kbdwllyfmo', 'ndmngppihm', 'nmbxubyowk', 'ntszdjhhwf', 'nxlubshvcj', 'qibuvxrvcf', 'ridhoqmalu', 'xhjsoifdno']\n" + ] + } + ], + "source": [ + "import random\n", + "import string\n", + "CHARS = list(string.ascii_lowercase)\n", + "print(CHARS)\n", + "def generate_test_data(length: int, _range: tuple):\n", + " arr = []\n", + " for i in range(length):\n", + " arr.append(random.randint(_range[0], _range[1]))\n", + " return arr\n", + "def bubsort(arr: list):\n", + " for i in range(len(arr)):\n", + " # print(arr)\n", + " changed = False\n", + " for j in range(len(arr) - i - 1):\n", + " if arr[j] > arr[j+1]:\n", + " swp = arr[j+1]\n", + " arr[j+1] = arr[j]\n", + " arr[j] = swp\n", + " changed = True\n", + " if not changed: return arr \n", + "def testarr_string(length: int):\n", + " arr = []\n", + " for i in range(length):\n", + " string = \"\"\n", + " for i in range(10):\n", + " string += CHARS[random.randint(0,len(CHARS) - 1)]\n", + " arr.append(string)\n", + " print(arr)\n", + " return arr\n", + "def bubsort_reliability(samples: int):\n", + " y = 0\n", + " n = 0\n", + " for _ in range(samples):\n", + " test = generate_test_data(100, (0, 100))\n", + " if bubsort(test) == sorted(test): y += 1\n", + " else: n += 1\n", + " if y == samples: print(f\"{samples} checks passed!\")\n", + "bubsort_reliability(100)\n", + "print(bubsort(testarr_string(10)))" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "OH NO ITEM NOT FOUND IN LIST!!\n" + ] + } + ], + "source": [ + "import random\n", + "def generate_test_data(length: int, _range: tuple):\n", + " arr = []\n", + " for i in range(length):\n", + " arr.append(random.randint(_range[0], _range[1]))\n", + " return arr\n", + "def linsearch(myList: list, valueToFind: int):\n", + " mindex: int = len(myList) - 1\n", + " index: int = 0\n", + " found = False\n", + " while not found and index <= mindex:\n", + " if myList[index] == valueToFind:\n", + " found = True\n", + " index += 1\n", + " if found: print(\"VALUE FOUND!!!\")\n", + " else: print(\"OH NO ITEM NOT FOUND IN LIST!!\")\n", + "def main():\n", + " inputFind = int(input(\"Value to find: \"))\n", + " linsearch(generate_test_data(10, (0, 1000)), inputFind)\n", + "if __name__ == \"__main__\":\n", + " main()\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} |