8000 Control flow: add some intermediate exercises (#104) · gaehlerm/python-tutorial@4186517 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4186517

Browse files
authored
Control flow: add some intermediate exercises (empa-scientific-it#104)
1 parent 89fcdb1 commit 4186517

File tree

2 files changed

+379
-44
lines changed

2 files changed

+379
-44
lines changed

control_flow.ipynb

Lines changed: 229 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"- [The `for` loop](#The-for-loop)\n",
2121
" - [The `enumerate` built-in](#The-enumerate-built-in)\n",
2222
" - [The `range` built-in](#The-range-built-in)\n",
23+
"- [Warm-up exercises 👈 **solve these first**](#Warm-up-exercises)\n",
2324
"- [Nested loops](#Nested-loops)\n",
2425
"- [Altering loops](#Altering-loops)\n",
2526
" - [`if` statement inside `for`/`while`](#if-statement-inside-for/while)\n",
@@ -349,14 +350,6 @@
349350
"Why writing six lines of code when we can do the same with just two?"
350351
]
351352
},
352-
{
353-
"cell_type": "code",
354-
"execution_count": null,
355-
"id": "84c2e6b1",
356-
"metadata": {},
357-
"outputs": [],
358-
"source": []
359-
},
360353
{
361354
"cell_type": "markdown",
362355
"id": "5b0747a4",
@@ -400,6 +393,188 @@
400393
"</div>"
401394
]
402395
},
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+
},
403578
{
404579
"cell_type": "markdown",
405580
"id": "642d0ffe-b8c9-4076-a2e2-1b86722cf1a1",
@@ -607,7 +782,9 @@
607782
{
608783
"cell_type": "markdown",
609784
"id": "478550ff-87ad-45f5-adcb-1f486bfb5689",
610-
"metadata": {},
785+
"metadata": {
786+
"tags": []
787+
},
611788
"source": [
612789
"## Exceptions"
613790
]
@@ -636,7 +813,10 @@
636813
{
637814
"cell_type": "markdown",
638815
"id": "542fdbf1-d9e5-4f71-baac-e66899c99292",
639-
"metadata": {},
816+
"metadata": {
817+
"jp-MarkdownHeadingCollapsed": true,
818+
"tags": []
819+
},
640820
"source": [
641821
"## The `try-except` block"
642822
]
@@ -779,6 +959,7 @@
779959
"cell_type": "markdown",
780960
"id": "26f986e9-dd0e-4445-b6df-73aee64c565b",
781961
"metadata": {
962+
"jp-MarkdownHeadingCollapsed": true,
782963
"tags": []
783964
},
784965
"source": [
@@ -824,10 +1005,19 @@
8241005
{
8251006
"cell_type": "markdown",
8261007
"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",
8271019
"metadata": {},
8281020
"source": [
829-
"## Find the pair 🌶️\n",
830-
"\n",
8311021
"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",
8321022
"\n",
8331023
"<div class=\"alert alert-block alert-warning\">\n",
@@ -844,7 +1034,10 @@
8441034
{
8451035
"cell_type": "markdown",
8461036
"id": "4156c367-e1b8-400c-bb2f-71c812c8ee7a",
847-
"metadata": {},
1037+
"metadata": {
1038+
"jp-MarkdownHeadingCollapsed": true,
1039+
"tags": []
1040+
},
8481041
"source": [
8491042
"### Part 1"
8501043
]
@@ -870,10 +1063,19 @@
8701063
{
8711064
"cell_type": "markdown",
8721065
"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",
8731077
"metadata": {},
8741078
"source": [
875-
"### Part 2\n",
876-
"\n",
8771079
"<div class=\"alert alert-block alert-warning\">\n",
8781080
" <h4><b>Question</b></h4>\n",
8791081
" Can you find the <b>product</b> of <em>three</em> numbers that add up to <code>2020</code>?\n",
@@ -906,10 +1108,19 @@
9061108
{
9071109
"cell_type": "markdown",
9081110
"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",
9091122
"metadata": {},
9101123
"source": [
911-
"## Cats with hats 🌶️🌶️\n",
912-
"\n",
9131124
"You have 100 cats.\n",
9141125
"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",
9151126
"\n",
@@ -953,6 +1164,7 @@
9531164
"cell_type": "markdown",
9541165
"id": "ef9ce337-7a8b-4766-9593-28edf579fe25",
9551166
"metadata": {
1167+
"jp-MarkdownHeadingCollapsed": true,
9561168
"tags": []
9571169
},
9581170
"source": [

0 commit comments

Comments
 (0)
0