Решение на упр.04 задача 2 от Йосиф Хамед

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

Към профила на Йосиф Хамед

Резултати

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

Код

#[derive(Debug)]
pub struct Book {
title: String,
author: String,
year: i32,
rating: f32,
}
#[derive(PartialEq)]
pub enum SortOrder {
Asc,
Desc,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompareResult {
Less,
Equal,
Greater,
}
pub trait Compare {
fn compare(&self, other: &Self) -> CompareResult;
}
impl Compare for Book {
fn compare(&self, other: &Self) -> CompareResult {
if self.year > other.year {
return CompareResult::Greater;
}
if self.year < other.year {
return CompareResult::Less;
}
return CompareResult::Equal;
}
}
pub trait Filter {
fn matches(&self, query: &str) -> bool;
}
impl Filter for Book {
fn matches(&self, query: &str) -> bool {
return self.title.contains(query) || self.author.contains(query);
}
}
pub trait Aggregate {
type Output;
fn aggregate(items: &[Self]) -> Self::Output
where
Self: Sized;
}
impl Aggregate for Book {
type Output = f32;
fn aggregate(items: &[Self]) -> Self::Output
where
Self: Sized,
{
let l = items.len() as f32;
items.iter().map(|b| b.rating).fold(0.0, |a, b| a + b) / l
}
}
impl Clone for Book {
fn clone(&self) -> Self {
Self {
title: self.title.clone(),
author: self.author.clone(),
year: self.year,
rating: self.rating,
}
}
}
pub fn process_items<T>(
items: Vec<T>,
query: &str,
order: SortOrder,
) -> (Vec<T>, <T as Aggregate>::Output)
where
T: Compare + Filter + Aggregate + Clone,
{
let mut items: Vec<T> = items.iter().filter(|i| i.matches(query)).cloned().collect();
items.sort_by(|a, b| match a.compare(b) {
CompareResult::Less => std::cmp::Ordering::Less,
CompareResult::Equal => std::cmp::Ordering::Equal,
CompareResult::Greater => std::cmp::Ordering::Greater,
});
if order == SortOrder::Desc {
items.reverse();
}
let o = T::aggregate(&items);
return (items, o);
}

Лог от изпълнението

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling quote v1.0.41
   Compiling unicode-ident v1.0.22
   Compiling futures-core v0.3.31
   Compiling futures-sink v0.3.31
   Compiling futures-channel v0.3.31
   Compiling pin-utils v0.1.0
   Compiling futures-task v0.3.31
   Compiling syn v2.0.109
   Compiling futures-io v0.3.31
   Compiling slab v0.4.11
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251106-1757769-1yyvfvw/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:11:5
   |
10 | pub enum SortOrder {
   |          --------- variant in this enum
11 |     Asc,
   |     ^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `solution` (test "solution_test") generated 1 warning
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.54s
     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 коментара)

Йосиф качи първо решение на 04.11.2025 02:42 (преди около 1 месеца)