feat: add Go theory guides (terminology, algorithms, testing, debugging) and 2 new chapters

This commit is contained in:
soda64 committed 2026-11-30 23:40:23 +01:00
1 parent 84be3e5d77
commit 011f23e637
13 files changed
+374 -3

No files matched your search

+14
View File
@@ -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.go`)
1. `BinarySearch(sorted []int, target int) (int, bool)` — wyszukiwanie binarne w `O(log n)` (zwróć indeks i `true`, lub `-1, false`).
2. `TwoSum(nums []int, target int) (int, int, bool)` — znajdź parę indeksów `(i, j)` (`i < j`) dającą `nums[i] + nums[j] == target` w czasie `O(n)` przy pomocy `map[int]int`.
3. `IsPalindrome(text string) bool` — metoda **dwóch wskaźników** (*two pointers*) na `[]rune`, ignorując wielkość liter i znaki niealfanumeryczne (`unicode.IsLetter`, `unicode.IsDigit`, `unicode.ToLower`).
4. `MaxWindowSum(nums []int, k int) (int, bool)` — maksymalna suma spójnego podciągu długości `k` metodą **okna przesuwnego** (*sliding window*) w `O(n)`.
## Uruchomienie testów
```bash
make test PKG=./exercises/01-core/11-algorithms
```
@@ -0,0 +1,17 @@
package algorithms
func BinarySearch(sorted []int, target int) (int, bool) {
panic("TODO: wyszukiwanie binarne O(log n): zwróć (indeks, true) lub (-1, false)")
}
func TwoSum(nums []int, target int) (int, int, bool) {
panic("TODO: znajdź (i, j, true) w O(n) używając map[int]int")
}
func IsPalindrome(text string) bool {
panic("TODO: dwa wskaźniki na []rune, ignoruj wielkość liter i znaki niealfanumeryczne")
}
func MaxWindowSum(nums []int, k int) (int, bool) {
panic("TODO: okno przesuwne O(n); zwróć (0, false) gdy k <= 0 lub k > len(nums)")
}
@@ -0,0 +1,49 @@
package algorithms
import "testing"
func TestBinarySearch(t *testing.T) {
if _, ok := BinarySearch([]int{}, 5); ok {
t.Fatal("expected false for empty slice")
}
if idx, ok := BinarySearch([]int{1, 3, 5, 7, 9}, 7); !ok || idx != 3 {
t.Fatalf("got (%d, %v), want (3, true)", idx, ok)
}
if _, ok := BinarySearch([]int{1, 3, 5, 7, 9}, 4); ok {
t.Fatal("expected false for missing element")
}
}
func TestTwoSum(t *testing.T) {
i, j, ok := TwoSum([]int{2, 7, 11, 15}, 9)
if !ok || i != 0 || j != 1 {
t.Fatalf("got (%d, %d, %v), want (0, 1, true)", i, j, ok)
}
if _, _, ok := TwoSum([]int{1, 2, 3}, 99); ok {
t.Fatal("expected false when no pair exists")
}
}
func TestIsPalindrome(t *testing.T) {
if !IsPalindrome("Kobyła ma mały bok") {
t.Error("expected Polish UTF-8 palindrome to be true")
}
if !IsPalindrome("A man, a plan, a canal: Panama") {
t.Error("expected classic palindrome to be true")
}
if IsPalindrome("kubernetes") {
t.Error("expected false for kubernetes")
}
}
func TestMaxWindowSum(t *testing.T) {
if got, ok := MaxWindowSum([]int{2, 1, 5, 1, 3, 2}, 3); !ok || got != 9 {
t.Fatalf("got (%d, %v), want (9, true)", got, ok)
}
if _, ok := MaxWindowSum([]int{1, 2}, 3); ok {
t.Fatal("expected false when k > len")
}
if _, ok := MaxWindowSum([]int{1, 2}, 0); ok {
t.Fatal("expected false when k <= 0")
}
}