-
-
Save rust-play/1db28c7115b6bc566176ae5e5800f415 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(unused)] | |
fn peaks<T: std::cmp::PartialOrd>(values: Vec<T>) -> Vec<usize> { | |
values | |
.windows(3) | |
.enumerate() | |
.filter_map(|(i, window)| { | |
if window[0] < window[1] && window[1] > window[2] { | |
Some(i + 1) | |
} else { | |
None | |
} | |
}) | |
.collect() | |
} | |
#[test] | |
fn single_peak() { | |
let values = vec![1, 2, 3, 1]; | |
assert_eq!(peaks(values), [2]); | |
} | |
#[test] | |
fn multiple_peaks() { | |
let values = vec![1, 3, 2, 3, 5, 6, 4]; | |
assert_eq!(peaks(values), [1, 5]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment