Skip to content

Commit 73f2109

Browse files
edoardob90despadam
andauthored
Fix errors in Control Flow (#344)
* Fixes - Warmup exercise rewording/reorganization - Quiz text * Fix bugs with Exercise 4 - Correct validation steps - Handle negative numbers if target is decimal - Reverse the final result string, since we built from right to left * fix docstring --------- Co-authored-by: Despina Adamopoulou <despoina.adamopoulou@empa.ch>
1 parent 047f9b4 commit 73f2109

File tree

3 files changed

+34
-24
lines changed

3 files changed

+34
-24
lines changed

02_control_flow.ipynb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
" - [The `enumerate` built-in](#The-enumerate-built-in)\n",
2727
" - [The `range` built-in](#The-range-built-in)\n",
2828
"- [Warm-up Exercises](#Warm-up-Exercises)\n",
29-
" 1. [Write a Python program that returns the characters in a string and their indexes](#1.-Write-a-Python-program-that-returns-the-characters-in-a-string-and-their-indexes)\n",
30-
" 2. [Write a Python program that returns all the numbers in a given range, __including__ the first and the last elements](#2.-Write-a-Python-program-that-returns-all-the-numbers-in-a-given-range,-including-the-first-and-the-last-elements)\n",
31-
" 3. [Write a Python program that takes a list of integers and returns the square root of each of them](#3.-Write-a-Python-program-that-takes-a-list-of-integers-and-returns-the-square-root-of-each-of-them)\n",
32-
" 4. [Write a Python program that takes an integer and divides it by 2 until the result is no longer an even number](#4.-Write-a-Python-program-that-takes-an-integer-and-divides-it-by-2-until-the-result-is-no-longer-an-even-number)\n",
29+
" 1. [Characters in a string](#Characters-in-a-string)\n",
30+
" 2. [Range of integers](#Range-of-integers)\n",
31+
" 3. [List of squares](#List-of-squares)\n",
32+
" 4. [Divide by two](#Divide-by-two)\n",
3333
"- [Altering loops](#Altering-loops)\n",
3434
" - [`if` statement inside `for`/`while`](#if-statement-inside-for/while)\n",
3535
" - [Exercise: conditionals inside loops](#Exercise:-conditionals-inside-loops)\n",
@@ -91,7 +91,7 @@
9191
"id": "5",
9292
"metadata": {},
9393
"source": [
94-
"Python [supports](./01_basic_datatypes.ipynb#Comparison-operators) different comparison expressions.\n",
94+
"Python supports different [comparison expressions](./01_basic_datatypes.ipynb#Comparison-operators).\n",
9595
"They are called **logical expressions** as they evaluate to either `True` or `False`.\n",
9696
"\n",
9797
"We can use these results in **conditional statements**, and have our program behave differently based on the result."
@@ -586,7 +586,8 @@
586586
"id": "31",
587587
"metadata": {},
588588
"source": [
589-
"#### 1. Write a Python program that returns the characters in a string and their indexes\n",
589+
"### Characters in a string\n",
590+
"Write a Python program that returns the characters in a string and their indexes.\n",
590591
"\n",
591592
"\n",
592593
"<div class=\"alert alert-block alert-warning\">\n",
@@ -616,8 +617,8 @@
616617
"\n",
617618
" Returns:\n",
618619
" - A list of tuples where each tuple contains:\n",
619-
" - index (int): The position of the character in the string\n",
620-
" - char (str): The character at that position\n",
620+
" - char (str): A string's character\n",
621+
" - index (int): The position of that character in the string\n",
621622
" \"\"\"\n",
622623
" return"
623624
]
@@ -629,7 +630,9 @@
629630
"tags": []
630631
},
631632
"source": [
632-
"#### 2. Write a Python program that returns all the numbers in a given range, including the first and the last elements\n",
633+
"### Range of integers\n",
634+
"Write a Python program that returns all the numbers in a given range, including the first and the last elements.\n",
635+
"When we say *increasing* or *decreasing*, we mean the **canonical** (or natural) ordering of integers.\n",
633636
"\n",
634637
"<div class=\"alert alert-block alert-warning\">\n",
635638
" <h4><b>Note</b></h4>\n",
@@ -670,15 +673,17 @@
670673
"tags": []
671674
},
672675
"source": [
673-
"#### 3. Write a Python program that takes a list of integers and returns the square root of each of them\n",
676+
"### List of squares\n",
677+
"\n",
678+
"Write a Python program that takes a list of integers and returns the square root of each of them.\n",
674679
"\n",
675680
"<div class=\"alert alert-block alert-info\">\n",
676681
" <h4><b>Hints</b></h4>\n",
677682
" <ul>\n",
678683
" <li>You can use the <code>math.sqrt</code> function to compute the square root of a number</li>\n",
679684
" <li>If a number does not have a square root in the real domain, you should skip it</li>\n",
680685
" </ul>\n",
681-
"</div>"
686+
"</div>\n"
682687
]
683688
},
684689
{
@@ -714,7 +719,9 @@
714719
"id": "37",
715720
"metadata": {},
716721
"source": [
717-
"#### 4. Write a Python program that takes an integer and divides it by 2 until the result is no longer an even number"
722+
"### Divide by two\n",
723+
"\n",
724+
"Write a Python program that takes an integer and divides it by 2 until the result is no longer an even number"
718725
]
719726
},
720727
{
@@ -1933,7 +1940,7 @@
19331940
"name": "python",
19341941
"nbconvert_exporter": "python",
19351942
"pygments_lexer": "ipython3",
1936-
"version": "3.10.15"
1943+
"version": "3.12.12"
19371944
},
19381945
"toc-autonumbering": false,
19391946
"toc-showmarkdowntxt": false,

tutorial/quiz/control_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ def __init__(self, title=""):
77
question="""What would be the output of the following code?
88
<pre><code class="language-python">
99
if 'blue' in {'red': 1, 'blue': 2, 'green': 3}:
10-
print(1, end=", ")
11-
print(2, end=", ")
10+
print(1)
11+
print(2)
1212
if 'd' in 'abc':
13-
print(3, end=", ")
13+
print(3)
1414
print(4)
1515
</code></pre>
1616
""",

tutorial/tests/test_02_control_flow.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,24 +379,24 @@ def reference_base_converter(number: str, from_base: int, to_base: int) -> str:
379379
err = "Invalid empty input"
380380
raise ValueError(err)
381381

382-
# Same to and from bases
383-
if from_base == to_base:
384-
return number
385-
386382
# Handle negative numbers
387383
is_negative = number.strip().startswith("-")
388384
number = number.strip().removeprefix("-")
389385

390-
# Remove spaces and convert to uppercase for consistency
391-
number = number.replace(" ", "").upper()
392-
393386
# Validate digits
394387
valid_digits = "0123456789ABCDEF"
395388
for digit in number:
396389
if digit not in valid_digits[:from_base]:
397390
err = f"Invalid digit '{digit}' for base {from_base}"
398391
raise ValueError(err)
399392

393+
# Same to and from bases
394+
if from_base == to_base:
395+
return number
396+
397+
# Remove spaces and convert to uppercase for consistency
398+
number = number.replace(" ", "").upper()
399+
400400
# Convert to base 10
401401
decimal = 0
402402
for digit in number:
@@ -407,7 +407,7 @@ def reference_base_converter(number: str, from_base: int, to_base: int) -> str:
407407
return "0"
408408

409409
if to_base == 10:
410-
return str(decimal)
410+
return f"-{str(decimal)}" if is_negative else str(decimal)
411411

412412
# Convert to target base
413413
result = ""
@@ -416,6 +416,9 @@ def reference_base_converter(number: str, from_base: int, to_base: int) -> str:
416416
result += valid_digits[digit]
417417
decimal //= to_base
418418

419+
# We built the number from the rightmost digit to leftmost, so we need to reverse the final string
420+
result = result[::-1]
421+
419422
return f"-{result}" if is_negative else result
420423

421424

0 commit comments

Comments
 (0)