Skip to content

Commit ed9e130

Browse files
committed
Added task Nested Loops
1 parent be884b3 commit ed9e130

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

Loops/Nested for Loop/__init__.py

Whitespace-only changes.

Loops/Nested for Loop/task-info.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type: edu
2+
files:
3+
- name: task.py
4+
visible: true
5+
placeholders:
6+
- offset: 61
7+
length: 31
8+
placeholder_text: '# TODO: use a nested loop here.'
9+
- name: tests/test_task.py
10+
visible: false
11+
- name: __init__.py
12+
visible: false
13+
- name: tests/__init__.py
14+
visible: false

Loops/Nested for Loop/task.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Nested Loops
2+
3+
A nested loop is a loop inside another loop.
4+
The inner loop is executed once for each iteration of the outer loop.
5+
6+
```python
7+
adjectives = ["black", "stylish", "expensive"]
8+
clothes = ["jacket", "shirt", "boots"]
9+
10+
for x in adjectives:
11+
for y in clothes:
12+
print(x, y)
13+
```
14+
Output:
15+
```text
16+
black jacket
17+
black shirt
18+
black boots
19+
stylish jacket
20+
stylish shirt
21+
stylish boots
22+
expensive jacket
23+
expensive shirt
24+
expensive boots
25+
```
26+
<details>
27+
28+
Note that any type of loop can be nested inside another loop.
29+
For example, a [`while` loop](course://Loops/While loop) (see further) can be nested inside a `for` loop or vice versa.
30+
</details>
31+
32+
33+
You are given a tic-tac-toe board of 3x3, your task is to print every position. Coordinates along each side
34+
are stored in the list `coordinates`. The output should be:
35+
```text
36+
1 x 1
37+
1 x 2
38+
1 x 3
39+
2 x 1
40+
...
41+
```
42+
and so on for every combination of coordinates.
43+
44+
<div class="hint">
45+
46+
In the nested `for` loop, iterate over the same list once again but using another variable name
47+
this time (`coordinate2`).
48+
</div>

Loops/Nested for Loop/task.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
coordinates = [1, 2, 3]
2+
3+
for coordinate1 in coordinates:
4+
for coordinate2 in coordinates:
5+
print(f'{coordinate1} x {coordinate2}')

Loops/Nested for Loop/tests/__init__.py

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
import contextlib
3+
import io
4+
5+
f = io.StringIO()
6+
7+
with contextlib.redirect_stdout(f):
8+
from task import *
9+
output = f.getvalue().split('\n')
10+
11+
12+
class TestCase(unittest.TestCase):
13+
def test_list(self):
14+
expected_output = ['1 x 1', '1 x 2', '1 x 3', '2 x 1', '2 x 2', '2 x 3', '3 x 1', '3 x 2', '3 x 3', '']
15+
self.assertEqual(expected_output, output, msg='Your result does not match the expected.')

0 commit comments

Comments
 (0)