Skip to content

Commit 2c7fa7b

Browse files
committed
Latest changes
1 parent f45085f commit 2c7fa7b

File tree

1 file changed

+234
-17
lines changed

1 file changed

+234
-17
lines changed

quickbook.ipynb

Lines changed: 234 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"execution_count": 1,
2525
"metadata": {
2626
"id": "buOgxm25ONit",
27-
"outputId": "f4e5a5c0-eb5d-4c8a-a22d-43df1c87c27b",
27+
"outputId": "7b9d37d8-af70-40f6-9502-6b2e943c7a08",
2828
"colab": {
2929
"base_uri": "https://localhost:8080/"
3030
}
@@ -37,8 +37,8 @@
3737
"Installing the latest version of Magic...\n",
3838
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
3939
" Dload Upload Total Spent Left Speed\n",
40-
" 0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0\n",
41-
"100 49.9M 100 49.9M 0 0 16.3M 0 0:00:03 0:00:03 --:--:-- 52.1M\n",
40+
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n",
41+
"100 49.9M 100 49.9M 0 0 17.1M 0 0:00:02 0:00:02 --:--:-- 49.1M\n",
4242
"Done. The 'magic' binary is in '/root/.modular/bin'\n",
4343
"\n",
4444
"Two more steps:\n",
@@ -69,7 +69,7 @@
6969
"execution_count": 3,
7070
"metadata": {
7171
"id": "TqFD0EK0ONiw",
72-
"outputId": "2c764610-85dc-480c-8035-4d66f77bf393",
72+
"outputId": "9c321fda-8b28-4704-df9e-cbdb83b92b1e",
7373
"colab": {
7474
"base_uri": "https://localhost:8080/"
7575
}
@@ -92,7 +92,7 @@
9292
"execution_count": 4,
9393
"metadata": {
9494
"id": "k3Ddb6GcONiw",
95-
"outputId": "7742dea4-6b54-45ad-c392-0a5a5ae1303e",
95+
"outputId": "9df3f27d-bddd-4243-c634-f0f1eb3ff840",
9696
"colab": {
9797
"base_uri": "https://localhost:8080/"
9898
}
@@ -112,13 +112,13 @@
112112
},
113113
{
114114
"cell_type": "code",
115-
"execution_count": 107,
115+
"execution_count": 20,
116116
"metadata": {
117117
"id": "IaxB1auxONix",
118118
"colab": {
119119
"base_uri": "https://localhost:8080/"
120120
},
121-
"outputId": "9fcaae95-97f2-4ee2-8f77-a90b36e98fc3"
121+
"outputId": "7d2a72b0-0406-42de-fa5e-946ff981971d"
122122
},
123123
"outputs": [
124124
{
@@ -138,8 +138,7 @@
138138
"\n",
139139
"@parameter\n",
140140
"fn compare_fn(interval1: (Int, Int), interval2: (Int, Int)) -> Bool:\n",
141-
" return interval1[0] < interval2[0]\n",
142-
"\n",
141+
" return interval1[1] < interval2[1]\n",
143142
"\n",
144143
"fn merge_intervals(mut intervals: List[(Int, Int)]) -> List[(Int, Int)]:\n",
145144
" if len(intervals) == 0:\n",
@@ -190,13 +189,13 @@
190189
},
191190
{
192191
"cell_type": "code",
193-
"execution_count": 108,
192+
"execution_count": 21,
194193
"metadata": {
195194
"colab": {
196195
"base_uri": "https://localhost:8080/"
197196
},
198197
"id": "h2k9wkDaONiz",
199-
"outputId": "5b2461ab-4ee4-4633-8572-6ee7dd647738"
198+
"outputId": "27b4c821-1f3c-4fcd-9833-d27298fcd32c"
200199
},
201200
"outputs": [
202201
{
@@ -213,13 +212,13 @@
213212
},
214213
{
215214
"cell_type": "code",
216-
"execution_count": 105,
215+
"execution_count": 19,
217216
"metadata": {
218217
"colab": {
219218
"base_uri": "https://localhost:8080/"
220219
},
221220
"id": "bSglX7bNONi0",
222-
"outputId": "24e912a4-ecc5-463d-a848-21189408635e"
221+
"outputId": "89eb14c8-d1fc-48b7-c063-acd8934ef42d"
223222
},
224223
"outputs": [
225224
{
@@ -247,21 +246,25 @@
247246
"base_uri": "https://localhost:8080/"
248247
},
249248
"id": "z2OxrAfbJYKc",
250-
"outputId": "d1d2aea5-9b68-46a8-fe4d-3afebb806331"
249+
"outputId": "d46beb8d-493d-4c7d-da94-07b9ab92e99f"
251250
},
252-
"execution_count": 106,
251+
"execution_count": 8,
253252
"outputs": [
254253
{
255254
"output_type": "stream",
256255
"name": "stdout",
257256
"text": [
257+
"\n",
258258
"### Merge Itervals\n",
259259
"### Merge overlapping intervals\n",
260260
"\n",
261261
"\n",
262262
"@parameter\n",
263-
"fn compare_fn(interval1: (Int, Int), interval2: (Int, Int)) -> Bool:\n",
264-
" return interval1[0] < interval2[0]\n",
263+
"fn compare_fn[end_time: Bool=False](interval1: (Int, Int), interval2: (Int, Int)) -> Bool:\n",
264+
" if end_time:\n",
265+
" return interval1[1] < interval2[1]\n",
266+
" else\n",
267+
" return interval1[0] interval2[0]\n",
265268
"\n",
266269
"\n",
267270
"fn merge_intervals(mut intervals: List[(Int, Int)]) -> List[(Int, Int)]:\n",
@@ -312,6 +315,220 @@
312315
]
313316
}
314317
]
318+
},
319+
{
320+
"cell_type": "code",
321+
"source": [
322+
"%%writefile min_meeting_rooms.mojo\n",
323+
"\n",
324+
"### Metting rooms\n",
325+
"### Minimum Conference Rooms for Meeting Intervals\n",
326+
"\n",
327+
"from algorithm import map\n",
328+
"\n",
329+
"\n",
330+
"@parameter\n",
331+
"fn compare_fn[\n",
332+
" end_time: Bool = False\n",
333+
"](interval1: (Int, Int), interval2: (Int, Int)) -> Bool:\n",
334+
" if end_time:\n",
335+
" return interval1[1] < interval2[1]\n",
336+
" else:\n",
337+
" return interval1[0] < interval2[0]\n",
338+
"\n",
339+
"\n",
340+
"fn min_meeting_rooms(mut intervals: List[(Int, Int)]) -> Int:\n",
341+
" if len(intervals) == 0:\n",
342+
" return 0\n",
343+
"\n",
344+
" sort[compare_fn[False]](intervals)\n",
345+
" starts = List[Int]()\n",
346+
" ends = List[Int]()\n",
347+
"\n",
348+
" @parameter\n",
349+
" fn segregate[end_time: Bool = False](idx: Int):\n",
350+
" if end_time:\n",
351+
" ends.append(intervals[idx][1])\n",
352+
" else:\n",
353+
" starts.append(intervals[idx][0])\n",
354+
"\n",
355+
" map[segregate[False]](len(intervals))\n",
356+
" sort[compare_fn[True]](intervals)\n",
357+
" map[segregate[True]](len(intervals))\n",
358+
"\n",
359+
" print(starts.__str__())\n",
360+
" print(ends.__str__())\n",
361+
"\n",
362+
" return 100\n",
363+
"\n",
364+
"\n",
365+
"from testing import assert_true\n",
366+
"\n",
367+
"\n",
368+
"fn main() raises:\n",
369+
" intervals = List[(Int, Int)]((0, 30), (5, 10), (15, 20))\n",
370+
" expected = 100\n",
371+
" result = min_meeting_rooms(intervals)\n",
372+
" assert_true(result == expected, \"Assertion failed\")\n",
373+
" intervals = List[(Int, Int)]((7, 10), (2, 4))\n",
374+
" expected = 100\n",
375+
" result = min_meeting_rooms(intervals)\n",
376+
" assert_true(result == expected, \"Assertion failed\")\n",
377+
" intervals = List[(Int, Int)]((1, 5), (8, 9), (8, 9))\n",
378+
" expected = 100\n",
379+
" result = min_meeting_rooms(intervals)\n",
380+
" assert_true(result == expected, \"Assertion failed\")"
381+
],
382+
"metadata": {
383+
"id": "9ljcqcz8keOw",
384+
"outputId": "d054dee3-99d4-40ab-a199-a8a4a8f0a93f",
385+
"colab": {
386+
"base_uri": "https://localhost:8080/"
387+
}
388+
},
389+
"execution_count": 38,
390+
"outputs": [
391+
{
392+
"output_type": "stream",
393+
"name": "stdout",
394+
"text": [
395+
"Overwriting min_meeting_rooms.mojo\n"
396+
]
397+
}
398+
]
399+
},
400+
{
401+
"cell_type": "code",
402+
"source": [
403+
"!magic run mojo min_meeting_rooms.mojo"
404+
],
405+
"metadata": {
406+
"id": "ttztLnIjtEyZ",
407+
"outputId": "daf7ff97-b05c-487b-dd49-24ec876f4309",
408+
"colab": {
409+
"base_uri": "https://localhost:8080/"
410+
}
411+
},
412+
"execution_count": 39,
413+
"outputs": [
414+
{
415+
"output_type": "stream",
416+
"name": "stdout",
417+
"text": [
418+
"\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[0, 5, 15]\n",
419+
"[10, 20, 30]\n",
420+
"[2, 7]\n",
421+
"[4, 10]\n",
422+
"[1, 8, 8]\n",
423+
"[5, 9, 9]\n"
424+
]
425+
}
426+
]
427+
},
428+
{
429+
"cell_type": "code",
430+
"source": [
431+
"!magic run mojo format min_meeting_rooms.mojo"
432+
],
433+
"metadata": {
434+
"id": "ynmikGkNy1Sy",
435+
"outputId": "780d42b0-9782-4736-a9b2-24c5b0525a69",
436+
"colab": {
437+
"base_uri": "https://localhost:8080/"
438+
}
439+
},
440+
"execution_count": 36,
441+
"outputs": [
442+
{
443+
"output_type": "stream",
444+
"name": "stdout",
445+
"text": [
446+
"\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 min_meeting_rooms.mojo\u001b[0m\n",
447+
"\n",
448+
"\u001b[1mAll done! ✨ 🍰 ✨\u001b[0m\n",
449+
"\u001b[34m\u001b[1m1 file \u001b[0m\u001b[1mreformatted\u001b[0m.\n"
450+
]
451+
}
452+
]
453+
},
454+
{
455+
"cell_type": "code",
456+
"source": [
457+
"!cat min_meeting_rooms.mojo"
458+
],
459+
"metadata": {
460+
"id": "HRKT0Oy1y4CY",
461+
"outputId": "dbd7dfdb-fbe4-447c-c4fe-ca2a836e508a",
462+
"colab": {
463+
"base_uri": "https://localhost:8080/"
464+
}
465+
},
466+
"execution_count": 37,
467+
"outputs": [
468+
{
469+
"output_type": "stream",
470+
"name": "stdout",
471+
"text": [
472+
"### Metting rooms\n",
473+
"### Minimum Conference Rooms for Meeting Intervals\n",
474+
"\n",
475+
"from algorithm import map\n",
476+
"\n",
477+
"\n",
478+
"@parameter\n",
479+
"fn compare_fn[\n",
480+
" end_time: Bool = False\n",
481+
"](interval1: (Int, Int), interval2: (Int, Int)) -> Bool:\n",
482+
" if end_time:\n",
483+
" return interval1[1] < interval2[1]\n",
484+
" else:\n",
485+
" return interval1[0] < interval2[0]\n",
486+
"\n",
487+
"\n",
488+
"fn min_meeting_rooms(mut intervals: List[(Int, Int)]) -> Int:\n",
489+
" if len(intervals) == 0:\n",
490+
" return 0\n",
491+
"\n",
492+
" sort[compare_fn[False]](intervals)\n",
493+
" starts = List[Int]()\n",
494+
" ends = List[Int]()\n",
495+
"\n",
496+
" @parameter\n",
497+
" fn segregate[end_time: Bool = False](idx: Int):\n",
498+
" if end_time:\n",
499+
" ends.append(intervals[idx][1])\n",
500+
" else:\n",
501+
" starts.append(intervals[idx][0])\n",
502+
"\n",
503+
" map[segregate[False]](len(intervals))\n",
504+
" sort[compare_fn[True]](intervals)\n",
505+
" map[segregate[True]](len(intervals))\n",
506+
"\n",
507+
" print(starts.__str__())\n",
508+
" print(ends.__str__())\n",
509+
"\n",
510+
" return 100\n",
511+
"\n",
512+
"\n",
513+
"from testing import assert_true\n",
514+
"\n",
515+
"\n",
516+
"fn main() raises:\n",
517+
" intervals = List[(Int, Int)]((0, 30), (5, 10), (15, 20))\n",
518+
" expected = 100\n",
519+
" result = min_meeting_rooms(intervals)\n",
520+
" assert_true(result == expected, \"Assertion failed\")\n",
521+
" intervals = List[(Int, Int)]((7, 10), (2, 4))\n",
522+
" expected = 100\n",
523+
" result = min_meeting_rooms(intervals)\n",
524+
" assert_true(result == expected, \"Assertion failed\")\n",
525+
" intervals = List[(Int, Int)]((1, 5), (8, 9), (8, 9))\n",
526+
" expected = 100\n",
527+
" result = min_meeting_rooms(intervals)\n",
528+
" assert_true(result == expected, \"Assertion failed\")\n"
529+
]
530+
}
531+
]
315532
}
316533
],
317534
"metadata": {

0 commit comments

Comments
 (0)