Skip to content

Instantly share code, notes, and snippets.

@zoeisnowooze
Forked from rust-play/playground.rs
Last active Nov 10, 2021
Embed
What would you like to do?
Code shared from the Rust Playground
#![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