diff options
Diffstat (limited to 'notebooks')
-rw-r--r-- | notebooks/data_structures.ipynb | 121 | ||||
-rw-r--r-- | notebooks/dictionaries.ipynb | 0 | ||||
-rw-r--r-- | notebooks/linked_lists.ipynb | 0 | ||||
-rw-r--r-- | notebooks/oop.ipynb | 0 | ||||
-rw-r--r-- | notebooks/recursion.ipynb | 158 | ||||
-rw-r--r-- | notebooks/sorting_algorithms.ipynb | 201 |
6 files changed, 480 insertions, 0 deletions
diff --git a/notebooks/data_structures.ipynb b/notebooks/data_structures.ipynb new file mode 100644 index 0000000..f116cf5 --- /dev/null +++ b/notebooks/data_structures.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ADT - Stack\n", + "Push and pop methods." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "stack = [None for index in range(0,10)]\n", + "basePointer = 0\n", + "topPointer = -1\n", + "stackFull = 10\n", + "\n", + "def push(item):\n", + " global stack, topPointer\n", + " topPointer += 1\n", + " if topPointer >= stackFull: \n", + " print(\"ERROR Cannot insert more.\")\n", + " return\n", + " stack[topPointer] = item\n", + " print(stack)\n", + "def pop():\n", + " global stack, topPointer\n", + " if topPointer < basePointer:\n", + " print(\"ERROR List is empty, cannot pop.\")\n", + " return\n", + " itemPopped = stack[topPointer]\n", + " print(f\"I popped this {itemPopped}\")\n", + " stack[topPointer] = None\n", + " topPointer -= 1\n", + " print(stack)\n", + "def test():\n", + " push(1)\n", + " push(2)\n", + " push(69)\n", + "\n", + " pop()\n", + " pop()\n", + "\n", + " print(stack)\n", + "\n", + " pop()\n", + " pop()\n", + "\n", + "test()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# ADT - Queue\n", + "Enqueue and dequeue methods." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "queue = [None for index in range(0, 10)]\n", + "frontPointer = 0\n", + "rearPointer = -1\n", + "queueLength = 0\n", + "queueFull = 10\n", + "def dequeue():\n", + " global queue, queueLength, queueFull, rearPointer, frontPointer\n", + " if queueLength == 0:\n", + " print(\"ERR:: Cannot dequeue an empty queue!\") \n", + " return\n", + " item = queue[frontPointer]\n", + " queue[frontPointer] = None\n", + " frontPointer += 1\n", + " print(f\"Dequeued element {item}\")\n", + " return\n", + " \n", + "def enqueue(item):\n", + " global queue, queueLength, queueFull, rearPointer\n", + " if queueLength < queueFull:\n", + " if rearPointer < len(queue) - 1:\n", + " rearPointer += 1\n", + " else:\n", + " rearPointer = 0\n", + " queueLength += 1\n", + " queue[rearPointer] = item\n", + " print(queue)\n", + " return\n", + " print(\"ERROR queue length exceeded!\")\n", + " return\n", + "for i in range(13):\n", + " enqueue(10 * i)\n", + "dequeue()\n", + "dequeue()\n", + "dequeue()\n", + "print(queue)\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "name": "python", + "version": "3.12.8" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/notebooks/dictionaries.ipynb b/notebooks/dictionaries.ipynb new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/notebooks/dictionaries.ipynb diff --git a/notebooks/linked_lists.ipynb b/notebooks/linked_lists.ipynb new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/notebooks/linked_lists.ipynb diff --git a/notebooks/oop.ipynb b/notebooks/oop.ipynb new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/notebooks/oop.ipynb diff --git a/notebooks/recursion.ipynb b/notebooks/recursion.ipynb new file mode 100644 index 0000000..9474acf --- /dev/null +++ b/notebooks/recursion.ipynb @@ -0,0 +1,158 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Recursion\n", + "Practices on recursion for Year 13 Computer Science." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Practice - outputs the binary representation of number recursively.\n", + "def x(n: int):\n", + " if ( n == 0 ) or ( n == 1 ):\n", + " print(n, end=\"\")\n", + " return\n", + " x(n // 2)\n", + " print(n % 2, end=\"\")\n", + "def rec_bin(n: int):\n", + " x(n)\n", + " print()\n", + "for i in range(0, 16):\n", + " rec_bin(i)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def fact(n: int): return n * fact(n-1) if n != 0 else 1\n", + "\n", + "def compound(p: float, r: float, y: int) -> float:\n", + " return p if y == 0 else compound(p*r, r, y-1)\n", + "\n", + "def print_fib(a, b, l):\n", + " print(a)\n", + " print(b)\n", + " print_next_fib(a, b, l-3)\n", + "\n", + "def print_next_fib(a: int, b: int, l: int):\n", + " if l == 0:\n", + " print(a+b)\n", + " return\n", + " print(a+b)\n", + " return print_next_fib(b, a+b, l-1)\n", + "\n", + "def fib_nth(n: int):\n", + " if n == 1:\n", + " return 0\n", + " if n == 2:\n", + " return 1\n", + " return fib_nth(n-1) + fib_nth(n-2)\n", + "\n", + "def draw_underline(l: int):\n", + " return '-'*l\n", + "\n", + "def test():\n", + " range_end = 15\n", + " print(f\"Printing series of factorials from 1 to {range_end}\\n{draw_underline(20)}\")\n", + " for i in range(1, range_end):\n", + " print(fact(i), end=\", \" if i < range_end-1 else \"\\n\")\n", + " fib_until = 15\n", + " print(f\"\\n\\nPrinting fibonacci sequence to {fib_until}\\n{draw_underline(20)}\")\n", + " print_fib(0, 1, fib_until)\n", + " print(fib_nth(5))\n", + " print(compound(36800*4, 1.06, 10) / (43129-3170*12))\n", + "def payback(tuition: int, rate: int, salary: int):\n", + " years = 0\n", + " remaining = tuition * 4\n", + "\n", + " while remaining > 0:\n", + " remaining -= salary\n", + " years += 1\n", + " remaining *= rate\n", + " print(f\"Year {years}: remaining: {remaining}\")\n", + " print(years)\n", + "\n", + "if __name__ == \"__main__\":\n", + " test()\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Object-oriented programming" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "class Student: \n", + " def __init__(self, name: str, dateOfBirth: str, examMark: int):\n", + " self.__name = name\n", + " self.__dateOfBirth = dateOfBirth\n", + " self.__examMark = examMark\n", + " def displayExamMark(self):\n", + " print(f\"{self.__name} got {self.__examMark} marks\")\n", + "\n", + "\n", + "class PartTimeStudent(Student):\n", + " def __init__(self, name: str, dateOfBirth: str, examMark: int):\n", + " self.__isFullTime = False\n", + " super().__init__(name, dateOfBirth, examMark)\n", + " def time(self):\n", + " return \"Full time\" if self.__isFullTime else \"Part time\"\n", + "\n", + "\n", + "class FullTimeStudent(Student):\n", + " def __init__(self, name: str, dateOfBirth: str, examMark: int):\n", + " self.__isFullTime = True\n", + " super().__init__(name, dateOfBirth, examMark)\n", + " def time(self):\n", + " return \"Full time\" if self.__isFullTime else \"Part time\"\n", + "\n", + "\n", + "myStudent = Student(\"Example Exampleperson\", \"1989-06-04\", 69)\n", + "myStudent.displayExamMark()\n", + "myFullStudent = FullTimeStudent(\"Dwayne Johnson\", \"1999-9-9\", 80)\n", + "myPartStudent = PartTimeStudent(\"Jake Paul\", \"1999-9-9\", 0)\n", + "myPartStudent.displayExamMark()\n", + "myFullStudent.displayExamMark()\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 +} 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 +} |