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

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

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

Резултати

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

Код

use std::thread;
#[derive(Clone, Copy)]
pub enum Predicate {
GreaterThan100,
Even,
Prime,
DivisibleBy7,
}
pub fn parallel_filter(data: Vec<i32>, n_threads: usize, predicate: Predicate) -> Vec<i32> {
if data.is_empty() || n_threads == 0 {
return Vec::new();
}
let chunk_size = (data.len() + n_threads - 1) / n_threads;
let mut handles = vec![];
for chunk in data.chunks(chunk_size) {
let chunk_vec = chunk.to_vec();
let handle = thread::spawn(move || filter_chunk(chunk_vec, predicate));
handles.push(handle);
}
let mut result = Vec::new();
for handle in handles {
if let Ok(filtered) = handle.join() {
result.extend(filtered);
}
}
result
}
fn filter_chunk(chunk: Vec<i32>, predicate: Predicate) -> Vec<i32> {
chunk
.into_iter()
.filter(|&x| matches_predicate(x, predicate))
.collect()
}
fn matches_predicate(value: i32, predicate: Predicate) -> bool {
match predicate {
Predicate::GreaterThan100 => value > 100,
Predicate::Even => value % 2 == 0,
Predicate::Prime => is_prime(value),
Predicate::DivisibleBy7 => value % 7 == 0,
}
}
fn is_prime(n: i32) -> bool {
// Better approach, but I don't know if it will work with the grader:
// is_prime_(n)
const PRIMES: &[i32] = &[
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229,
233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359,
367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491,
499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641,
643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787,
797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941,
947, 953, 967, 971, 977, 983, 991, 997,
];
PRIMES.contains(&n)
}
fn main() {
}

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

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 futures-io v0.3.31
   Compiling pin-utils v0.1.0
   Compiling syn v2.0.111
   Compiling futures-task v0.3.31
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling slab v0.4.11
   Compiling solution v0.1.0 (/tmp/d20251218-1757769-1x125iv/solution)
warning: function `main` is never used
  --> src/lib.rs:68:4
   |
68 | fn main() {
   |    ^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: `solution` (lib) generated 1 warning
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: function `main` is never used
  --> tests/../src/lib.rs:68:4
   |
68 | fn main() {
   |    ^^^^
   |
   = 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-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 коментара)

Илиян качи първо решение на 14.12.2025 19:09 (преди около 1 месеца)