Browse Source

Revised working copy...

switch from for loop to iteration chain
cleanup calculation code
output source string
master
Jim Infield 3 years ago
parent
commit
810b7fbf77
  1. 36
      src/main.rs

36
src/main.rs

@ -13,36 +13,36 @@ fn main() {
// }; // };
let strings = args[0]; let strings = args[0];
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::<usize>().unwrap())
.collect();
// convert h,m,s into total seconds
times.push(e[0]*3600 + e[1]*60 + e[2])
}
let mut times =
strings.split(", ")
.map(|s|
s.split('|')
.map(|e| e.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)
.sum()
)
.collect::<Vec<usize>>();
times.sort_unstable(); times.sort_unstable();
let tot = times.iter().sum::<usize>();
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 (tot, len) = (times.iter().sum::<usize>(), times.len());
let (avg, mid, rng) = (tot/len, len/2, times[len-1] - times[0]);
let med = if len % 2 == 1 { times[mid] }
else { (times[mid-1] + times[mid]) / 2 };
let format_time = |s| { let format_time = |s| {
format!("{:02}|{:02}|{:02}", s/3600, s%3600/60, s%60) format!("{:02}|{:02}|{:02}", s/3600, s%3600/60, s%60)
}; };
println!("\n{}", strings);
println!("Range: {} Average: {} Median: {}", println!("Range: {} Average: {} Median: {}",
format_time(rng), format_time(avg), format_time(med) format_time(rng), format_time(avg), format_time(med)
); );
println!(); println!();
} }

Loading…
Cancel
Save