Text processing/1: Difference between revisions

m
Added Sidef
m (Undo revision 218797 by Trizen (talk))
m (Added Sidef)
Line 3,208:
</pre>
Though it is easier to show when the consecutive false readings ends, if longest run is the last thing in the file, it hasn't really "ended".
 
=={{header|Sidef}}==
{{trans|Perl 6}}
<lang ruby>var gaps = [];
var previous = :valid;
 
ARGF.each { |line|
var (date, *readings) = line.words...;
var valid = [];
var hour = 0;
readings.map{.to_n}.each_slice(2, { |slice|
var(reading, flag) = slice...;
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}}.");</lang>
{{out}}
<pre>
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.
</pre>
''Output is from the sample of the task.''
 
=={{header|Tcl}}==
2,747

edits