@ -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 {