Skip to content

Instantly share code, notes, and snippets.

@zoeisnowooze
zoeisnowooze / peaks.rs
Last active November 10, 2021 21:29 — forked from rust-play/playground.rs
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 {
@zoeisnowooze
zoeisnowooze / reorder.rs
Last active October 19, 2021 19:45 — forked from rust-play/playground.rs
Code shared from the Rust Playground
#![allow(unused)]
fn reorder<T: Clone>(values: Vec<T>, indices: Vec<usize>) -> Vec<Option<(usize, T)>> {
let mut pairs: Vec<_> = indices.iter().zip(values).collect();
pairs.sort_by_key(|v| v.0);
let mut iter = pairs.iter().peekable();
(0..*pairs.last().unwrap().0 + 1)
.map(|i| match iter.peek() {
Some((j, _)) => {
@zoeisnowooze
zoeisnowooze / is_odious.rs
Created October 11, 2021 19:10 — forked from rust-play/playground.rs
Code shared from the Rust Playground
//! Checks if an integer is an odious number.
/// Determine if an integer is an odious number.
///
/// 14 is an odious number because it has an odd number of 1s in its binary
/// expansion 1110. Conversely, 5 isn't an odious number. It has an even number
/// of 1s in its binary expansion 101 and so is called an evil number.
fn is_odious(num: i64) -> bool {
let mut num = num;
let mut sum = false;