|
20 | 20 | "- [The `for` loop](#The-for-loop)\n",
|
21 | 21 | " - [The `enumerate` built-in](#The-enumerate-built-in)\n",
|
22 | 22 | " - [The `range` built-in](#The-range-built-in)\n",
|
| 23 | + "- [Warm-up exercises 👈 **solve these first**](#Warm-up-exercises)\n", |
23 | 24 | "- [Nested loops](#Nested-loops)\n",
|
24 | 25 | "- [Altering loops](#Altering-loops)\n",
|
25 | 26 | " - [`if` statement inside `for`/`while`](#if-statement-inside-for/while)\n",
|
|
349 | 350 | "Why writing six lines of code when we can do the same with just two?"
|
350 | 351 | ]
|
351 | 352 | },
|
352 |
| - { |
353 |
| - "cell_type": "code", |
354 |
| - "execution_count": null, |
355 |
| - "id": "84c2e6b1", |
356 |
| - "metadata": {}, |
357 |
| - "outputs": [], |
358 |
| - "source": [] |
359 |
| - }, |
360 | 353 | {
|
361 | 354 | "cell_type": "markdown",
|
362 | 355 | "id": "5b0747a4",
|
|
400 | 393 | "</div>"
|
401 | 394 | ]
|
402 | 395 | },
|
| 396 | + { |
| 397 | + "cell_type": "markdown", |
| 398 | + "id": "efb1a53e-a447-4808-bbb3-34a614d35735", |
| 399 | + "metadata": { |
| 400 | + "tags": [] |
| 401 | + }, |
| 402 | + "source": [ |
| 403 | + "## Warm-up exercises\n", |
| 404 | + "\n", |
| 405 | + "Here are a few exercises to practice the concepts seen above.\n", |
| 406 | + "\n", |
| 407 | + "<div class=\"alert alert-block alert-danger\">\n", |
| 408 | + " <h4><b>Important</b></h4>\n", |
| 409 | + " <ul>\n", |
| 410 | + " <li>Try to use loop constructs like <code>for</code> or <code>while</code>, and the built-in iteration functions like <code>range()</code></li>\n", |
| 411 | + " <li>Try to solve these exercises <strong>first</strong> before attempting any of those suggested in the last section.</li>\n", |
| 412 | + " </ul>\n", |
| 413 | + "</div>" |
| 414 | + ] |
| 415 | + }, |
| 416 | + { |
| 417 | + "cell_type": "code", |
| 418 | + "execution_count": null, |
| 419 | + "id": "530cb91a-1243-401b-84a0-ff7973833908", |
| 420 | + "metadata": { |
| 421 | + "tags": [] |
| 422 | + }, |
| 423 | + "outputs": [], |
| 424 | + "source": [ |
| 425 | + "%reload_ext tutorial.tests.testsuite" |
| 426 | + ] |
| 427 | + }, |
| 428 | + { |
| 429 | + "cell_type": "markdown", |
| 430 | + "id": "e23dfd66-a14b-4346-af3d-9b1419063c84", |
| 431 | + "metadata": {}, |
| 432 | + "source": [ |
| 433 | + "#### 1. Write a Python program that returns the characters in a string and their indexes\n", |
| 434 | + "\n", |
| 435 | + "\n", |
| 436 | + "<div class=\"alert alert-block alert-warning\">\n", |
| 437 | + " <h4><b>Note</b></h4> \n", |
| 438 | + " The index should be returned <strong>after</strong> the corresponding character.\n", |
| 439 | + "</div>\n", |
| 440 | + "\n", |
| 441 | + "For example, if the string is `python`, the result should be `[('p', 0), ('y', 1), ('t', 2), ('h', 3), ('o', 4), ('n', 5)]`" |
| 442 | + ] |
| 443 | + }, |
| 444 | + { |
| 445 | + "cell_type": "code", |
| 446 | + "execution_count": null, |
| 447 | + "id": "4eb8fcdc-eed6-469b-891b-817e8b4dd23c", |
| 448 | + "metadata": { |
| 449 | + "tags": [] |
| 450 | + }, |
| 451 | + "outputs": [], |
| 452 | + "source": [ |
| 453 | + "%%ipytest\n", |
| 454 | + "\n", |
| 455 | + "def solution_indexed_string(string: str) -> list[tuple]:\n", |
| 456 | + " \"\"\"\n", |
| 457 | + " Write your solution here\n", |
| 458 | + " \"\"\"\n", |
| 459 | + " return" |
| 460 | + ] |
| 461 | + }, |
| 462 | + { |
| 463 | + "cell_type": "markdown", |
| 464 | + "id": "679b0816-3b20-4001-a39a-5558179bb9de", |
| 465 | + "metadata": { |
| 466 | + "tags": [] |
| 467 | + }, |
| 468 | + "source": [ |
| 469 | + "#### 2. Write a Python program that returns all the numbers in a given range, __including__ the first and the last elements\n", |
| 470 | + "\n", |
| 471 | + "<div class=\"alert alert-block alert-warning\">\n", |
| 472 | + " <h4><b>Note</b></h4>\n", |
| 473 | + " Ranges can also contain <strong>decreasing</strong> numbers. Make sure to build the correct range.\n", |
| 474 | + "</div>" |
| 475 | + ] |
| 476 | + }, |
| 477 | + { |
| 478 | + "cell_type": "code", |
| 479 | + "execution_count": null, |
| 480 | + "id": "c24d6340-4731-4776-b800-f2ef01f8afaf", |
| 481 | + "metadata": { |
| 482 | + "tags": [] |
| 483 | + }, |
| 484 | + "outputs": [], |
| 485 | + "source": [ |
| 486 | + "%%ipytest\n", |
| 487 | + "\n", |
| 488 | + "def solution_range_of_nums(start: int, end: int) -> list[int]:\n", |
| 489 | + " \"\"\"\n", |
| 490 | + " Write your solution here\n", |
| 491 | + " \"\"\"\n", |
| 492 | + " return" |
| 493 | + ] |
| 494 | + }, |
| 495 | + { |
| 496 | + "cell_type": "markdown", |
| 497 | + "id": "9823551f-1ea7-44b7-9dc3-2e002331e334", |
| 498 | + "metadata": { |
| 499 | + "tags": [] |
| 500 | + }, |
| 501 | + "source": [ |
| 502 | + "#### 3. Write a Python program that takes a list of integers and returns the square root of each of them\n", |
| 503 | + "\n", |
| 504 | + "<div class=\"alert alert-block alert-info\">\n", |
| 505 | + " <h4><b>Hints</b></h4>\n", |
| 506 | + " <ul>\n", |
| 507 | + " <li>You can use the <code>math.sqrt</code> function to compute the square root of a number</li>\n", |
| 508 | + " <li>Be sure to check that it makes sense to compute the square root</li>\n", |
| 509 | + " </ul>\n", |
| 510 | + "</div>" |
| 511 | + ] |
| 512 | + }, |
| 513 | + { |
| 514 | + "cell_type": "code", |
| 515 | + "execution_count": null, |
| 516 | + "id": "4fc3cab5-6d16-43a4-adf8-8560a0c29558", |
| 517 | + "metadata": { |
| 518 | + "tags": [] |
| 519 | + }, |
| 520 | + "outputs": [], |
| 521 | + "source": [ |
| 522 | + "%%ipytest\n", |
| 523 | + "\n", |
| 524 | + "import math\n", |
| 525 | + "\n", |
| 526 | + "def solution_sqrt_of_nums(numbers: list) -> list:\n", |
| 527 | + " \"\"\"\n", |
| 528 | + " Write your solution here\n", |
| 529 | + " \"\"\"\n", |
| 530 | + " return" |
| 531 | + ] |
| 532 | + }, |
| 533 | + { |
| 534 | + "cell_type": "markdown", |
| 535 | + "id": "692f608f-2e79-4ad4-9a3d-ec9625941f81", |
| 536 | + "metadata": {}, |
| 537 | + "source": [ |
| 538 | + "#### 4. Write a Python program that takes an integer and divides it until the result is no longer an even number" |
| 539 | + ] |
| 540 | + }, |
| 541 | + { |
| 542 | + "cell_type": "markdown", |
| 543 | + "id": "2d06d01d-21a6-479d-bdbf-38de3151f9d3", |
| 544 | + "metadata": {}, |
| 545 | + "source": [ |
| 546 | + "<div class=\"alert alert-block alert-info\">\n", |
| 547 | + " <h4><b>Hint</b></h4>\n", |
| 548 | + " Your program should <strong>return</strong> the number when the iteration stopped\n", |
| 549 | + "</div>" |
| 550 | + ] |
| 551 | + }, |
| 552 | + { |
| 553 | + "cell_type": "code", |
| 554 | + "execution_count": null, |
| 555 | + "id": "4d83467c-e736-4b27-b9c2-9ade4a70a918", |
| 556 | + "metadata": { |
| 557 | + "tags": [] |
| 558 | + }, |
| 559 | + "outputs": [], |
| 560 | + "source": [ |
| 561 | + "%%ipytest\n", |
| 562 | + "\n", |
| 563 | + "def solution_divide_until(num: int) -> int:\n", |
| 564 | + " \"\"\"\n", |
| 565 | + " Write your solution here\n", |
| 566 | + " \"\"\"\n", |
| 567 | + " return" |
| 568 | + ] |
| 569 | + }, |
| 570 | + { |
| 571 | + "cell_type": "markdown", |
| 572 | + "id": "e906c5d5-bc12-4829-b290-ed17ac1ddb3d", |
| 573 | + "metadata": {}, |
| 574 | + "source": [ |
| 575 | + "---" |
| 576 | + ] |
| 577 | + }, |
403 | 578 | {
|
404 | 579 | "cell_type": "markdown",
|
405 | 580 | "id": "642d0ffe-b8c9-4076-a2e2-1b86722cf1a1",
|
|
607 | 782 | {
|
608 | 783 | "cell_type": "markdown",
|
609 | 784 | "id": "478550ff-87ad-45f5-adcb-1f486bfb5689",
|
610 |
| - "metadata": {}, |
| 785 | + "metadata": { |
| 786 | + "tags": [] |
| 787 | + }, |
611 | 788 | "source": [
|
612 | 789 | "## Exceptions"
|
613 | 790 | ]
|
|
636 | 813 | {
|
637 | 814 | "cell_type": "markdown",
|
638 | 815 | "id": "542fdbf1-d9e5-4f71-baac-e66899c99292",
|
639 |
| - "metadata": {}, |
| 816 | + "metadata": { |
| 817 | + "jp-MarkdownHeadingCollapsed": true, |
| 818 | + "tags": [] |
| 819 | + }, |
640 | 820 | "source": [
|
641 | 821 | "## The `try-except` block"
|
642 | 822 | ]
|
|
779 | 959 | "cell_type": "markdown",
|
780 | 960 | "id": "26f986e9-dd0e-4445-b6df-73aee64c565b",
|
781 | 961 | "metadata": {
|
| 962 | + "jp-MarkdownHeadingCollapsed": true, |
782 | 963 | "tags": []
|
783 | 964 | },
|
784 | 965 | "source": [
|
|
824 | 1005 | {
|
825 | 1006 | "cell_type": "markdown",
|
826 | 1007 | "id": "5c2e26a7-31f4-4419-a68d-811da56adfb1",
|
| 1008 | + "metadata": { |
| 1009 | + "jp-MarkdownHeadingCollapsed": true, |
| 1010 | + "tags": [] |
| 1011 | + }, |
| 1012 | + "source": [ |
| 1013 | + "## Find the pair 🌶️" |
| 1014 | + ] |
| 1015 | + }, |
| 1016 | + { |
| 1017 | + "cell_type": "markdown", |
| 1018 | + "id": "e04d871e-16ae-4209-9118-7fdbc1d90d61", |
827 | 1019 | "metadata": {},
|
828 | 1020 | "source": [
|
829 |
| - "## Find the pair 🌶️\n", |
830 |
| - "\n", |
831 | 1021 | "Given a list of integers, your task is to complete the code below that finds the pair of numbers in the list that add up to `2020`. The list of numbers is already available as the variable `nums`.\n",
|
832 | 1022 | "\n",
|
833 | 1023 | "<div class=\"alert alert-block alert-warning\">\n",
|
|
844 | 1034 | {
|
845 | 1035 | "cell_type": "markdown",
|
846 | 1036 | "id": "4156c367-e1b8-400c-bb2f-71c812c8ee7a",
|
847 |
| - "metadata": {}, |
| 1037 | + "metadata": { |
| 1038 | + "jp-MarkdownHeadingCollapsed": true, |
| 1039 | + "tags": [] |
| 1040 | + }, |
848 | 1041 | "source": [
|
849 | 1042 | "### Part 1"
|
850 | 1043 | ]
|
|
870 | 1063 | {
|
871 | 1064 | "cell_type": "markdown",
|
872 | 1065 | "id": "55055676-4a91-4c45-8db6-b1736a958138",
|
| 1066 | + "metadata": { |
| 1067 | + "jp-MarkdownHeadingCollapsed": true, |
| 1068 | + "tags": [] |
| 1069 | + }, |
| 1070 | + "source": [ |
| 1071 | + "### Part 2" |
| 1072 | + ] |
| 1073 | + }, |
| 1074 | + { |
| 1075 | + "cell_type": "markdown", |
| 1076 | + "id": "44c96dcd-d89d-4073-9e8f-226efd124d99", |
873 | 1077 | "metadata": {},
|
874 | 1078 | "source": [
|
875 |
| - "### Part 2\n", |
876 |
| - "\n", |
877 | 1079 | "<div class=\"alert alert-block alert-warning\">\n",
|
878 | 1080 | " <h4><b>Question</b></h4>\n",
|
879 | 1081 | " Can you find the <b>product</b> of <em>three</em> numbers that add up to <code>2020</code>?\n",
|
|
906 | 1108 | {
|
907 | 1109 | "cell_type": "markdown",
|
908 | 1110 | "id": "cc5d1993-29ab-4e73-911f-61830a220c4e",
|
| 1111 | + "metadata": { |
| 1112 | + "jp-MarkdownHeadingCollapsed": true, |
| 1113 | + "tags": [] |
| 1114 | + }, |
| 1115 | + "source": [ |
| 1116 | + "## Cats with hats 🌶️🌶️" |
| 1117 | + ] |
| 1118 | + }, |
| 1119 | + { |
| 1120 | + "cell_type": "markdown", |
| 1121 | + "id": "cf68ea90-36ea-4017-8b13-ab90455c5252", |
909 | 1122 | "metadata": {},
|
910 | 1123 | "source": [
|
911 |
| - "## Cats with hats 🌶️🌶️\n", |
912 |
| - "\n", |
913 | 1124 | "You have 100 cats.\n",
|
914 | 1125 | "One day you decide to arrange all your cats in a giant circle. Initially, none of your cats have any hats on. You walk around the circle 100 times, always starting at the same spot, with the first cat (cat #1).\n",
|
915 | 1126 | "\n",
|
|
953 | 1164 | "cell_type": "markdown",
|
954 | 1165 | "id": "ef9ce337-7a8b-4766-9593-28edf579fe25",
|
955 | 1166 | "metadata": {
|
| 1167 | + "jp-MarkdownHeadingCollapsed": true, |
956 | 1168 | "tags": []
|
957 | 1169 | },
|
958 | 1170 | "source": [
|
|
0 commit comments