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:
Output is from the sample of the task.
Last updated
Was this helpful?