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

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

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

Резултати

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

Код

use std::thread;
enum Predicate {
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
fn is_predicate(n: i32, predicate: &Predicate) -> bool {
match predicate {
Predicate::GreaterThan100 => n > 100,
Predicate::Even => n % 2 == 0,
Predicate::Prime => {
for i in 2..((n as f64).sqrt().floor() as i32) {
if n % i == 0 {
return false;
}
}
true
}
Predicate::DivisibleBy7 => n % 7 == 0,
}
}
fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
thread::scope(|s| {
let mut handlers = Vec::new();
for chunk in data.chunks(n_threads) {
// let predicate = ;
let handler = s.spawn(|| {
chunk
.iter()
.cloned()
.filter(|n| is_predicate(*n, &predicate))
.collect::<Vec<i32>>()
});
handlers.push(handler);
}
let mut result = Vec::new();
for handler in handlers {
result.append(&mut handler.join().unwrap())
}
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-sink v0.3.31
   Compiling futures-core v0.3.31
   Compiling futures-channel v0.3.31
   Compiling slab v0.4.11
   Compiling syn v2.0.111
   Compiling pin-utils v0.1.0
   Compiling futures-task v0.3.31
   Compiling memchr v2.7.6
   Compiling futures-io v0.3.31
   Compiling pin-project-lite v0.2.16
   Compiling solution v0.1.0 (/tmp/d20251218-1757769-1tvzhk1/solution)
warning: enum `Predicate` is never used
 --> src/lib.rs:3:6
  |
3 | enum Predicate {
  |      ^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `is_predicate` is never used
  --> src/lib.rs:10:4
   |
10 | fn is_predicate(n: i32, predicate: &Predicate) -> bool {
   |    ^^^^^^^^^^^^

warning: function `parallel_filter` is never used
  --> src/lib.rs:26:4
   |
26 | 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.53s
     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.01s

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

Деян качи първо решение на 13.12.2025 20:30 (преди около 2 месеца)