Решение на упр.04 задача 2 от Деян Делчев

Обратно към всички решения

Към профила на Деян Делчев

Резултати

  • 2 точки от тестове
  • 0 бонус точки
  • 2 точки общо
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::cmp::Ordering;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CompareResult {
Less,
Equal,
Greater,
}
trait Compare {
fn compare(&self, other: &Self) -> CompareResult;
}
trait Filter {
fn matches(&self, query: &str) -> bool;
}
trait Aggregate {
type Output;
fn aggregate(items: &[Self]) -> Self::Output
where
Self: Sized;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SortOrder {
Desc,
Asc,
}
fn process_items<T>(items: Vec<T>, query: &str, order: SortOrder) -> (Vec<T>, T::Output)
where
T: Compare + Filter + Aggregate + Clone,
{
let mut filtered_items: Vec<T> = items
.iter()
.cloned()
.filter(|item| item.matches(query))
.collect();
filtered_items.sort_by(|a, b| match order {
SortOrder::Asc => match a.compare(b) {
CompareResult::Less => Ordering::Less,
CompareResult::Equal => Ordering::Equal,
CompareResult::Greater => Ordering::Greater,
},
SortOrder::Desc => match a.compare(b) {
CompareResult::Less => Ordering::Greater,
CompareResult::Equal => Ordering::Equal,
CompareResult::Greater => Ordering::Less,
},
});
let aggregated = T::aggregate(&filtered_items);
(filtered_items, aggregated)
}
#[derive(Clone)]
struct Book {
title: String,
author: String,
year: u32,
rating: f32,
}
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 Filter for Book {
fn matches(&self, query: &str) -> bool {
self.title.contains(query) || self.author.contains(query)
}
}
impl Aggregate for Book {
type Output = f32;
fn aggregate(items: &[Self]) -> Self::Output
where
Self: Sized,
{
items.iter().map(|item| item.rating).sum::<f32>() / 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 memchr v2.7.6
   Compiling syn v2.0.109
   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 futures-task v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-q0lc53/solution)
warning: enum `CompareResult` is never used
 --> src/lib.rs:4:6
  |
4 | enum CompareResult {
  |      ^^^^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: trait `Compare` is never used
  --> src/lib.rs:10:7
   |
10 | trait Compare {
   |       ^^^^^^^

warning: trait `Filter` is never used
  --> src/lib.rs:14:7
   |
14 | trait Filter {
   |       ^^^^^^

warning: trait `Aggregate` is never used
  --> src/lib.rs:18:7
   |
18 | trait Aggregate {
   |       ^^^^^^^^^

warning: enum `SortOrder` is never used
  --> src/lib.rs:26:6
   |
26 | enum SortOrder {
   |      ^^^^^^^^^

warning: function `process_items` is never used
  --> src/lib.rs:31:4
   |
31 | fn process_items<T>(items: Vec<T>, query: &str, order: SortOrder) -> (Vec<T>, T::Output)
   |    ^^^^^^^^^^^^^

warning: struct `Book` is never constructed
  --> src/lib.rs:57:8
   |
57 | struct Book {
   |        ^^^^

warning: `solution` (lib) generated 7 warnings
   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:28:5
   |
26 | enum SortOrder {
   |      --------- variant in this enum
27 |     Desc,
28 |     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 8.72s
     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

История (1 версия и 0 коментара)

Деян качи първо решение на 03.11.2025 15:33 (преди около 1 месеца)