1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
}
|