feat: add theory guides (terminology, algorithms, testing, debugging) and 2 new chapters
This commit is contained in:
1 parent
ae4c421815
commit
0ac80d60d4
14 files changed
+380
-3
No files matched your search
@@ -0,0 +1,14 @@
|
||||
# 11 — Algorytmy i Złożoność Obliczeniowa (`O(log n)`, `O(n)`)
|
||||
|
||||
Teoria: [notes/03-algorithms-and-complexity.md](../../../notes/03-algorithms-and-complexity.md)
|
||||
|
||||
## Zadania (`algorithms.py`)
|
||||
1. `binary_search(sorted_nums: list[int], target: int) -> int | None` — wyszukiwanie binarne w `O(log n)` (zwróć indeks lub `None`).
|
||||
2. `two_sum(nums: list[int], target: int) -> tuple[int, int] | None` — znajdź parę indeksów `(i, j)` (`i < j`) dającą `nums[i] + nums[j] == target` w czasie `O(n)` przy pomocy `dict`.
|
||||
3. `is_palindrome(text: str) -> bool` — sprawdź palindrom metodą **dwóch wskaźników** (*two pointers*), ignorując wielkość liter i znaki niebędące literami/cyframi.
|
||||
4. `max_window_sum(nums: list[int], k: int) -> int | None` — maksymalna suma spójnego podciągu długości `k` metodą **okna przesuwnego** (*sliding window*) w `O(n)`.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=01-core/11-algorithms
|
||||
```
|
||||
@@ -0,0 +1,14 @@
|
||||
def binary_search(sorted_nums: list[int], target: int) -> int | None:
|
||||
raise NotImplementedError("Wyszukiwanie binarne O(log n): zwróć indeks lub None")
|
||||
|
||||
|
||||
def two_sum(nums: list[int], target: int) -> tuple[int, int] | None:
|
||||
raise NotImplementedError("Znajdź parę indeksów (i, j) w O(n) używając dict")
|
||||
|
||||
|
||||
def is_palindrome(text: str) -> bool:
|
||||
raise NotImplementedError("Dwa wskaźniki (two pointers), tylko znaki alfanumeryczne, lowercase")
|
||||
|
||||
|
||||
def max_window_sum(nums: list[int], k: int) -> int | None:
|
||||
raise NotImplementedError("Okno przesuwne (sliding window) O(n); None gdy k <= 0 lub k > len")
|
||||
@@ -0,0 +1,25 @@
|
||||
from algorithms import binary_search, is_palindrome, max_window_sum, two_sum
|
||||
|
||||
|
||||
def test_binary_search() -> None:
|
||||
assert binary_search([], 5) is None
|
||||
assert binary_search([1, 3, 5, 7, 9], 7) == 3
|
||||
assert binary_search([1, 3, 5, 7, 9], 4) is None
|
||||
|
||||
|
||||
def test_two_sum() -> None:
|
||||
assert two_sum([2, 7, 11, 15], 9) == (0, 1)
|
||||
assert two_sum([3, 2, 4], 6) == (1, 2)
|
||||
assert two_sum([1, 2, 3], 99) is None
|
||||
|
||||
|
||||
def test_is_palindrome() -> None:
|
||||
assert is_palindrome("Kobyła ma mały bok") is True
|
||||
assert is_palindrome("A man, a plan, a canal: Panama") is True
|
||||
assert is_palindrome("devops") is False
|
||||
|
||||
|
||||
def test_max_window_sum() -> None:
|
||||
assert max_window_sum([2, 1, 5, 1, 3, 2], 3) == 9
|
||||
assert max_window_sum([1, 2], 3) is None
|
||||
assert max_window_sum([1, 2], 0) is None
|
||||
@@ -0,0 +1,18 @@
|
||||
# 05 — Rodzaje Testów: Jednostkowe, Integracyjne i Mutacyjne
|
||||
|
||||
Teoria: [notes/04-testing-pyramid-and-types.md](../../../notes/04-testing-pyramid-and-types.md)
|
||||
|
||||
W tym rozdziale ćwiczysz **3 warstwy testowania** i sprawdzasz, jak pisać testy odporne na mutanty (*mutation testing*).
|
||||
|
||||
## Zadania (`testing_types.py` + `test_testing_types.py`)
|
||||
1. **Unit (logika z warunkami brzegowymi):** zaimplementuj `is_within_rate_limit(current_requests: int, limit: int) -> bool` (zwraca `True`, gdy `current_requests <= limit` oraz `limit > 0`, a dla ujemnego `current_requests` rzuca `ValueError`).
|
||||
2. **Integration (prawdziwy system plików):** zaimplementuj `save_and_load_config(path: Path, data: dict[str, str]) -> dict[str, str]`, które zapisuje JSON do pliku na dysku i odczytuje go z powrotem.
|
||||
3. **Mutation Testing (eksperyment ręczny):** po tym, jak wszystkie testy przejdą:
|
||||
* zmień na chwilę w `testing_types.py` operator `<=` na `<` (to jest **mutant**!),
|
||||
* uruchom `make test MOD=02-engineering-2026/05-testing-types-and-mutation`,
|
||||
* upewnij się, że co najmniej jeden test **wykrył i zabił mutanta** (zakończył się błędem), a potem cofnij zmianę.
|
||||
|
||||
## Uruchomienie testów
|
||||
```bash
|
||||
make test MOD=02-engineering-2026/05-testing-types-and-mutation
|
||||
```
|
||||
@@ -0,0 +1,27 @@
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from testing_types import is_within_rate_limit, save_and_load_config
|
||||
|
||||
|
||||
# --- 1. Testy jednostkowe + granice zabijające mutanty (<= vs <, > 0 vs >= 0) ---
|
||||
def test_rate_limit_boundaries_kill_mutants() -> None:
|
||||
assert is_within_rate_limit(9, 10) is True
|
||||
assert is_within_rate_limit(10, 10) is True # Zabija mutanta `<=` -> `<`
|
||||
assert is_within_rate_limit(11, 10) is False
|
||||
assert is_within_rate_limit(0, 0) is False # Zabija mutanta `limit > 0` -> `>= 0`
|
||||
assert is_within_rate_limit(0, -1) is False
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
is_within_rate_limit(-1, 10)
|
||||
|
||||
|
||||
# --- 2. Test integracyjny (prawdziwy zapis i odczyt z systemu plików) ---
|
||||
def test_save_and_load_config_integration(tmp_path: Path) -> None:
|
||||
cfg_file = tmp_path / "service-config.json"
|
||||
payload = {"env": "prod", "region": "eu-central-1"}
|
||||
|
||||
loaded = save_and_load_config(cfg_file, payload)
|
||||
|
||||
assert cfg_file.exists()
|
||||
assert loaded == payload
|
||||
@@ -0,0 +1,15 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def is_within_rate_limit(current_requests: int, limit: int) -> bool:
|
||||
"""Sprawdza, czy liczba zapytań mieści się w limicie (current_requests <= limit)."""
|
||||
raise NotImplementedError(
|
||||
"ValueError dla current_requests < 0; False dla limit <= 0; inaczej current <= limit"
|
||||
)
|
||||
|
||||
|
||||
def save_and_load_config(path: Path, data: dict[str, str]) -> dict[str, str]:
|
||||
"""Zapisuje słownik jako JSON pod path i odczytuje go z powrotem z dysku."""
|
||||
_ = json
|
||||
raise NotImplementedError("Zapisz json.dumps(data) do path i odczytaj przez json.loads")
|
||||
Reference in new issue
Block a user