{ "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 }