@ -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) ]