From 8024399deeabbd1e6fd4e1e267f163d87e88b8f4 Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Wed, 18 Aug 2021 18:10:26 -0500 Subject: [PATCH] function ready for submission --- src/main.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index cc0730c..5b40d29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,32 @@ fn main() { } fn stati(s: &str) -> String { - s.into() + let mut t = + s.split(", ") + .map( + |m| m.split('|') + .map(|e| e.parse::().unwrap()) + .collect::>() + .chunks(3) + .map(|c| (c[0],c[1],c[2])) + .map(|(h,m,s)| h*3600 + m*60 + s) + .sum() + ) + .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)]