Text processing/1: Difference between revisions

(Added Kotlin)
Line 2,753:
Longest period of invalid readings was 589 hours,
from 1993-02-09 1:00 till 1993-03-05 14:00.
</pre>
 
=={{header|Phix}}==
As the link to the data file is broken, this is limited to the 6 lines shown in the task description for testing purposes.<br>
(first constant shown in a smaller font, it actually fits at 4px but...)
<pre style="font-size: 6px">
constant data="""
1991-03-30 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1
1991-03-31 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 10.000 1 20.000 1 20.000 1 20.000 1 35.000 1 50.000 1 60.000 1 40.000 1 30.000 1 30.000 1 30.000 1 25.000 1 20.000 1 20.000 1 20.000 1 20.000 1 20.000 1 35.000 1
1991-03-31 40.000 1 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2 0.000 -2
1991-04-01 0.000 -2 13.000 1 16.000 1 21.000 1 24.000 1 22.000 1 20.000 1 18.000 1 29.000 1 44.000 1 50.000 1 43.000 1 38.000 1 27.000 1 27.000 1 24.000 1 23.000 1 18.000 1 12.000 1 13.000 1 14.000 1 15.000 1 13.000 1 10.000 1
1991-04-02 8.000 1 9.000 1 11.000 1 12.000 1 12.000 1 12.000 1 27.000 1 26.000 1 27.000 1 33.000 1 32.000 1 31.000 1 29.000 1 31.000 1 25.000 1 25.000 1 24.000 1 21.000 1 17.000 1 14.000 1 15.000 1 12.000 1 12.000 1 10.000 1
1991-04-03 10.000 1 9.000 1 10.000 1 10.000 1 9.000 1 10.000 1 15.000 1 24.000 1 28.000 1 24.000 1 18.000 1 14.000 1 12.000 1 13.000 1 14.000 1 15.000 1 14.000 1 15.000 1 13.000 1 13.000 1 13.000 1 12.000 1 10.000 1 10.000 1"""
</pre>
<lang Phix>constant lines = split(data,'\n')
 
include builtins\timedate.e
 
integer count = 0,
max_count = 0,
ntot = 0
atom readtot = 0
timedate run_start, max_start
 
procedure end_bad_run()
if count then
if count>max_count then
max_count = count
max_start = run_start
end if
count = 0
end if
end procedure
 
for i=1 to length(lines) do
sequence oneline = split(lines[i],no_empty:=true), r
if length(oneline)!=49 then
?"bad line (length!=49)"
else
r = parse_date_string(oneline[1],{"YYYY-MM-DD"})
if not timedate(r) then
?{"bad date",oneline[1]}
else
timedate td = r
integer rejects=0, accepts=0
atom readsum = 0
for j=2 to 48 by 2 do
r = scanf(oneline[j],"%f")
if length(r)!=1 then
?{"error scanning",oneline[j]}
rejects += 1
else
atom reading = r[1][1]
r = scanf(oneline[j+1],"%d")
if length(r)!=1 then
?{"error scanning",oneline[j+1]}
rejects += 1
else
integer flag = r[1][1]
if flag<0 then
if count=0 then
run_start = td
end if
count += 1
rejects += 1
else
end_bad_run()
accepts += 1
readsum += reading
end if
end if
end if
end for
if rejects=0 then
readtot += readsum
ntot += 1
end if
-- readtot += readsum
-- ntot += accepts
printf(1,"Date: %s, Rejects: %2d, Accepts: %2d, Line total: %7.3f, Average %6.3f\n",
{format_timedate(td,"DD/MM/YYYY"),rejects, accepts, readsum, readsum/accepts})
end if
end if
end for
 
printf(1,"Average: %.3f (of %d entirely valid days)\n",{readtot/(24*ntot),ntot})
--printf(1,"Average: %.3f\n",{readtot/ntot})
end_bad_run()
if max_count then
printf(1,"Maximum run of %d consecutive false readings starting: %s\n",
{max_count,format_timedate(max_start,"DD/MM/YYYY")})
end if</lang>
I opted to show an average of entirely good days; the commented out alternative matches Kotlin (18.242).
{{out}}
<pre>
Date: 30/03/1991, Rejects: 0, Accepts: 24, Line total: 240.000, Average 10.000
Date: 31/03/1991, Rejects: 0, Accepts: 24, Line total: 565.000, Average 23.542
Date: 31/03/1991, Rejects: 23, Accepts: 1, Line total: 40.000, Average 40.000
Date: 01/04/1991, Rejects: 1, Accepts: 23, Line total: 534.000, Average 23.217
Date: 02/04/1991, Rejects: 0, Accepts: 24, Line total: 475.000, Average 19.792
Date: 03/04/1991, Rejects: 0, Accepts: 24, Line total: 335.000, Average 13.958
Average: 16.823 (of 4 entirely valid days)
Maximum run of 24 consecutive false readings starting: 31/03/1991
</pre>
 
7,806

edits