feat: add Rust theory guides (terminology, algorithms, testing, debugging) and 2 new crates
This commit is contained in:
1 parent
2e002b6176
commit
015eae08c3
14 files changed
+357
-5
No files matched your search
@@ -0,0 +1,4 @@
|
||||
[package]
|
||||
name = "core11-algorithms"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
@@ -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 (`src/lib.rs`)
|
||||
1. `binary_search(sorted: &[i32], target: i32) -> Option<usize>` — wyszukiwanie binarne w `O(log n)`.
|
||||
2. `two_sum(nums: &[i32], target: i32) -> Option<(usize, usize)>` — znajdź parę indeksów `(i, j)` (`i < j`) dającą `nums[i] + nums[j] == target` w `O(n)` używając `HashMap`.
|
||||
3. `is_palindrome(text: &str) -> bool` — metoda **dwóch wskaźników** (*two pointers*) na znakach alfanumerycznych (lowercase, obsługa UTF-8).
|
||||
4. `max_window_sum(nums: &[i32], k: usize) -> Option<i32>` — 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=core11-algorithms
|
||||
```
|
||||
@@ -0,0 +1,53 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub fn binary_search(sorted: &[i32], target: i32) -> Option<usize> {
|
||||
todo!("Wyszukiwanie binarne O(log n): zwróć Some(indeks) lub None")
|
||||
}
|
||||
|
||||
pub fn two_sum(nums: &[i32], target: i32) -> Option<(usize, usize)> {
|
||||
let _ = HashMap::<i32, usize>::new();
|
||||
todo!("Znajdź parę indeksów (i, j) w O(n) używając HashMap")
|
||||
}
|
||||
|
||||
pub fn is_palindrome(text: &str) -> bool {
|
||||
todo!("Dwa wskaźniki na znakach alfanumerycznych (to_lowercase, UTF-8)")
|
||||
}
|
||||
|
||||
pub fn max_window_sum(nums: &[i32], k: usize) -> Option<i32> {
|
||||
todo!("Okno przesuwne O(n); None gdy k == 0 lub k > nums.len()")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn finds_with_binary_search() {
|
||||
assert_eq!(binary_search(&[], 5), None);
|
||||
assert_eq!(binary_search(&[1, 3, 5, 7, 9], 7), Some(3));
|
||||
assert_eq!(binary_search(&[1, 3, 5, 7, 9], 4), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn finds_two_sum_in_linear_time() {
|
||||
assert_eq!(two_sum(&[2, 7, 11, 15], 9), Some((0, 1)));
|
||||
assert_eq!(two_sum(&[3, 2, 4], 6), Some((1, 2)));
|
||||
assert_eq!(two_sum(&[1, 2, 3], 99), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checks_utf8_palindrome() {
|
||||
assert!(is_palindrome("Kobyła ma mały bok"));
|
||||
assert!(is_palindrome("A man, a plan, a canal: Panama"));
|
||||
assert!(!is_palindrome("rustacean"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn computes_max_window_sum() {
|
||||
assert_eq!(max_window_sum(&[2, 1, 5, 1, 3, 2], 3), Some(9));
|
||||
assert_eq!(max_window_sum(&[1, 2], 3), None);
|
||||
assert_eq!(max_window_sum(&[1, 2], 0), None);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user