From 1f687d718f42362aede555f453128fed7894abb3 Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Wed, 18 Aug 2021 17:47:19 -0500 Subject: [PATCH] function ready for submission --- src/main.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e3b2a5e..a5aa63a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,7 +68,37 @@ fn main() { } fn stati(s: &str) -> String { - s.into() + use regex::Regex; + use regex::Match; + + let non_digit = Regex::new(r"\D+").unwrap(); + + let to_int = |m: Option| { + m.unwrap().as_str().parse::().unwrap() + }; + + let mut t = + non_digit.split(s) + .map(|m| m.parse::().unwrap()) + .collect::>() + .chunks(3) + .map(|c| (c[0],c[1],c[2])) + .map(|(h,m,s)| h*3600 + m*60 + s) + .collect::>(); + + t.sort_unstable(); + + let (len, tot) = (t.len(), t.iter().sum::()); + let (avg, mid, rng) = (tot/len, len/2, t[len-1] - t[0]); + let med = if len % 2 == 1 { t[mid] } else { (t[mid-1] + t[mid]) / 2}; + + let hms = |i| { + format!("{:02}|{:02}|{:02}", i/3600, i%3600/60, i%60) + }; + + format!("Range: {} Average: {} Median: {}", + hms(rng), hms(avg), hms(med) + ) } #[cfg(test)]