Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions arithmetic_progression.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,56 @@
// https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=9729edf14743236bf3e936225c8d7880
#![feature(iter_map_windows)]

use itertools::Itertools;
use std::{fmt::Debug, ops::Sub};
use core::{fmt::Debug, ops::Sub, cmp::Ordering};

fn is_arithmetic_progression(
nums: impl IntoIterator<Item: Copy + PartialOrd + Sub<Output: PartialEq>>,
nums: impl IntoIterator<Item: Copy + TotalOrd + Sub<Output: PartialEq>>,
) -> bool {
nums.into_iter()
.sorted_by(|a, b| a.partial_cmp(b).unwrap())
.sorted_by(TotalOrd::total_cmp)
.map_windows(|&[a, b]| b - a)
.all_equal()
}

trait TotalOrd {
fn total_cmp(&self, rhs: &Self) -> Ordering;
}

impl<T: TotalOrd> TotalOrd for &T {
fn total_cmp(&self, rhs: &Self) -> Ordering {
(**self).total_cmp(*rhs)
}
}

macro_rules! impl_float {
($($ty:ty)+) => {
$(
impl TotalOrd for $ty {
fn total_cmp(&self, rhs: &Self) -> Ordering {
<$ty>::total_cmp(self, rhs)
}
}
)+
}
}

impl_float!(f32 f64);

macro_rules! impl_int {
($($ty:ty)+) => {
$(
impl TotalOrd for $ty {
fn total_cmp(&self, rhs: &Self) -> Ordering {
<$ty>::cmp(self, rhs)
}
}
)+
}
}

impl_int!(i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize);

fn print_result(example: usize, nums: impl Debug, result: bool) {
print!("Example {example}:\nInput: {nums:?}\nOutput: {result}\n");
}
Expand Down