Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
53
Task/Text-processing-1/Nim/text-processing-1.nim
Normal file
53
Task/Text-processing-1/Nim/text-processing-1.nim
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import os, strutils, sequtils
|
||||
|
||||
var
|
||||
nodata = 0
|
||||
nodataMax = -1
|
||||
nodataMaxLine:seq[string] = @[]
|
||||
|
||||
totFile = 0.0
|
||||
numFile = 0
|
||||
|
||||
for filename in commandLineParams():
|
||||
var f = open(filename)
|
||||
for line in f.lines:
|
||||
var
|
||||
totLine = 0.0
|
||||
numLine = 0
|
||||
field = line.split()
|
||||
date = field[0]
|
||||
data: seq[float] = @[]
|
||||
flags: seq[int] = @[]
|
||||
|
||||
for i, f in field[1 .. -1]:
|
||||
if i mod 2 == 0: data.add parseFloat(f)
|
||||
else: flags.add parseInt(f)
|
||||
|
||||
for datum, flag in items(zip(data, flags)):
|
||||
if flag < 1:
|
||||
inc nodata
|
||||
else:
|
||||
if nodataMax == nodata and nodata > 0:
|
||||
nodataMaxLine.add date
|
||||
if nodataMax < nodata and nodata > 0:
|
||||
nodataMax = nodata
|
||||
nodataMaxLine = @[date]
|
||||
nodata = 0
|
||||
totLine += datum
|
||||
inc numLine
|
||||
|
||||
totFile += totLine
|
||||
numFile += numLine
|
||||
|
||||
echo "Line: $# Reject: $# Accept: $# LineTot: $# LineAvg: $#"
|
||||
.format(date, data.len - numLine, numLine,
|
||||
formatFloat(totLine, precision = 0), formatFloat(
|
||||
(if numLine > 0: totLine / float(numLine) else: 0.0), precision = 0))
|
||||
|
||||
echo ""
|
||||
echo "File(s) = ", commandLineParams().join(" ")
|
||||
echo "Total = ", formatFloat(totFile, precision = 0)
|
||||
echo "Readings = ", numFile
|
||||
echo "Average = ", formatFloat(totFile / float(numFile), precision = 0)
|
||||
echo ""
|
||||
echo "Maximum run(s) of ", nodataMax, " consecutive false readings ends at line starting with date(s): ", nodataMaxLine.join(" ")
|
||||
33
Task/Text-processing-1/Sidef/text-processing-1.sidef
Normal file
33
Task/Text-processing-1/Sidef/text-processing-1.sidef
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
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}}.");
|
||||
1
Task/Text-processing-1/jq/text-processing-1-1.jq
Normal file
1
Task/Text-processing-1/jq/text-processing-1-1.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
foreach STREAM as $row ( INITIAL; EXPRESSION; VALUE ).
|
||||
1
Task/Text-processing-1/jq/text-processing-1-2.jq
Normal file
1
Task/Text-processing-1/jq/text-processing-1-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
foreach (inputs | split("\t")) as $line (INITIAL; EXPRESSION; VALUE)
|
||||
1
Task/Text-processing-1/jq/text-processing-1-3.jq
Normal file
1
Task/Text-processing-1/jq/text-processing-1-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
foreach ((inputs | split("\t")), null) as $line (INITIAL; EXPRESSION; VALUE)
|
||||
71
Task/Text-processing-1/jq/text-processing-1-4.jq
Normal file
71
Task/Text-processing-1/jq/text-processing-1-4.jq
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
# Input: { "max": max_run_length,
|
||||
# "starts": array_of_start_line_values, # of all the maximal runs
|
||||
# "start_dates": array_of_start_dates # of all the maximal runs
|
||||
# }
|
||||
def report:
|
||||
(.starts | length) as $l
|
||||
| if $l == 1 then
|
||||
"There is one maximal run of lines with flag<=0.",
|
||||
"The maximal run has length \(.max) and starts at line \(.starts[0]) and has start date \(.start_dates[0])."
|
||||
elif $l == 0 then
|
||||
"There is no lines with flag<=0."
|
||||
else
|
||||
"There are \($l) maximal runs of lines with flag<=0.",
|
||||
"These runs have length \(.max) and start at the following line numbers:",
|
||||
"\(.starts)",
|
||||
"The corresponding dates are:",
|
||||
"\(.start_dates)"
|
||||
end;
|
||||
|
||||
# "process" processes "tab-separated string values" on stdin
|
||||
def process:
|
||||
|
||||
# Given a line in the form of an array [date, datum1, flag2, ...],
|
||||
# "synopsis" returns [ number of data items on the line with flag>0, sum, number of data items on the line with flag<=0 ]
|
||||
def synopsis: # of a line
|
||||
. as $row
|
||||
| reduce range(0; (length - 1) / 2) as $i
|
||||
( [0,0,0];
|
||||
($row[1+ (2*$i)] | tonumber) as $datum
|
||||
| ($row[2+(2*$i)] | tonumber) as $flag
|
||||
| if ($flag>0) then .[0] += 1 | .[1] += $datum else .[2] += 1 end );
|
||||
|
||||
# state: {"line": line_number # (first line is line 0)
|
||||
# "synopis": _, # value returned by "synopsis"
|
||||
# "start": line_number_of_start_of_current_run,
|
||||
# "start_date": date_of_start_of_current_run,
|
||||
# "length": length_of_current_run # so far
|
||||
# "max": max_run_length # so far
|
||||
# "starts": array_of_start_values # of all the maximal runs
|
||||
# "start_dates": array_of_start_dates # of all the maximal runs
|
||||
# }
|
||||
foreach ((inputs | split("\t")), null) as $line # null signals END
|
||||
# Slots are effectively initialized by default to null
|
||||
( { "line": -1, "length": 0, "max": 0, "starts": [], "start_dates": [] };
|
||||
if $line == null then .line = null
|
||||
else
|
||||
.line += 1
|
||||
# | debug
|
||||
# synopsis returns [number with flag>0, sum, number with flag<=0 ]
|
||||
| .synopsis = ($line | synopsis)
|
||||
| if .synopsis[2] > 0 then
|
||||
if .start then . else .start = .line | .start_date = $line[0] end
|
||||
| .length += 1
|
||||
| if .max < .length then
|
||||
(.max = .length)
|
||||
| .starts = [ .start ]
|
||||
| .start_dates = [ .start_date ]
|
||||
elif .max == .length then
|
||||
.starts += [ .start ]
|
||||
| .start_dates += [ .start_date ]
|
||||
else .
|
||||
end
|
||||
else .start = null | .length = 0
|
||||
end
|
||||
end;
|
||||
.)
|
||||
| if .line == null then {max, starts, start_dates} | report
|
||||
else .synopsis
|
||||
end;
|
||||
|
||||
process
|
||||
7
Task/Text-processing-1/jq/text-processing-1-5.jq
Normal file
7
Task/Text-processing-1/jq/text-processing-1-5.jq
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
$ jq -c -n -R -r -f Text_processing_1.jq readings.txt
|
||||
[22,590,2]
|
||||
[24,410,0]
|
||||
...
|
||||
[23,47.3,1]
|
||||
There is one maximal run of lines with flag<=0.
|
||||
The maximal run has length 93 and starts at line 5378 and has start date 2004-09-30.
|
||||
Loading…
Add table
Add a link
Reference in a new issue