Skip to content

Commit 539f96d

Browse files
committed
Created using Colab
1 parent 859b5de commit 539f96d

File tree

1 file changed

+295
-0
lines changed

1 file changed

+295
-0
lines changed

quickbook.ipynb

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "view-in-github",
7+
"colab_type": "text"
8+
},
9+
"source": [
10+
"<a href=\"https://colab.research.google.com/github/ratulb/mojo_programming/blob/main/quickbook.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"source": [
16+
"**Mojo quickbook**"
17+
],
18+
"metadata": {
19+
"id": "Gh82E_KBDg3z"
20+
}
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": null,
25+
"metadata": {
26+
"id": "buOgxm25ONit"
27+
},
28+
"outputs": [],
29+
"source": [
30+
"!curl -ssL https://magic.modular.com/ | bash"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {
37+
"id": "FVZvyhRiONiw"
38+
},
39+
"outputs": [],
40+
"source": [
41+
"import os\n",
42+
"os.environ['PATH'] +=':/root/.modular/bin'"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {
49+
"id": "TqFD0EK0ONiw"
50+
},
51+
"outputs": [],
52+
"source": [
53+
"!magic init codes --format mojoproject"
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {
60+
"id": "k3Ddb6GcONiw"
61+
},
62+
"outputs": [],
63+
"source": [
64+
"%cd codes/"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {
71+
"id": "IaxB1auxONix",
72+
"colab": {
73+
"base_uri": "https://localhost:8080/"
74+
},
75+
"outputId": "1c86b6bb-c58c-4b9e-88e1-6e3c071c49cd"
76+
},
77+
"outputs": [
78+
{
79+
"output_type": "stream",
80+
"name": "stdout",
81+
"text": [
82+
"Overwriting find_first_last.mojo\n"
83+
]
84+
}
85+
],
86+
"source": [
87+
"%%writefile find_first_last.mojo\n",
88+
"\n",
89+
"### Find First/Last\n",
90+
"### Find first and last index of a target value in a sorted array\n",
91+
"\n",
92+
"fn find_first_last(arr: List[Int], target: Int) -> (Int, Int):\n",
93+
" result = (-1, -1)\n",
94+
" if len(arr) == 0:\n",
95+
" return result\n",
96+
" left, right = 0, len(arr) -1\n",
97+
"\n",
98+
" while left <= right:\n",
99+
" mid = (left + right) // 2\n",
100+
" if arr[mid] == target:\n",
101+
" result[1] = mid\n",
102+
" left = mid + 1\n",
103+
" elif arr[mid] > target:\n",
104+
" right = mid - 1\n",
105+
" else:\n",
106+
" left = mid + 1\n",
107+
" left, right = 0, result[1] # result[1] -1 would keep left index at -1 for single occurence of target\n",
108+
"\n",
109+
" while left <= right:\n",
110+
" mid = (left + right) // 2\n",
111+
" if arr[mid] == target:\n",
112+
" result[0] = mid\n",
113+
" right = mid - 1\n",
114+
" elif arr[mid] > target:\n",
115+
" right = mid - 1\n",
116+
" else:\n",
117+
" left = mid + 1\n",
118+
" return result\n",
119+
"\n",
120+
"from testing import assert_true\n",
121+
"\n",
122+
"fn main() raises:\n",
123+
" arr = List(5,7,7,8,8,10)\n",
124+
" target = 8\n",
125+
" result = find_first_last(arr, target)\n",
126+
" assert_true(result[0] == 3 and result[1] == 4, \"Assertion failed\")\n",
127+
" target = 6\n",
128+
" result = find_first_last(arr, target)\n",
129+
" assert_true(result[0] == -1 and result[1] == -1, \"Assertion failed\")\n",
130+
"\n",
131+
" arr = List(5,7,7,8,10)\n",
132+
" target = 8\n",
133+
" result = find_first_last(arr, target)\n",
134+
" assert_true(result[0] == 3 and result[1] == 3, \"Assertion failed\")\n",
135+
"\n"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"metadata": {
142+
"colab": {
143+
"base_uri": "https://localhost:8080/"
144+
},
145+
"id": "h2k9wkDaONiz",
146+
"outputId": "c374f027-c4ce-454f-ca08-1f4f9c2bf141"
147+
},
148+
"outputs": [
149+
{
150+
"output_type": "stream",
151+
"name": "stdout",
152+
"text": [
153+
"\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K3 3\n"
154+
]
155+
}
156+
],
157+
"source": [
158+
"!magic run mojo find_first_last.mojo"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": null,
164+
"metadata": {
165+
"colab": {
166+
"base_uri": "https://localhost:8080/"
167+
},
168+
"id": "bSglX7bNONi0",
169+
"outputId": "77b66492-8da2-483e-a074-c99d2fca3a79"
170+
},
171+
"outputs": [
172+
{
173+
"output_type": "stream",
174+
"name": "stdout",
175+
"text": [
176+
"\u001b[32m⠁\u001b[0m \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[32m⠁\u001b[0m activating environment \r\u001b[2K\u001b[1mreformatted find_first_last.mojo\u001b[0m\n",
177+
"\n",
178+
"\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n",
179+
"\u001b[34m\u001b[1m1 file \u001b[0m\u001b[1mreformatted\u001b[0m.\n"
180+
]
181+
}
182+
],
183+
"source": [
184+
"!magic run mojo format find_first_last.mojo"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"source": [
190+
"!cat find_first_last.mojo"
191+
],
192+
"metadata": {
193+
"colab": {
194+
"base_uri": "https://localhost:8080/"
195+
},
196+
"id": "z2OxrAfbJYKc",
197+
"outputId": "10fd7ac4-728c-4acc-c4c8-8fb863464700"
198+
},
199+
"execution_count": null,
200+
"outputs": [
201+
{
202+
"output_type": "stream",
203+
"name": "stdout",
204+
"text": [
205+
"### Find First/Last\n",
206+
"### Find first and last index of a target value in a sorted array\n",
207+
"\n",
208+
"\n",
209+
"fn find_first_last(arr: List[Int], target: Int) -> (Int, Int):\n",
210+
" result = (-1, -1)\n",
211+
" if len(arr) == 0:\n",
212+
" return result\n",
213+
" left, right = 0, len(arr) - 1\n",
214+
"\n",
215+
" while left <= right:\n",
216+
" mid = (left + right) // 2\n",
217+
" if arr[mid] == target:\n",
218+
" result[1] = mid\n",
219+
" left = mid + 1\n",
220+
" elif arr[mid] > target:\n",
221+
" right = mid - 1\n",
222+
" else:\n",
223+
" left = mid + 1\n",
224+
" left, right = (\n",
225+
" 0,\n",
226+
" result[1],\n",
227+
" ) # result[1] -1 would keep left index at -1 for single occurence of target\n",
228+
"\n",
229+
" while left <= right:\n",
230+
" mid = (left + right) // 2\n",
231+
" if arr[mid] == target:\n",
232+
" result[0] = mid\n",
233+
" right = mid - 1\n",
234+
" elif arr[mid] > target:\n",
235+
" right = mid - 1\n",
236+
" else:\n",
237+
" left = mid + 1\n",
238+
" return result\n",
239+
"\n",
240+
"\n",
241+
"from testing import assert_true\n",
242+
"\n",
243+
"\n",
244+
"fn main() raises:\n",
245+
" arr = List(5, 7, 7, 8, 8, 10)\n",
246+
" target = 8\n",
247+
" result = find_first_last(arr, target)\n",
248+
" assert_true(result[0] == 3 and result[1] == 4, \"Assertion failed\")\n",
249+
" target = 6\n",
250+
" result = find_first_last(arr, target)\n",
251+
" assert_true(result[0] == -1 and result[1] == -1, \"Assertion failed\")\n",
252+
"\n",
253+
" arr = List(5, 7, 7, 8, 10)\n",
254+
" target = 8\n",
255+
" result = find_first_last(arr, target)\n",
256+
" assert_true(result[0] == 3 and result[1] == 3, \"Assertion failed\")\n"
257+
]
258+
}
259+
]
260+
}
261+
],
262+
"metadata": {
263+
"colab": {
264+
"provenance": [],
265+
"include_colab_link": true
266+
},
267+
"kaggle": {
268+
"accelerator": "nvidiaTeslaT4",
269+
"dataSources": [],
270+
"dockerImageVersionId": 31041,
271+
"isGpuEnabled": true,
272+
"isInternetEnabled": true,
273+
"language": "python",
274+
"sourceType": "notebook"
275+
},
276+
"kernelspec": {
277+
"display_name": "Python 3",
278+
"name": "python3"
279+
},
280+
"language_info": {
281+
"codemirror_mode": {
282+
"name": "ipython",
283+
"version": 3
284+
},
285+
"file_extension": ".py",
286+
"mimetype": "text/x-python",
287+
"name": "python",
288+
"nbconvert_exporter": "python",
289+
"pygments_lexer": "ipython3",
290+
"version": "3.11.11"
291+
}
292+
},
293+
"nbformat": 4,
294+
"nbformat_minor": 0
295+
}

0 commit comments

Comments
 (0)