Решение на упр.04 задача 2 от Калоян Стоянов
Резултати
- 2 точки от тестове
- 0 бонус точки
- 2 точки общо
- 4 успешни тест(а)
- 0 неуспешни тест(а)
Код
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareResult {
Less,
Equal,
Greater,
}
pub trait Compare {
fn compare(&self, other: &Self) -> CompareResult;
}
pub trait Filter {
fn matches(&self, query: &str) -> bool;
}
pub trait Aggregate {
type Output;
fn aggregate(items: &[Self]) -> Self::Output
where
Self: Sized;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortOrder {
Desc,
Asc,
}
pub fn process_items<T>(items: Vec<T>, query: &str, order: SortOrder) -> (Vec<T>, T::Output)
where
T: Compare + Filter + Aggregate + Clone,
{
let mut filtered: Vec<T> = items.into_iter().filter(|it| it.matches(query)).collect();
let n = filtered.len();
if n > 1 {
for i in 0..n {
let mut swapped = false;
for j in 0..(n - 1 - i) {
let a = &filtered[j];
let b = &filtered[j + 1];
let cmp = a.compare(b);
let should_swap = match order {
SortOrder::Asc => cmp == CompareResult::Greater,
SortOrder::Desc => cmp == CompareResult::Less,
};
if should_swap {
filtered.swap(j, j + 1);
swapped = true;
}
}
if !swapped {
break;
}
}
}
let agg = T::aggregate(&filtered);
(filtered, agg)
}
#[derive(Clone, Debug)]
pub struct Book {
pub title: String,
pub author: String,
pub year: u32,
pub rating: f32,
}
impl Filter for Book {
fn matches(&self, query: &str) -> bool {
let q = query.to_lowercase();
self.title.to_lowercase().contains(&q) || self.author.to_lowercase().contains(&q)
}
}
impl Compare for Book {
fn compare(&self, other: &Self) -> CompareResult {
if self.year < other.year {
CompareResult::Less
} else if self.year > other.year {
CompareResult::Greater
} else {
CompareResult::Equal
}
}
}
impl Aggregate for Book {
type Output = f32;
fn aggregate(items: &[Self]) -> Self::Output {
if items.is_empty() {
0.0
} else {
let sum: f32 = items.iter().map(|b| b.rating).sum();
sum / (items.len() as f32)
}
}
}
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.103
Compiling unicode-ident v1.0.22
Compiling quote v1.0.41
Compiling futures-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling pin-project-lite v0.2.16
Compiling pin-utils v0.1.0
Compiling futures-io v0.3.31
Compiling slab v0.4.11
Compiling syn v2.0.109
Compiling futures-task v0.3.31
Compiling memchr v2.7.6
Compiling solution v0.1.0 (/tmp/d20251106-1757769-1j8030b/solution)
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
warning: variant `Asc` is never constructed
--> tests/../src/lib.rs:26:5
|
24 | pub enum SortOrder {
| --------- variant in this enum
25 | Desc,
26 | Asc,
| ^^^
|
= note: `SortOrder` has derived impls for the traits `Clone` and `Debug`, but these are intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: `solution` (test "solution_test") generated 1 warning
Finished `test` profile [unoptimized + debuginfo] target(s) in 10.11s
Running tests/solution_test.rs (target/debug/deps/solution_test-8c2c5f784503f204)
running 4 tests
test solution_test::test_aggregate ... ok
test solution_test::test_compare ... ok
test solution_test::test_matches ... ok
test solution_test::test_process ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
