Text processing/1
var gaps = [];
var previous = :valid;
ARGF.each { |line|
var (date, *readings) = line.words...;
var valid = [];
var hour = 0;
for reading, flag in readings.map{.to_n}.slices(2) {
if (flag > 0) {
valid << reading;
if (previous == :invalid) {
gaps[-1]{:end} = "#{date} #{hour}:00";
previous = :valid;
}
}
else {
if (previous == :valid) {
gaps << Hash(start => "#{date} #{hour}:00");
}
gaps[-1]{:count} := 0 ++;
previous = :invalid;
}
++hour;
}
say ("#{date}: #{ '%8s' % (valid ? ('%.3f' % Math.avg(valid...)) : 0) }",
" mean from #{ '%2s' % valid.len } valid.");
}
var longest = gaps.sort_by{|a| -a{:count} }.first;
say ("Longest period of invalid readings was #{longest{:count}} hours,\n",
"from #{longest{:start}} till #{longest{:end}}.");
Output:
1991-03-30: 10.000 mean from 24 valid.
1991-03-31: 23.542 mean from 24 valid.
1991-03-31: 40.000 mean from 1 valid.
1991-04-01: 23.217 mean from 23 valid.
1991-04-02: 19.792 mean from 24 valid.
1991-04-03: 13.958 mean from 24 valid.
Longest period of invalid readings was 24 hours,
from 1991-03-31 1:00 till 1991-04-01 1:00.
Output is from the sample of the task.