From 3446524d70e155859f9901d9935b0c082752fb7b Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 13 Jun 2025 10:29:45 +0200 Subject: [PATCH] Improvments --- Cargo.toml | 2 +- src/main.rs | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a1175cb..a1d1ba5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peak-and-valley" -author = "Fistons " +authors = ["Fistons "] version = "0.1.0" edition = "2024" diff --git a/src/main.rs b/src/main.rs index f9be344..b82ed7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,12 @@ #[allow(dead_code)] fn find_peaks_and_valleys(values: &[isize]) -> Vec { values - .windows(3) - .enumerate() - .flat_map(|(idx, arr)| { - let (a, b, c) = (arr[0], arr[1], arr[2]); - if (b > a && b > c) || (b < a && b < c) { - Some(idx + 1) - } else { - None - } - }) - .collect() + .windows(3) // Take three consecutives elements at once + .enumerate() // Get the index of the first one + .map(|(idx, arr)| (idx, [arr[0], arr[1], arr[2]])) // Map the elements as a fixed size array + .filter(|(_, [a, b, c])| (b > a && b > c) || (b < a && b < c)) // Keep if the middle element is peak or valley + .map(|(idx, _)| idx + 1) // Map to index of the middle element + .collect() // Profit! } fn main() {}