Решение на упр.10 задача 1 от Георги Илиев

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

Към профила на Георги Илиев

Резултати

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

Код

use std::thread;
#[derive(Clone, Copy)]
enum Predicate {
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
fn matches_predicate(x: i32, p: Predicate) -> bool {
match p {
Predicate::GreaterThan100 => x > 100,
Predicate::Even => x % 2 == 0,
Predicate::DivisibleBy7 => x % 7 == 0,
Predicate::Prime => {
if x <= 1 {
return false;
}
for a in 2..x {
if x % a == 0 {
return false;
}
}
true
}
}
}
fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
let chunk_size = (data.len() + n_threads - 1) / n_threads;
let mut thread_vec = Vec::new();
for chunk in data.chunks(chunk_size) {
let chunk_data = chunk.to_vec();
let pred = predicate;
let thread = thread::spawn(move || {
chunk_data
.into_iter()
.filter(|&x| matches_predicate(x, pred))
.collect::<Vec<i32>>()
});
thread_vec.push(thread);
}
let mut result = Vec::new();
for thread in thread_vec {
let mut partial = thread.join().unwrap();
result.append(&mut partial);
}
result
}

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

Updating crates.io index
     Locking 17 packages to latest compatible versions
   Compiling proc-macro2 v1.0.103
   Compiling quote v1.0.42
   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 memchr v2.7.6
   Compiling syn v2.0.111
   Compiling slab v0.4.11
   Compiling futures-task v0.3.31
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling pin-utils v0.1.0
   Compiling solution v0.1.0 (/tmp/d20251218-1757769-1pyglh7/solution)
warning: enum `Predicate` is never used
 --> src/lib.rs:4:6
  |
4 | enum Predicate {
  |      ^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `matches_predicate` is never used
  --> src/lib.rs:11:4
   |
11 | fn matches_predicate(x: i32, p: Predicate) -> bool {
   |    ^^^^^^^^^^^^^^^^^

warning: function `parallel_filter` is never used
  --> src/lib.rs:30:4
   |
30 | fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
   |    ^^^^^^^^^^^^^^^

warning: `solution` (lib) generated 3 warnings
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.78s
     Running tests/solution_test.rs (target/debug/deps/solution_test-ee0783488e12dce9)

running 2 tests
test solution_test::test_basic ... ok
test solution_test::test_uneven_split ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

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

Георги качи първо решение на 17.12.2025 23:58 (преди 1 ден)