Skip to content

Commit 783954f

Browse files
committed
Changed 02 exercise and added a new 04-loops
1 parent 5e0a2e0 commit 783954f

8 files changed

Lines changed: 247 additions & 52 deletions

File tree

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
# 1. Define variable with a name "mystring" and assign it to some string
2-
mystring = "some_string"
3-
4-
# 2. Define variable with a name "myinteger" and assign it to some integer
5-
myinteger = 5
6-
7-
# 3. Define variable with a name "myfloat" and assign it to some float
8-
myfloat = 14.5
9-
10-
# 4. Define variable with a name "myboolean" and assign it to some bool
11-
myboolean = True
12-
13-
# 5. Define a variable with a name "mynonevalue" and assign it to None
14-
mynonevalue = None
1+
station_name = "Orion 9"
2+
module_count = 12
3+
signal_frequency = 1420.405
4+
life_support_active = True
5+
backup_generator = None
6+
7+
raw_energy_reading = "100"
8+
energy_level = int(raw_energy_reading)
9+
raw_distance_reading = 45
10+
exact_distance = float(raw_distance_reading)
11+
raw_status_code = 1
12+
is_operational = bool(raw_status_code)
13+
14+
station_status = str(module_count) + " - " + station_name
15+
print(station_status)
Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1-
def test_string_definition(run_script, expect_variable_type):
1+
def test_task1(run_script, expect_variable_type):
22
_, namespace = run_script("datatypes_solution.py")
3-
expect_variable_type(namespace, "mystring", str)
3+
expect_variable_type(namespace, "station_name", str)
4+
expect_variable_type(namespace, "module_count", int)
5+
expect_variable_type(namespace, "signal_frequency", float)
6+
expect_variable_type(namespace, "life_support_active", bool)
7+
expect_variable_type(namespace, "backup_generator", type(None))
48

59

6-
def test_integer_definition(run_script, expect_variable_type):
10+
def test_task2(run_script, expect_variables):
711
_, namespace = run_script("datatypes_solution.py")
8-
expect_variable_type(namespace, "myinteger", int)
9-
10-
11-
def test_float_definition(run_script, expect_variable_type):
12-
_, namespace = run_script("datatypes_solution.py")
13-
expect_variable_type(namespace, "myfloat", float)
14-
15-
16-
def test_boolean_definition(run_script, expect_variable_type):
17-
_, namespace = run_script("datatypes_solution.py")
18-
expect_variable_type(namespace, "myboolean", bool)
19-
20-
21-
def test_none_definition(run_script, expect_variable_type):
22-
_, namespace = run_script("datatypes_solution.py")
23-
expect_variable_type(namespace, "mynonevalue", type(None))
12+
expect_variables(
13+
namespace,
14+
{
15+
"raw_energy_reading": "100",
16+
"energy_level": 100,
17+
"raw_distance_reading": 45,
18+
"exact_distance": 45.0,
19+
"raw_status_code": 1,
20+
"is_operational": True,
21+
},
22+
)
23+
24+
25+
def test_task3(run_script, expect_variable_type, expect_output):
26+
stdout, namespace = run_script("datatypes_solution.py")
27+
expect_variable_type(namespace, "station_status", str)
28+
test_st_mod_count = namespace["module_count"]
29+
test_st_name = namespace["station_name"]
30+
expect_output(stdout, f"{test_st_mod_count} - {test_st_name}")
Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
import pytest
22

33

4-
def test_string_definition(run_script, expect_variable_type):
4+
def test_task1(run_script, expect_variable_type):
55
_, namespace = run_script("datatypes.py")
6-
expect_variable_type(namespace, "mystring", str)
6+
expect_variable_type(namespace, "station_name", str)
7+
expect_variable_type(namespace, "module_count", int)
8+
expect_variable_type(namespace, "signal_frequency", float)
9+
expect_variable_type(namespace, "life_support_active", bool)
10+
expect_variable_type(namespace, "backup_generator", type(None))
711

812

913
@pytest.mark.skip(reason="Finish earlier task first")
10-
def test_integer_definition(run_script, expect_variable_type):
14+
def test_task2(run_script, expect_variables):
1115
_, namespace = run_script("datatypes.py")
12-
expect_variable_type(namespace, "myinteger", int)
16+
expect_variables(
17+
namespace,
18+
{
19+
"raw_energy_reading": "100",
20+
"energy_level": 100,
21+
"raw_distance_reading": 45,
22+
"exact_distance": 45.0,
23+
"raw_status_code": 1,
24+
"is_operational": True,
25+
},
26+
)
1327

1428

1529
@pytest.mark.skip(reason="Finish earlier task first")
16-
def test_float_definition(run_script, expect_variable_type):
17-
_, namespace = run_script("datatypes.py")
18-
expect_variable_type(namespace, "myfloat", float)
19-
20-
21-
@pytest.mark.skip(reason="Finish earlier task first")
22-
def test_boolean_definition(run_script, expect_variable_type):
23-
_, namespace = run_script("datatypes.py")
24-
expect_variable_type(namespace, "myboolean", bool)
25-
26-
27-
@pytest.mark.skip(reason="Finish earlier task first")
28-
def test_none_definition(run_script, expect_variable_type):
29-
_, namespace = run_script("datatypes.py")
30-
expect_variable_type(namespace, "mynonevalue", None)
30+
def test_task3(run_script, expect_variable_type, expect_output):
31+
stdout, namespace = run_script("datatypes.py")
32+
expect_variable_type(namespace, "station_status", str)
33+
test_st_mod_count = namespace["module_count"]
34+
test_st_name = namespace["station_name"]
35+
expect_output(stdout, f"{test_st_mod_count} - {test_st_name}")
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Exercise 04 — Neural Net Breach (Loops & Control Flow)
2+
3+
## Description
4+
You have gained access to a secure mainframe terminal. To breach the inner vaults, you need to automate your exploit scripts using **`while` loops**, **`for` loops**, and loop controls (**`break`** and **`continue`**).
5+
6+
---
7+
8+
## Tasks
9+
10+
### Task 1: Firewall Override (`while` loop)
11+
Crack the security door lock by counting down security layers.
12+
* You are given a variable `security_level = 5`.
13+
* Initialize a string variable `countdown_log = ""`.
14+
* Write a `while` loop that runs as long as `security_level` is **greater than `0`**.
15+
* Inside the loop:
16+
* Convert `security_level` to a string and concatenate it (plus a space) onto `countdown_log`.
17+
* Decrement `security_level` by `1`.
18+
* Result of `countdown_log` should be `"5 4 3 2 1 "`.
19+
20+
> **Next step:** Run pytest. If `test_task_1` passes, open `test_loops.py` and remove the `@pytest.mark.skip` line above `test_task_2`.
21+
22+
---
23+
24+
### Task 2: Power Accumulator (`for` loop with `continue`)
25+
Harvest power units across numbers `1` through `10`, but skip unstable energy frequencies (odd numbers).
26+
* Initialize an integer variable `total_energy = 0`.
27+
* Write a `for` loop using `range(1, 11)`:
28+
* If the number is **odd** (`number % 2 != 0`), use `continue` to skip it.
29+
* Otherwise, add the even number to `total_energy`.
30+
* Result of `total_energy` should be `30` (2 + 4 + 6 + 8 + 10).
31+
32+
> **Next step:** Run pytest to verify your energy total.
33+
34+
---
35+
36+
### Task 3: Vault Decryptor (`for` loop with `break`)
37+
Search through code attempts from `1` to `100` until you hit the bypass trigger.
38+
* You are given a target variable `bypass_code = 7`.
39+
* Initialize an integer variable `attempts_made = 0`.
40+
* Write a `for` loop using `range(1, 101)`:
41+
* Increment `attempts_made` by `1` at the start of each iteration.
42+
* If the current loop number equals `bypass_code`, trigger a `break` immediately to stop scanning.
43+
* Result of `attempts_made` should be `7`.
44+
45+
### Task 4: Signal Pulse Generator (Nested Loops with `range`)
46+
Generate a matrix of beacon pulses for the tracking grid using nested `range()` calls.
47+
* Initialize an empty string variable named `grid_output = ""`.
48+
* Write an outer `for` loop using `range(1, 4)` representing **sector rows** (`1` to `3`).
49+
* Inside it, write an inner `for` loop using `range(1, 4)` representing **beacon columns** (`1` to `3`).
50+
* On each inner iteration, concatenate `f"[{row},{col}]"` onto `grid_output`.
51+
* At the end of each outer loop iteration (after the inner loop completes), concatenate `"\n"` (a newline) onto `grid_output`.
52+
53+
> **Expected result:** `grid_output` will form a 3x3 text grid of sector-beacon coordinates.
54+
55+
### Task 5: Security Lock Pattern Matching (`while` + `continue` + `break`)
56+
Simulate a keypass brute-force attempt where you skip corrupted attempt indices and stop when you match the key.
57+
* You are given `secret_pin = 42` and `current_attempt = 35`.
58+
* Initialize an integer `valid_checks = 0`.
59+
* Write a `while` loop that runs indefinitely (`while True`):
60+
* Increment `current_attempt` by `1`.
61+
* If `current_attempt` is divisible by `5` (e.g., `current_attempt % 5 == 0`), skip it using `continue` (simulating network throttling on multiples of 5).
62+
* Increment `valid_checks` by `1`.
63+
* If `current_attempt == secret_pin`, trigger a `break` immediately.
64+
65+
> **Expected result:** `valid_checks` counts only non-throttled attempts made before hitting pin `42`.
66+
67+
---
68+
69+
## How to Run the Tests
70+
Run the following command from the root folder:
71+
```bash
72+
python check.py 04
73+
```
74+
75+
## Resources
76+
Review the concepts covered in this exercise in [The Python Ledger lesson on Loops](https://thepythonledger.github.io/Docusaurus-engine/lessons/python-foundations/loops).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# write your code here
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Task 1
2+
security_level = 5
3+
countdown_log = ""
4+
while security_level > 0:
5+
security_level_str = str(security_level)
6+
countdown_log += security_level_str + " "
7+
security_level -= 1
8+
9+
# Task 2
10+
total_energy = 0
11+
for energy in range(1, 11):
12+
if energy % 2 != 0:
13+
continue
14+
total_energy += energy
15+
16+
# Task 3
17+
bypass_code = 7
18+
attempts_made = 0
19+
for attempt in range(1, 101):
20+
attempts_made += 1
21+
if attempt == bypass_code:
22+
break
23+
24+
# Task 4
25+
grid_output = ""
26+
for row in range(1, 4):
27+
for col in range(1, 4):
28+
coords = f"[{row},{col}]"
29+
grid_output += coords
30+
grid_output += "/n"
31+
32+
# Task 5
33+
secret_pin = 42
34+
current_attempt = 35
35+
valid_checks = 0
36+
while True:
37+
current_attempt += 1
38+
if current_attempt % 5 == 0:
39+
continue
40+
valid_checks += 1
41+
if current_attempt == secret_pin:
42+
break
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def test_task1(run_script, expect_variables):
2+
_, namespace = run_script("loops_solution.py")
3+
expect_variables(namespace, {"security_level": 0, "countdown_log": "5 4 3 2 1 "})
4+
5+
6+
def test_task2(run_script, expect_variables):
7+
_, namespace = run_script("loops_solution.py")
8+
expect_variables(namespace, {"total_energy": 30})
9+
10+
11+
def test_task3(run_script, expect_variables):
12+
_, namespace = run_script("loops_solution.py")
13+
expect_variables(namespace, {"bypass_code": 7, "attempts_made": 7})
14+
15+
16+
def test_task4(run_script, expect_variables):
17+
_, namespace = run_script("loops_solution.py")
18+
expect_variables(
19+
namespace,
20+
{"grid_output": "[1,1][1,2][1,3]/n[2,1][2,2][2,3]/n[3,1][3,2][3,3]/n"},
21+
)
22+
23+
24+
def test_task5(run_script, expect_variables):
25+
_, namespace = run_script("loops_solution.py")
26+
expect_variables(
27+
namespace, {"secret_pin": 42, "current_attempt": 42, "valid_checks": 6}
28+
)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
4+
def test_task1(run_script, expect_variables):
5+
_, namespace = run_script("loops.py")
6+
expect_variables(namespace, {"security_level": 0, "countdown_log": "5 4 3 2 1 "})
7+
8+
9+
@pytest.mark.skip(reason="Finish task 1 first")
10+
def test_task2(run_script, expect_variables):
11+
_, namespace = run_script("loops.py")
12+
expect_variables(namespace, {"total_energy": 30})
13+
14+
15+
@pytest.mark.skip(reason="Finish task 2 first")
16+
def test_task3(run_script, expect_variables):
17+
_, namespace = run_script("loops.py")
18+
expect_variables(namespace, {"bypass_code": 7, "attempts_made": 7})
19+
20+
21+
@pytest.mark.skip(reason="Finish task 3 first")
22+
def test_task4(run_script, expect_variables):
23+
_, namespace = run_script("loops.py")
24+
expect_variables(
25+
namespace,
26+
{"grid_output": "[1,1],[1,2],[1,3]\n[2,1],[2,2],[2,3]\n[3,1],[3,2],[3,3]"},
27+
)
28+
29+
30+
@pytest.mark.skip(reason="Finish task 4 first")
31+
def test_task5(run_script, expect_variables):
32+
_, namespace = run_script("loops.py")
33+
expect_variables(
34+
namespace, {"secret_pin": 42, "current_attempt": 42, "valid_checks": 6}
35+
)

0 commit comments

Comments
 (0)