Browse Source

function ready for submission

regex
Jim Infield 3 years ago
parent
commit
1f687d718f
  1. 32
      src/main.rs

32
src/main.rs

@ -68,7 +68,37 @@ fn main() {
} }
fn stati(s: &str) -> String { 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<Match>| {
m.unwrap().as_str().parse::<usize>().unwrap()
};
let mut t =
non_digit.split(s)
.map(|m| m.parse::<usize>().unwrap())
.collect::<Vec<usize>>()
.chunks(3)
.map(|c| (c[0],c[1],c[2]))
.map(|(h,m,s)| h*3600 + m*60 + s)
.collect::<Vec<usize>>();
t.sort_unstable();
let (len, tot) = (t.len(), t.iter().sum::<usize>());
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)] #[cfg(test)]

Loading…
Cancel
Save