Compare commits

...

4 Commits

Author SHA1 Message Date
Jim Infield 1f687d718f function ready for submission 3 years ago
Jim Infield ab29927252 switch from h,m,s captures to simple non-digit split 3 years ago
Jim Infield 9bea5bc12c refined regex workup 3 years ago
Jim Infield 17056b1b6f Rough workup with regex.. 3 years ago
  1. 35
      Cargo.lock
  2. 1
      Cargo.toml
  3. 90
      src/main.rs

35
Cargo.lock

@ -2,6 +2,41 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "cw_rust_athletic-statistics" name = "cw_rust_athletic-statistics"
version = "0.1.0" version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "memchr"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"

1
Cargo.toml

@ -6,3 +6,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
regex = "1"

90
src/main.rs

@ -1,5 +1,7 @@
#![allow(unused)] #![allow(unused)]
fn main() { fn main() {
use regex::Regex;
use regex::Match;
let args = vec![ let args = vec![
"01|15|59, 1|47|16, 01|17|20, 1|32|34, 2|17|17", "01|15|59, 1|47|16, 01|17|20, 1|32|34, 2|17|17",
@ -8,15 +10,95 @@ fn main() {
println!(); println!();
for arg in args {
println!("{:?}\n{:?}\n", arg, stati(arg));
};
// for arg in args {
// println!("{:?}\n{:?}\n", arg, stati(arg));
// };
println!(); println!();
let test = args[0];
let hms = Regex::new(r"(\d\d?)\|(\d\d)\|(\d\d)").unwrap();
let csv = Regex::new(r", ").unwrap();
let dig = Regex::new(r"\D+").unwrap();
let parse_int = |m: Option<Match>| {
m.unwrap().as_str().parse::<usize>().unwrap()
};
// let mut vec =
// hms.captures_iter(test)
// .map(|cap|
// cap.iter().skip(1)
// .map(parse_int)
// .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>>();
let mut vec =
dig.split(test)
.map(|c| c.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>>();
vec.sort_unstable();
let (len, tot) = (vec.len(), vec.iter().sum::<usize>());
let (avg, mid, rng) = (tot/len, len/2, vec[len-1] - vec[0]);
let med = if len % 2 == 1 { vec[mid] }
else { (vec[mid-1] + vec[mid]) / 2 };
let time = |s| {
format!("{:02}|{:02}|{:02}", s/3600, s%3600/60, s%60)
};
println!("\n{}", test);
println!("Range: {} Average: {} Median: {}",
time(rng), time(avg), time(med)
);
println!("\n");
} }
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