|
24 | 24 | "execution_count": 1, |
25 | 25 | "metadata": { |
26 | 26 | "id": "buOgxm25ONit", |
27 | | - "outputId": "f4e5a5c0-eb5d-4c8a-a22d-43df1c87c27b", |
| 27 | + "outputId": "7b9d37d8-af70-40f6-9502-6b2e943c7a08", |
28 | 28 | "colab": { |
29 | 29 | "base_uri": "https://localhost:8080/" |
30 | 30 | } |
|
37 | 37 | "Installing the latest version of Magic...\n", |
38 | 38 | " % Total % Received % Xferd Average Speed Time Time Time Current\n", |
39 | 39 | " 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", |
42 | 42 | "Done. The 'magic' binary is in '/root/.modular/bin'\n", |
43 | 43 | "\n", |
44 | 44 | "Two more steps:\n", |
|
69 | 69 | "execution_count": 3, |
70 | 70 | "metadata": { |
71 | 71 | "id": "TqFD0EK0ONiw", |
72 | | - "outputId": "2c764610-85dc-480c-8035-4d66f77bf393", |
| 72 | + "outputId": "9c321fda-8b28-4704-df9e-cbdb83b92b1e", |
73 | 73 | "colab": { |
74 | 74 | "base_uri": "https://localhost:8080/" |
75 | 75 | } |
|
92 | 92 | "execution_count": 4, |
93 | 93 | "metadata": { |
94 | 94 | "id": "k3Ddb6GcONiw", |
95 | | - "outputId": "7742dea4-6b54-45ad-c392-0a5a5ae1303e", |
| 95 | + "outputId": "9df3f27d-bddd-4243-c634-f0f1eb3ff840", |
96 | 96 | "colab": { |
97 | 97 | "base_uri": "https://localhost:8080/" |
98 | 98 | } |
|
112 | 112 | }, |
113 | 113 | { |
114 | 114 | "cell_type": "code", |
115 | | - "execution_count": 107, |
| 115 | + "execution_count": 20, |
116 | 116 | "metadata": { |
117 | 117 | "id": "IaxB1auxONix", |
118 | 118 | "colab": { |
119 | 119 | "base_uri": "https://localhost:8080/" |
120 | 120 | }, |
121 | | - "outputId": "9fcaae95-97f2-4ee2-8f77-a90b36e98fc3" |
| 121 | + "outputId": "7d2a72b0-0406-42de-fa5e-946ff981971d" |
122 | 122 | }, |
123 | 123 | "outputs": [ |
124 | 124 | { |
|
138 | 138 | "\n", |
139 | 139 | "@parameter\n", |
140 | 140 | "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", |
143 | 142 | "\n", |
144 | 143 | "fn merge_intervals(mut intervals: List[(Int, Int)]) -> List[(Int, Int)]:\n", |
145 | 144 | " if len(intervals) == 0:\n", |
|
190 | 189 | }, |
191 | 190 | { |
192 | 191 | "cell_type": "code", |
193 | | - "execution_count": 108, |
| 192 | + "execution_count": 21, |
194 | 193 | "metadata": { |
195 | 194 | "colab": { |
196 | 195 | "base_uri": "https://localhost:8080/" |
197 | 196 | }, |
198 | 197 | "id": "h2k9wkDaONiz", |
199 | | - "outputId": "5b2461ab-4ee4-4633-8572-6ee7dd647738" |
| 198 | + "outputId": "27b4c821-1f3c-4fcd-9833-d27298fcd32c" |
200 | 199 | }, |
201 | 200 | "outputs": [ |
202 | 201 | { |
|
213 | 212 | }, |
214 | 213 | { |
215 | 214 | "cell_type": "code", |
216 | | - "execution_count": 105, |
| 215 | + "execution_count": 19, |
217 | 216 | "metadata": { |
218 | 217 | "colab": { |
219 | 218 | "base_uri": "https://localhost:8080/" |
220 | 219 | }, |
221 | 220 | "id": "bSglX7bNONi0", |
222 | | - "outputId": "24e912a4-ecc5-463d-a848-21189408635e" |
| 221 | + "outputId": "89eb14c8-d1fc-48b7-c063-acd8934ef42d" |
223 | 222 | }, |
224 | 223 | "outputs": [ |
225 | 224 | { |
|
247 | 246 | "base_uri": "https://localhost:8080/" |
248 | 247 | }, |
249 | 248 | "id": "z2OxrAfbJYKc", |
250 | | - "outputId": "d1d2aea5-9b68-46a8-fe4d-3afebb806331" |
| 249 | + "outputId": "d46beb8d-493d-4c7d-da94-07b9ab92e99f" |
251 | 250 | }, |
252 | | - "execution_count": 106, |
| 251 | + "execution_count": 8, |
253 | 252 | "outputs": [ |
254 | 253 | { |
255 | 254 | "output_type": "stream", |
256 | 255 | "name": "stdout", |
257 | 256 | "text": [ |
| 257 | + "\n", |
258 | 258 | "### Merge Itervals\n", |
259 | 259 | "### Merge overlapping intervals\n", |
260 | 260 | "\n", |
261 | 261 | "\n", |
262 | 262 | "@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", |
265 | 268 | "\n", |
266 | 269 | "\n", |
267 | 270 | "fn merge_intervals(mut intervals: List[(Int, Int)]) -> List[(Int, Int)]:\n", |
|
312 | 315 | ] |
313 | 316 | } |
314 | 317 | ] |
| 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 | + ] |
315 | 532 | } |
316 | 533 | ], |
317 | 534 | "metadata": { |
|
0 commit comments