From b7fccb6629925f6630156493b21a65992e90fc23 Mon Sep 17 00:00:00 2001 From: Despina Adamopoulou Date: Tue, 30 Apr 2024 14:56:31 +0200 Subject: [PATCH 1/7] fix typos and hidden cells --- control_flow.ipynb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/control_flow.ipynb b/control_flow.ipynb index 9edd6fdc..84ef808b 100644 --- a/control_flow.ipynb +++ b/control_flow.ipynb @@ -76,7 +76,7 @@ "source": [ "Python [supports](./basic_datatypes.ipynb#Comparison-operators) different comparison expressions. They return either `True` or `False`.\n", "\n", - "We use these results to evaluate **conditional statements**, and branch have our program behave differently." + "We use these results to evaluate **conditional statements**, and have our program behave differently, based on the result." ] }, { @@ -153,7 +153,7 @@ "id": "9", "metadata": {}, "source": [ - "`while` loops repeat a block of code until some condition remains true.\n", + "`while` loops repeat a block of code for as long as some condition remains true.\n", "\n", "1. The `while` **statement** starts with the `while` (reserved) keyword, followed by a `test_condition`, and must end with a colon (`:`)\n", "2. The **loop body** contains the lines of code that get repeated. Each line must be indented, or you will get a syntax error\n", @@ -814,7 +814,6 @@ "cell_type": "markdown", "id": "52", "metadata": { - "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -975,12 +974,12 @@ "\n", "
\n", "

Question

\n", - " Complete the Python code below. Given an integer $n$, return the list of all integers $m \\leq n$ that are factors of $n$.\n", + " Complete the Python code below. Given an integer n, return the list of all integers m <= n that are factors of n.\n", "
\n", "\n", "
\n", "

Hint

\n", - " For the moment, consider all integers less than or equal to $n$\n", + " For the moment, consider all integers less than or equal to n\n", "
" ] }, @@ -1035,7 +1034,6 @@ "cell_type": "markdown", "id": "67", "metadata": { - "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1064,7 +1062,6 @@ "cell_type": "markdown", "id": "69", "metadata": { - "jp-MarkdownHeadingCollapsed": true, "tags": [] }, "source": [ @@ -1363,7 +1360,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.3" + "version": "3.10.13" }, "toc-autonumbering": false, "toc-showmarkdowntxt": false, From a34e48016e3420a29e608d2cac114aa793247e65 Mon Sep 17 00:00:00 2001 From: Despina Adamopoulou Date: Thu, 2 May 2024 15:50:27 +0200 Subject: [PATCH 2/7] simplify solution for finding factors of an integer --- tutorial/tests/test_control_flow.py | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/tutorial/tests/test_control_flow.py b/tutorial/tests/test_control_flow.py index 84affc45..b6342726 100644 --- a/tutorial/tests/test_control_flow.py +++ b/tutorial/tests/test_control_flow.py @@ -1,7 +1,7 @@ import pathlib import sys from collections import Counter -from math import isclose, isqrt, sqrt +from math import isclose, sqrt from typing import List, Tuple import pytest @@ -125,25 +125,8 @@ def test_divide_until(num: int, function_to_test) -> None: # -def is_prime(num: int) -> bool: - """Check if a number is prime""" - if num <= 3: - return num > 1 - - if num % 2 == 0 or num % 3 == 0: - return False - - for i in range(5, isqrt(num) + 1, 6): - if num % i == 0 or num % (i + 2) == 0: - return False - - return True - - def reference_find_factors(num: int) -> List[int]: - """Dumb way to find the factors of an integer""" - if is_prime(num): - return [1, num] + """Reference solution to find the factors of an integer""" return [m for m in range(1, num + 1) if num % m == 0] From d26cb6e6aa4d65a1f3c006d94dfd42c2299b9bfe Mon Sep 17 00:00:00 2001 From: Despina Adamopoulou Date: Fri, 3 May 2024 09:38:31 +0200 Subject: [PATCH 3/7] added quiz on loops --- control_flow.ipynb | 174 ++++++++++++++++++++++----------------- tutorial/control_flow.py | 87 ++++++++++++++++++++ 2 files changed, 184 insertions(+), 77 deletions(-) create mode 100644 tutorial/control_flow.py diff --git a/control_flow.ipynb b/control_flow.ipynb index 84ef808b..f3c70e35 100644 --- a/control_flow.ipynb +++ b/control_flow.ipynb @@ -16,6 +16,7 @@ "# Table of contents\n", "- [References](#References)\n", "- [Conditionals](#Conditionals)\n", + "- [Quiz on conditionals](#Quiz-on-conditionals)\n", "- [The `while` loop](#The-while-loop)\n", "- [The `for` loop](#The-for-loop)\n", " - [The `enumerate` built-in](#The-enumerate-built-in)\n", @@ -141,6 +142,25 @@ { "cell_type": "markdown", "id": "8", + "metadata": {}, + "source": [ + "## Quiz on conditionals" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9", + "metadata": {}, + "outputs": [], + "source": [ + "from tutorial import control_flow as quiz\n", + "quiz.ControlFlow()" + ] + }, + { + "cell_type": "markdown", + "id": "10", "metadata": { "tags": [] }, @@ -150,7 +170,7 @@ }, { "cell_type": "markdown", - "id": "9", + "id": "11", "metadata": {}, "source": [ "`while` loops repeat a block of code for as long as some condition remains true.\n", @@ -170,7 +190,7 @@ }, { "cell_type": "markdown", - "id": "10", + "id": "12", "metadata": {}, "source": [ "A very simple example:" @@ -179,7 +199,7 @@ { "cell_type": "code", "execution_count": null, - "id": "11", + "id": "13", "metadata": { "tags": [] }, @@ -195,7 +215,7 @@ }, { "cell_type": "markdown", - "id": "12", + "id": "14", "metadata": {}, "source": [ "First, `n` is initialized to 1.\n", @@ -208,7 +228,7 @@ }, { "cell_type": "markdown", - "id": "13", + "id": "15", "metadata": {}, "source": [ "\n", @@ -237,7 +257,7 @@ }, { "cell_type": "markdown", - "id": "14", + "id": "16", "metadata": { "tags": [] }, @@ -247,7 +267,7 @@ }, { "cell_type": "markdown", - "id": "15", + "id": "17", "metadata": {}, "source": [ "In Python, an **iterable** is an **object** capable of returning its members one at a time.\n", @@ -263,7 +283,7 @@ }, { "cell_type": "markdown", - "id": "16", + "id": "18", "metadata": {}, "source": [ "In other languages (C++, Java or JavaScript), a `for` loop is more similar to `while` in Python. For example, the C++ loop\n", @@ -306,7 +326,7 @@ { "cell_type": "code", "execution_count": null, - "id": "17", + "id": "19", "metadata": { "tags": [] }, @@ -318,7 +338,7 @@ }, { "cell_type": "markdown", - "id": "18", + "id": "20", "metadata": {}, "source": [ "The loops runs until there are letters in the string `'Python'`, as strings are iterable.\n", @@ -328,7 +348,7 @@ { "cell_type": "code", "execution_count": null, - "id": "19", + "id": "21", "metadata": { "tags": [] }, @@ -344,7 +364,7 @@ }, { "cell_type": "markdown", - "id": "20", + "id": "22", "metadata": {}, "source": [ "Why writing six lines of code when we can do the same with just two?" @@ -352,7 +372,7 @@ }, { "cell_type": "markdown", - "id": "21", + "id": "23", "metadata": {}, "source": [ "### The `enumerate` built-in\n", @@ -365,7 +385,7 @@ { "cell_type": "code", "execution_count": null, - "id": "22", + "id": "24", "metadata": {}, "outputs": [], "source": [ @@ -375,7 +395,7 @@ }, { "cell_type": "markdown", - "id": "23", + "id": "25", "metadata": {}, "source": [ "### The `range` built-in\n", @@ -395,7 +415,7 @@ }, { "cell_type": "markdown", - "id": "24", + "id": "26", "metadata": { "tags": [] }, @@ -416,7 +436,7 @@ { "cell_type": "code", "execution_count": null, - "id": "25", + "id": "27", "metadata": { "tags": [] }, @@ -427,7 +447,7 @@ }, { "cell_type": "markdown", - "id": "26", + "id": "28", "metadata": {}, "source": [ "#### 1. Write a Python program that returns the characters in a string and their indexes\n", @@ -444,7 +464,7 @@ { "cell_type": "code", "execution_count": null, - "id": "27", + "id": "29", "metadata": { "tags": [] }, @@ -461,7 +481,7 @@ }, { "cell_type": "markdown", - "id": "28", + "id": "30", "metadata": { "tags": [] }, @@ -477,7 +497,7 @@ { "cell_type": "code", "execution_count": null, - "id": "29", + "id": "31", "metadata": { "tags": [] }, @@ -494,7 +514,7 @@ }, { "cell_type": "markdown", - "id": "30", + "id": "32", "metadata": { "tags": [] }, @@ -513,7 +533,7 @@ { "cell_type": "code", "execution_count": null, - "id": "31", + "id": "33", "metadata": { "tags": [] }, @@ -532,7 +552,7 @@ }, { "cell_type": "markdown", - "id": "32", + "id": "34", "metadata": {}, "source": [ "#### 4. Write a Python program that takes an integer and divides it by 2 until the result is no longer an even number" @@ -540,7 +560,7 @@ }, { "cell_type": "markdown", - "id": "33", + "id": "35", "metadata": {}, "source": [ "
\n", @@ -552,7 +572,7 @@ { "cell_type": "code", "execution_count": null, - "id": "34", + "id": "36", "metadata": { "tags": [] }, @@ -569,7 +589,7 @@ }, { "cell_type": "markdown", - "id": "35", + "id": "37", "metadata": {}, "source": [ "---" @@ -577,7 +597,7 @@ }, { "cell_type": "markdown", - "id": "36", + "id": "38", "metadata": { "tags": [] }, @@ -587,7 +607,7 @@ }, { "cell_type": "markdown", - "id": "37", + "id": "39", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -612,7 +632,7 @@ }, { "cell_type": "markdown", - "id": "38", + "id": "40", "metadata": { "tags": [] }, @@ -622,7 +642,7 @@ }, { "cell_type": "markdown", - "id": "39", + "id": "41", "metadata": {}, "source": [ "There are **4 ways** in which you can alter the normal execution of a loop:\n", @@ -637,7 +657,7 @@ }, { "cell_type": "markdown", - "id": "40", + "id": "42", "metadata": {}, "source": [ "### `if` statement inside `for`/`while`\n", @@ -648,7 +668,7 @@ { "cell_type": "code", "execution_count": null, - "id": "41", + "id": "43", "metadata": { "tags": [] }, @@ -667,7 +687,7 @@ }, { "cell_type": "markdown", - "id": "42", + "id": "44", "metadata": {}, "source": [ "### The `break` keyword\n", @@ -678,7 +698,7 @@ { "cell_type": "code", "execution_count": null, - "id": "43", + "id": "45", "metadata": { "tags": [] }, @@ -696,7 +716,7 @@ }, { "cell_type": "markdown", - "id": "44", + "id": "46", "metadata": {}, "source": [ "### The `continue` keyword\n", @@ -707,7 +727,7 @@ { "cell_type": "code", "execution_count": null, - "id": "45", + "id": "47", "metadata": { "tags": [] }, @@ -723,7 +743,7 @@ }, { "cell_type": "markdown", - "id": "46", + "id": "48", "metadata": {}, "source": [ "### `else` after a `for`/`while`\n", @@ -736,7 +756,7 @@ { "cell_type": "code", "execution_count": null, - "id": "47", + "id": "49", "metadata": { "tags": [] }, @@ -754,7 +774,7 @@ }, { "cell_type": "markdown", - "id": "48", + "id": "50", "metadata": {}, "source": [ "Let's see what happens if we change the list to `[1, 2, 3, 4]`:" @@ -763,7 +783,7 @@ { "cell_type": "code", "execution_count": null, - "id": "49", + "id": "51", "metadata": { "tags": [] }, @@ -781,7 +801,7 @@ }, { "cell_type": "markdown", - "id": "50", + "id": "52", "metadata": { "tags": [] }, @@ -791,7 +811,7 @@ }, { "cell_type": "markdown", - "id": "51", + "id": "53", "metadata": {}, "source": [ "Another way of controlling the flow of a program is by **catching exceptions**.\n", @@ -812,7 +832,7 @@ }, { "cell_type": "markdown", - "id": "52", + "id": "54", "metadata": { "tags": [] }, @@ -822,7 +842,7 @@ }, { "cell_type": "markdown", - "id": "53", + "id": "55", "metadata": {}, "source": [ "When you can predict if a certain exception might occur, it's **always a good programming practice** to write what the program should do in that case\n", @@ -833,7 +853,7 @@ { "cell_type": "code", "execution_count": null, - "id": "54", + "id": "56", "metadata": { "tags": [] }, @@ -851,7 +871,7 @@ }, { "cell_type": "markdown", - "id": "55", + "id": "57", "metadata": {}, "source": [ "You can handle **multiple exceptions** at the same time" @@ -860,7 +880,7 @@ { "cell_type": "code", "execution_count": null, - "id": "56", + "id": "58", "metadata": { "tags": [] }, @@ -877,7 +897,7 @@ }, { "cell_type": "markdown", - "id": "57", + "id": "59", "metadata": { "tags": [] }, @@ -901,7 +921,7 @@ { "cell_type": "code", "execution_count": null, - "id": "58", + "id": "60", "metadata": { "tags": [] }, @@ -920,7 +940,7 @@ }, { "cell_type": "markdown", - "id": "59", + "id": "61", "metadata": {}, "source": [ "- We try to open a file called `README.txt`. A `FileNotFoundError` will be raised if it's not found\n", @@ -932,7 +952,7 @@ }, { "cell_type": "markdown", - "id": "60", + "id": "62", "metadata": { "tags": [] }, @@ -943,7 +963,7 @@ { "cell_type": "code", "execution_count": null, - "id": "61", + "id": "63", "metadata": { "tags": [] }, @@ -956,7 +976,7 @@ }, { "cell_type": "markdown", - "id": "62", + "id": "64", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -967,7 +987,7 @@ }, { "cell_type": "markdown", - "id": "63", + "id": "65", "metadata": {}, "source": [ "A factor of a positive integer `n` is any positive integer less than or equal to `n` that divides `n` with no remainder.\n", @@ -986,7 +1006,7 @@ { "cell_type": "code", "execution_count": null, - "id": "64", + "id": "66", "metadata": { "tags": [] }, @@ -1003,7 +1023,7 @@ }, { "cell_type": "markdown", - "id": "65", + "id": "67", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -1014,7 +1034,7 @@ }, { "cell_type": "markdown", - "id": "66", + "id": "68", "metadata": {}, "source": [ "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", @@ -1032,7 +1052,7 @@ }, { "cell_type": "markdown", - "id": "67", + "id": "69", "metadata": { "tags": [] }, @@ -1043,7 +1063,7 @@ { "cell_type": "code", "execution_count": null, - "id": "68", + "id": "70", "metadata": { "tags": [] }, @@ -1060,7 +1080,7 @@ }, { "cell_type": "markdown", - "id": "69", + "id": "71", "metadata": { "tags": [] }, @@ -1070,7 +1090,7 @@ }, { "cell_type": "markdown", - "id": "70", + "id": "72", "metadata": {}, "source": [ "
\n", @@ -1087,7 +1107,7 @@ { "cell_type": "code", "execution_count": null, - "id": "71", + "id": "73", "metadata": { "tags": [] }, @@ -1104,7 +1124,7 @@ }, { "cell_type": "markdown", - "id": "72", + "id": "74", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -1115,7 +1135,7 @@ }, { "cell_type": "markdown", - "id": "73", + "id": "75", "metadata": {}, "source": [ "You have 100 cats.\n", @@ -1142,7 +1162,7 @@ { "cell_type": "code", "execution_count": null, - "id": "74", + "id": "76", "metadata": { "tags": [] }, @@ -1159,7 +1179,7 @@ }, { "cell_type": "markdown", - "id": "75", + "id": "77", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -1170,7 +1190,7 @@ }, { "cell_type": "markdown", - "id": "76", + "id": "78", "metadata": {}, "source": [ "During a winter holidays break, your friends propose to hold a [toboggan](https://en.wikipedia.org/wiki/Toboggan) race. While inspecting the map of the place where you decided to hold the race, you realize that it could be rather dangerous as there are many trees along the slope.\n", @@ -1198,7 +1218,7 @@ }, { "cell_type": "markdown", - "id": "77", + "id": "79", "metadata": {}, "source": [ "#### Part 1 🌶️" @@ -1206,7 +1226,7 @@ }, { "cell_type": "markdown", - "id": "78", + "id": "80", "metadata": {}, "source": [ "
\n", @@ -1217,7 +1237,7 @@ }, { "cell_type": "markdown", - "id": "79", + "id": "81", "metadata": {}, "source": [ "
\n", @@ -1228,7 +1248,7 @@ }, { "cell_type": "markdown", - "id": "80", + "id": "82", "metadata": {}, "source": [ "
\n", @@ -1246,7 +1266,7 @@ { "cell_type": "code", "execution_count": null, - "id": "81", + "id": "83", "metadata": {}, "outputs": [], "source": [ @@ -1284,7 +1304,7 @@ }, { "cell_type": "markdown", - "id": "82", + "id": "84", "metadata": { "tags": [] }, @@ -1294,7 +1314,7 @@ }, { "cell_type": "markdown", - "id": "83", + "id": "85", "metadata": {}, "source": [ "You check other possible slopes to see if you chose the safest one. These are all the possible slopes according to your map:\n", @@ -1313,7 +1333,7 @@ }, { "cell_type": "markdown", - "id": "84", + "id": "86", "metadata": {}, "source": [ "
\n", @@ -1325,7 +1345,7 @@ { "cell_type": "code", "execution_count": null, - "id": "85", + "id": "87", "metadata": {}, "outputs": [], "source": [ diff --git a/tutorial/control_flow.py b/tutorial/control_flow.py new file mode 100644 index 00000000..45a5b177 --- /dev/null +++ b/tutorial/control_flow.py @@ -0,0 +1,87 @@ +from .common import Question, Quiz + + +class ControlFlow(Quiz): + def __init__(self, title=""): + q1 = Question( + question="""What would be the output of the following code? +

+                    if 'blue' in {'red': 1, 'blue': 2, 'green': 3}:
+                        print(1)
+                        print(2)
+                        if 'd' in 'abc':
+                            print(3)
+                    print(4)
+                
+ """, + options={ + "1 2 3 4": "The character 'd' does not exist in the string 'abc', so the second if statement is False.", + "1 2 4": "Correct!", + "4": "The word 'blue' exists in the dictionary's keywords, so the first if statement is True.", + }, + correct_answer="1 2 4", + shuffle=True, + ) + + q2 = Question( + question="""What would be the output of the following code? +

+                    x = 5
+                    if x > 10:
+                        print("A")
+                    else:
+                        if x > 7:
+                            print("B")
+                        else:
+                            print("C")
+                
+ """, + options={ + "A": "This code is using nested if-else statements. Make sure to check all conditions.", + "B": "This code is using nested if-else statements. Make sure to check all conditions. ", + "C": "Correct!", + }, + correct_answer="C", + shuffle=True, + ) + + q3 = Question( + question="""Is the following a syntactically valid statement? +

+                    if x < y: if x > 10: print('something')
+                
+ """, + options={ + "No": "Correct! You cannot write a nested statement in one line.", + "Yes": "Conditionals, when written in one line, can only be simple and not nested.", + }, + correct_answer="No", + shuffle=True, + ) + + q4 = Question( + question="""Is the following a syntactically valid statement? +

+                    min = a if a < b else b
+                
+ """, + options={ + "Yes": "Correct! This is called a ternary operator.", + "No": "It is syntactically correct and is called a ternary operator. It is particularly handy when you want to avoid writing multiple lines for a simple if-else statement.", + }, + correct_answer="Yes", + shuffle=True, + ) + + q5 = Question( + question="""How do you check for multiple conditions in a single if statement?""", + options={ + "if x > 10 and y < 5:": "Correct! Python uses the keyword and.", + "if x > 10 && y < 5:": "This is not a valid Python operator.", + "if x > 10 & y < 5:": "This is not a valid Python operator.", + }, + correct_answer="if x > 10 and y < 5:", + shuffle=True, + ) + + super().__init__(questions=[q1, q2, q3, q4, q5]) From 501ec0ee6b3ccb93a30ae270ac39416983e4d3dd Mon Sep 17 00:00:00 2001 From: Despina Adamopoulou Date: Fri, 3 May 2024 14:45:40 +0200 Subject: [PATCH 4/7] loops quiz first draft --- control_flow.ipynb | 92 ++++++++++++++++++++++++---------------- tutorial/control_flow.py | 85 ++++++++++++++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 37 deletions(-) diff --git a/control_flow.ipynb b/control_flow.ipynb index f3c70e35..d11e78b3 100644 --- a/control_flow.ipynb +++ b/control_flow.ipynb @@ -28,6 +28,7 @@ " - [The `break` keyword](#The-break-keyword)\n", " - [The `continue` keyword](#The-continue-keyword)\n", " - [`else` after a `for`/`while`](#else-after-a-for/while)\n", + "- [Quiz on loops](#Quiz-on-loops)\n", "- [Exceptions](#Exceptions)\n", " - [The `try-except` block](#The-try-except-block)\n", "- [Exercises](#Exercises)\n", @@ -155,7 +156,7 @@ "outputs": [], "source": [ "from tutorial import control_flow as quiz\n", - "quiz.ControlFlow()" + "quiz.ConditionalsQuiz()" ] }, { @@ -802,6 +803,25 @@ { "cell_type": "markdown", "id": "52", + "metadata": {}, + "source": [ + "## Quiz on loops" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "53", + "metadata": {}, + "outputs": [], + "source": [ + "from tutorial import control_flow as quiz\n", + "quiz.LoopsQuiz()" + ] + }, + { + "cell_type": "markdown", + "id": "54", "metadata": { "tags": [] }, @@ -811,7 +831,7 @@ }, { "cell_type": "markdown", - "id": "53", + "id": "55", "metadata": {}, "source": [ "Another way of controlling the flow of a program is by **catching exceptions**.\n", @@ -832,7 +852,7 @@ }, { "cell_type": "markdown", - "id": "54", + "id": "56", "metadata": { "tags": [] }, @@ -842,7 +862,7 @@ }, { "cell_type": "markdown", - "id": "55", + "id": "57", "metadata": {}, "source": [ "When you can predict if a certain exception might occur, it's **always a good programming practice** to write what the program should do in that case\n", @@ -853,7 +873,7 @@ { "cell_type": "code", "execution_count": null, - "id": "56", + "id": "58", "metadata": { "tags": [] }, @@ -871,7 +891,7 @@ }, { "cell_type": "markdown", - "id": "57", + "id": "59", "metadata": {}, "source": [ "You can handle **multiple exceptions** at the same time" @@ -880,7 +900,7 @@ { "cell_type": "code", "execution_count": null, - "id": "58", + "id": "60", "metadata": { "tags": [] }, @@ -897,7 +917,7 @@ }, { "cell_type": "markdown", - "id": "59", + "id": "61", "metadata": { "tags": [] }, @@ -921,7 +941,7 @@ { "cell_type": "code", "execution_count": null, - "id": "60", + "id": "62", "metadata": { "tags": [] }, @@ -940,7 +960,7 @@ }, { "cell_type": "markdown", - "id": "61", + "id": "63", "metadata": {}, "source": [ "- We try to open a file called `README.txt`. A `FileNotFoundError` will be raised if it's not found\n", @@ -952,7 +972,7 @@ }, { "cell_type": "markdown", - "id": "62", + "id": "64", "metadata": { "tags": [] }, @@ -963,7 +983,7 @@ { "cell_type": "code", "execution_count": null, - "id": "63", + "id": "65", "metadata": { "tags": [] }, @@ -976,7 +996,7 @@ }, { "cell_type": "markdown", - "id": "64", + "id": "66", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -987,7 +1007,7 @@ }, { "cell_type": "markdown", - "id": "65", + "id": "67", "metadata": {}, "source": [ "A factor of a positive integer `n` is any positive integer less than or equal to `n` that divides `n` with no remainder.\n", @@ -1006,7 +1026,7 @@ { "cell_type": "code", "execution_count": null, - "id": "66", + "id": "68", "metadata": { "tags": [] }, @@ -1023,7 +1043,7 @@ }, { "cell_type": "markdown", - "id": "67", + "id": "69", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -1034,7 +1054,7 @@ }, { "cell_type": "markdown", - "id": "68", + "id": "70", "metadata": {}, "source": [ "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", @@ -1052,7 +1072,7 @@ }, { "cell_type": "markdown", - "id": "69", + "id": "71", "metadata": { "tags": [] }, @@ -1063,7 +1083,7 @@ { "cell_type": "code", "execution_count": null, - "id": "70", + "id": "72", "metadata": { "tags": [] }, @@ -1080,7 +1100,7 @@ }, { "cell_type": "markdown", - "id": "71", + "id": "73", "metadata": { "tags": [] }, @@ -1090,7 +1110,7 @@ }, { "cell_type": "markdown", - "id": "72", + "id": "74", "metadata": {}, "source": [ "
\n", @@ -1107,7 +1127,7 @@ { "cell_type": "code", "execution_count": null, - "id": "73", + "id": "75", "metadata": { "tags": [] }, @@ -1124,7 +1144,7 @@ }, { "cell_type": "markdown", - "id": "74", + "id": "76", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -1135,7 +1155,7 @@ }, { "cell_type": "markdown", - "id": "75", + "id": "77", "metadata": {}, "source": [ "You have 100 cats.\n", @@ -1162,7 +1182,7 @@ { "cell_type": "code", "execution_count": null, - "id": "76", + "id": "78", "metadata": { "tags": [] }, @@ -1179,7 +1199,7 @@ }, { "cell_type": "markdown", - "id": "77", + "id": "79", "metadata": { "jp-MarkdownHeadingCollapsed": true, "tags": [] @@ -1190,7 +1210,7 @@ }, { "cell_type": "markdown", - "id": "78", + "id": "80", "metadata": {}, "source": [ "During a winter holidays break, your friends propose to hold a [toboggan](https://en.wikipedia.org/wiki/Toboggan) race. While inspecting the map of the place where you decided to hold the race, you realize that it could be rather dangerous as there are many trees along the slope.\n", @@ -1218,7 +1238,7 @@ }, { "cell_type": "markdown", - "id": "79", + "id": "81", "metadata": {}, "source": [ "#### Part 1 🌶️" @@ -1226,7 +1246,7 @@ }, { "cell_type": "markdown", - "id": "80", + "id": "82", "metadata": {}, "source": [ "
\n", @@ -1237,7 +1257,7 @@ }, { "cell_type": "markdown", - "id": "81", + "id": "83", "metadata": {}, "source": [ "
\n", @@ -1248,7 +1268,7 @@ }, { "cell_type": "markdown", - "id": "82", + "id": "84", "metadata": {}, "source": [ "
\n", @@ -1266,7 +1286,7 @@ { "cell_type": "code", "execution_count": null, - "id": "83", + "id": "85", "metadata": {}, "outputs": [], "source": [ @@ -1304,7 +1324,7 @@ }, { "cell_type": "markdown", - "id": "84", + "id": "86", "metadata": { "tags": [] }, @@ -1314,7 +1334,7 @@ }, { "cell_type": "markdown", - "id": "85", + "id": "87", "metadata": {}, "source": [ "You check other possible slopes to see if you chose the safest one. These are all the possible slopes according to your map:\n", @@ -1333,7 +1353,7 @@ }, { "cell_type": "markdown", - "id": "86", + "id": "88", "metadata": {}, "source": [ "
\n", @@ -1345,7 +1365,7 @@ { "cell_type": "code", "execution_count": null, - "id": "87", + "id": "89", "metadata": {}, "outputs": [], "source": [ diff --git a/tutorial/control_flow.py b/tutorial/control_flow.py index 45a5b177..2938409b 100644 --- a/tutorial/control_flow.py +++ b/tutorial/control_flow.py @@ -1,7 +1,7 @@ from .common import Question, Quiz -class ControlFlow(Quiz): +class ConditionalsQuiz(Quiz): def __init__(self, title=""): q1 = Question( question="""What would be the output of the following code? @@ -85,3 +85,86 @@ def __init__(self, title=""): ) super().__init__(questions=[q1, q2, q3, q4, q5]) + + +class LoopsQuiz(Quiz): + def __init__(self, title=""): + q1 = Question( + question="""What would be the output of the following code? +

+                    x = 0
+                    for i in range(10):
+                        for j in range(-1, -10, -1):
+                            x += 1
+                            print(x)
+                
+ """, + options={ + "100": "", + "90": "Correct!", + "99": "", + }, + correct_answer="90", + shuffle=True, + ) + + q2 = Question( + question="""What would be the output of the following code? +

+                    for num in range(-2, -5, -1):
+                        print(num, end=", ")
+                
+ """, + options={ + "-2, -1, -3, -4": "", + "-2, -1, 0": "", + "-2, -3, -4": "Correct!", + }, + correct_answer="-2, -3, -4", + shuffle=True, + ) + + q3 = Question( + question="""What would be the output of the following code? +

+                    var = 10
+                    for i in range(10):
+                        for j in range(2, 10, 1):
+                            if var % 2 == 0:
+                                continue
+                                var += 1
+                        var+=1
+                    else:
+                        var+=1
+                    print(var)
+                
+ """, + options={ + "11": "", + "20": "", + "21": "Correct!", + }, + correct_answer="21", + shuffle=True, + ) + + q4 = Question( + question="""What would be the output of the following code? +

+                    for num in range(10, 14):
+                        for i in range(2, num):
+                            if num % i == 1:
+                                print(num)
+                                break
+                
+ """, + options={ + "10 11 12 13": "Correct!", + "10 11 12": "", + "11 12 13": "", + }, + correct_answer="10 11 12 13", + shuffle=True, + ) + + super().__init__(questions=[q1, q2, q3, q4]) From 8aa046f713e9d4797337f6d6502a4dad7f8575c7 Mon Sep 17 00:00:00 2001 From: Despina Adamopoulou Date: Fri, 3 May 2024 15:49:33 +0200 Subject: [PATCH 5/7] finalize quiz on loops --- tutorial/control_flow.py | 62 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/tutorial/control_flow.py b/tutorial/control_flow.py index 2938409b..89d4cec4 100644 --- a/tutorial/control_flow.py +++ b/tutorial/control_flow.py @@ -7,19 +7,19 @@ def __init__(self, title=""): question="""What would be the output of the following code?

                     if 'blue' in {'red': 1, 'blue': 2, 'green': 3}:
-                        print(1)
-                        print(2)
+                        print(1, end=", ")
+                        print(2, end=", ")
                         if 'd' in 'abc':
-                            print(3)
+                            print(3, end=", ")
                     print(4)
                 
""", options={ - "1 2 3 4": "The character 'd' does not exist in the string 'abc', so the second if statement is False.", - "1 2 4": "Correct!", + "1, 2, 3, 4": "The character 'd' does not exist in the string 'abc', so the second if statement is False.", + "1, 2, 4": "Correct!", "4": "The word 'blue' exists in the dictionary's keywords, so the first if statement is True.", }, - correct_answer="1 2 4", + correct_answer="1, 2, 4", shuffle=True, ) @@ -96,15 +96,15 @@ def __init__(self, title=""): for i in range(10): for j in range(-1, -10, -1): x += 1 - print(x) + print(x) """, options={ - "100": "", - "90": "Correct!", - "99": "", + "9": "Pay attention to the range of the outter loop.", + "18": "Correct!", + "20": "Remember that range() does not include its stop element.", }, - correct_answer="90", + correct_answer="18", shuffle=True, ) @@ -116,11 +116,11 @@ def __init__(self, title=""): """, options={ - "-2, -1, -3, -4": "", - "-2, -1, 0": "", - "-2, -3, -4": "Correct!", + "-2, -3, -4, -5,": "Remember that range() does not include its stop element.", + "-2, -1, 0,": "Pay attention to the step element of range().", + "-2, -3, -4,": "Correct!", }, - correct_answer="-2, -3, -4", + correct_answer="-2, -3, -4,", shuffle=True, ) @@ -133,15 +133,15 @@ def __init__(self, title=""): if var % 2 == 0: continue var += 1 - var+=1 + var += 1 else: - var+=1 + var += 1 print(var) """, options={ - "11": "", - "20": "", + "12": "Pay attention to the range of the outter loop.", + "20": "Pay attention to the else statement.", "21": "Correct!", }, correct_answer="21", @@ -151,19 +151,27 @@ def __init__(self, title=""): q4 = Question( question="""What would be the output of the following code?

-                    for num in range(10, 14):
-                        for i in range(2, num):
-                            if num % i == 1:
-                                print(num)
+                    s = ""
+                    n = 5
+                    while n > 0:
+                        n -= 1
+                        if (n % 2) == 0:
+                            continue
+
+                        a = ['apple', 'mango', 'banana']
+                        while a:
+                            s += str(n) + a.pop(0)
+                            if len(a) < 2:
                                 break
+                    print(s)
                 
""", options={ - "10 11 12 13": "Correct!", - "10 11 12": "", - "11 12 13": "", + "3apple3mango1apple1mango": "Correct!", + "3apple3mango3banana1apple1mango1banana": "Pay attention to the break.", + "3apple3mango1banana": "Remember that list a gets re-initialized in every iteration of the outter loop.", }, - correct_answer="10 11 12 13", + correct_answer="3apple3mango1apple1mango", shuffle=True, ) From c8f6d8d06efe2d74bc42fb3e9488b2e908611287 Mon Sep 17 00:00:00 2001 From: Despina Adamopoulou <16343312+despadam@users.noreply.github.com> Date: Fri, 3 May 2024 17:30:21 +0200 Subject: [PATCH 6/7] Update tutorial/control_flow.py Co-authored-by: Aliaksandr Yakutovich --- tutorial/control_flow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorial/control_flow.py b/tutorial/control_flow.py index 89d4cec4..bd4651d7 100644 --- a/tutorial/control_flow.py +++ b/tutorial/control_flow.py @@ -78,7 +78,7 @@ def __init__(self, title=""): options={ "if x > 10 and y < 5:": "Correct! Python uses the keyword and.", "if x > 10 && y < 5:": "This is not a valid Python operator.", - "if x > 10 & y < 5:": "This is not a valid Python operator.", + "if x > 10 & y < 5:": "& is bitwise AND operator.", }, correct_answer="if x > 10 and y < 5:", shuffle=True, From 54efcc4dd36e6204ad706dfd8b81383675f461f1 Mon Sep 17 00:00:00 2001 From: Despina Adamopoulou Date: Fri, 3 May 2024 17:37:12 +0200 Subject: [PATCH 7/7] fix typos and range in outer loop --- tutorial/control_flow.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tutorial/control_flow.py b/tutorial/control_flow.py index 89d4cec4..77e99e83 100644 --- a/tutorial/control_flow.py +++ b/tutorial/control_flow.py @@ -93,14 +93,14 @@ def __init__(self, title=""): question="""What would be the output of the following code?

                     x = 0
-                    for i in range(10):
+                    for i in range(2):
                         for j in range(-1, -10, -1):
                             x += 1
                     print(x)
                 
""", options={ - "9": "Pay attention to the range of the outter loop.", + "9": "Pay attention to the range of the outer loop.", "18": "Correct!", "20": "Remember that range() does not include its stop element.", }, @@ -140,7 +140,7 @@ def __init__(self, title=""): """, options={ - "12": "Pay attention to the range of the outter loop.", + "12": "Pay attention to the range of the outer loop.", "20": "Pay attention to the else statement.", "21": "Correct!", }, @@ -169,7 +169,7 @@ def __init__(self, title=""): options={ "3apple3mango1apple1mango": "Correct!", "3apple3mango3banana1apple1mango1banana": "Pay attention to the break.", - "3apple3mango1banana": "Remember that list a gets re-initialized in every iteration of the outter loop.", + "3apple3mango1banana": "Remember that list a gets re-initialized in every iteration of the outer loop.", }, correct_answer="3apple3mango1apple1mango", shuffle=True,