From 25e853c9dea00531faded68d68f0dcc9ee6a41c4 Mon Sep 17 00:00:00 2001 From: Jim Infield Date: Mon, 16 Aug 2021 18:07:07 -0500 Subject: [PATCH] Rough working copy... switch to sort_unstable, per clippy insistance generate time format '00|00|00' separate from output line --- src/main.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index db08396..4040a3a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,18 +16,32 @@ fn main() { let mut times = vec![]; for t in strings.split(", ") { + // split t on '|' and gather ints for h,m,s let e: Vec<_> = t.split('|') - .map(|i| i.parse::().unwrap()) + .map(|i| i.parse::().unwrap()) .collect(); + // convert h,m,s into total seconds times.push(e[0]*3600 + e[1]*60 + e[2]) } - let times_orig = times.clone(); - times.sort(); + + times.sort_unstable(); - println!("\n{:?}\n{:?}\n{:?}\n", - times_orig, - times, - times.iter().sum::(), + let tot = times.iter().sum::(); + let len = times.len(); + let avg = tot/len; + let rng = times[len-1] - times[0]; + let med = if len%2 == 1 { + times[len/2] + } else { + (times[len/2-1] + times[len])/2 + }; + + let format_time = |s| { + format!("{:02}|{:02}|{:02}", s/3600, s%3600/60, s%60) + }; + + println!("Range: {} Average: {} Median: {}", + format_time(rng), format_time(avg), format_time(med) ); println!(); }