Browse Source

Rough workup with regex..

drop back to single line of input
iterate through captures, then iterate through sub-captures
move integer parsing out to a closure
move median calculation within iteration out to a closure
regex
Jim Infield 3 years ago
parent
commit
17056b1b6f
  1. 35
      Cargo.lock
  2. 1
      Cargo.toml
  3. 54
      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"

54
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,11 +10,57 @@ 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 = "01|15|59, 1|47|16.35, 1|20|30, 1|43|03";
let hms = Regex::new(r"(\d\d?)\|(\d\d)\|(\d\d)").unwrap();
let csv = Regex::new(r", ").unwrap();
let parse_int = |m: Option<Match>| {
m.unwrap().as_str().parse::<usize>().unwrap()
};
let mut vec = vec![];
for cap in hms.captures_iter(test) {
let mut col = cap.iter().skip(1)
.map(|e| parse_int(e)).collect::<Vec<usize>>();
vec.push(col[0] * 3600 + col[1] * 60 + col[2]);
}
let median = |v: Vec<usize>| {
if v.len() % 2 == 1 {
v[v.len()/2 + 1]
} else {
(v[v.len()/2] + v[v.len()/2 + 1])/2
}
};
vec.sort();
let len = vec.len();
let tot = vec.iter().sum();
let mut list = vec![
( "Total", tot ),
( "Range", vec[len-1] - vec[0] ),
( "Mean", tot/len ),
( "Median", median(vec) )
];
for i in list {
print!(" {}: {:02}|{:02}|{:02}",
i.0, i.1/3600, i.1%3600/60, i.1%60
);
}
println!("\n");
} }
fn stati(s: &str) -> String { fn stati(s: &str) -> String {

Loading…
Cancel
Save