Skip to content

Instantly share code, notes, and snippets.

@zoeisnowooze
Forked from rust-play/playground.rs
Last active November 10, 2021 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoeisnowooze/f8f8829283fff597dd5607cb6f432127 to your computer and use it in GitHub Desktop.
Save zoeisnowooze/f8f8829283fff597dd5607cb6f432127 to your computer and use it in GitHub Desktop.
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