diff options
author | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-02-19 09:43:52 +0200 |
---|---|---|
committer | Zhongheng Liu <z.liu@outlook.com.gr> | 2025-02-19 09:43:52 +0200 |
commit | 8834da60a01294fd509c7cebf3b129fcc378d152 (patch) | |
tree | a1c6c4bd71e95780f87d35240754c5b54d3042ae /notebooks/recursion.ipynb | |
parent | 34bd7099d27656b4454015b0c410ca1713db5271 (diff) | |
download | cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.tar.gz cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.tar.bz2 cs-y13-8834da60a01294fd509c7cebf3b129fcc378d152.zip |
chore: changing to ipynb
Diffstat (limited to 'notebooks/recursion.ipynb')
-rw-r--r-- | notebooks/recursion.ipynb | 158 |
1 files changed, 158 insertions, 0 deletions
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 +} |