Skip to content

Commit 96dbf37

Browse files
committed
Add file with random exercises
1 parent 1fef2ed commit 96dbf37

1 file changed

Lines changed: 159 additions & 0 deletions

File tree

source_code/exercises.ipynb

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "7ea52ac8-def9-4fa6-ad62-437fb876c85b",
6+
"metadata": {},
7+
"source": [
8+
"Some random programming exercises."
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "2eea84dc-48b1-4582-835f-ac084837f56d",
14+
"metadata": {},
15+
"source": [
16+
"## Contains digit\n",
17+
"\n",
18+
"Write a function that returns `True` if a given number contains a given digit, `False` otherwise."
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 1,
24+
"id": "b3a54ce5-354e-4525-aa1e-f6fcb85f7fc1",
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"def has_digit(n, digit):\n",
29+
" while n > 0:\n",
30+
" if n % 10 == digit:\n",
31+
" return True\n",
32+
" n //= 10\n",
33+
" return False"
34+
]
35+
},
36+
{
37+
"cell_type": "markdown",
38+
"id": "2823f917-cd56-4e0f-bbaf-69bc6c59b339",
39+
"metadata": {},
40+
"source": [
41+
"Test the implmentation, taking edge cases into account."
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 6,
47+
"id": "711c7090-69b5-42fe-b986-dc498f2ee6a3",
48+
"metadata": {},
49+
"outputs": [
50+
{
51+
"name": "stdout",
52+
"output_type": "stream",
53+
"text": [
54+
"34901 has digit 0\n",
55+
"34901 has digit 1\n",
56+
"no 2 in 34901\n",
57+
"34901 has digit 3\n",
58+
"34901 has digit 4\n",
59+
"no 5 in 34901\n",
60+
"no 6 in 34901\n",
61+
"no 7 in 34901\n",
62+
"no 8 in 34901\n",
63+
"34901 has digit 9\n"
64+
]
65+
}
66+
],
67+
"source": [
68+
"n = 34901\n",
69+
"for digit in range(10):\n",
70+
" if has_digit(n, digit):\n",
71+
" print(f'{n} has digit {digit}')\n",
72+
" else:\n",
73+
" print(f'no {digit} in {n}')"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 8,
79+
"id": "e6d1d4c6-480c-43ff-a422-87dd7090a205",
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"name": "stdout",
84+
"output_type": "stream",
85+
"text": [
86+
"no 0 in 3491\n",
87+
"3491 has digit 1\n",
88+
"no 2 in 3491\n",
89+
"3491 has digit 3\n",
90+
"3491 has digit 4\n",
91+
"no 5 in 3491\n",
92+
"no 6 in 3491\n",
93+
"no 7 in 3491\n",
94+
"no 8 in 3491\n",
95+
"3491 has digit 9\n"
96+
]
97+
}
98+
],
99+
"source": [
100+
"n = 3491\n",
101+
"for digit in range(10):\n",
102+
" if has_digit(n, digit):\n",
103+
" print(f'{n} has digit {digit}')\n",
104+
" else:\n",
105+
" print(f'no {digit} in {n}')"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": 7,
111+
"id": "92538c32-190b-4e6f-bd96-6a4fef1ce54c",
112+
"metadata": {},
113+
"outputs": [
114+
{
115+
"data": {
116+
"text/plain": [
117+
"True"
118+
]
119+
},
120+
"execution_count": 7,
121+
"metadata": {},
122+
"output_type": "execute_result"
123+
}
124+
],
125+
"source": [
126+
"has_digit(210, 0)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"id": "259a311a-edfc-4c82-9239-5162c797df72",
133+
"metadata": {},
134+
"outputs": [],
135+
"source": []
136+
}
137+
],
138+
"metadata": {
139+
"kernelspec": {
140+
"display_name": "Python 3 (ipykernel)",
141+
"language": "python",
142+
"name": "python3"
143+
},
144+
"language_info": {
145+
"codemirror_mode": {
146+
"name": "ipython",
147+
"version": 3
148+
},
149+
"file_extension": ".py",
150+
"mimetype": "text/x-python",
151+
"name": "python",
152+
"nbconvert_exporter": "python",
153+
"pygments_lexer": "ipython3",
154+
"version": "3.10.4"
155+
}
156+
},
157+
"nbformat": 4,
158+
"nbformat_minor": 5
159+
}

0 commit comments

Comments
 (0)