|
| 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). |
0 commit comments