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
66
Task/Text-processing-2/Nim/text-processing-2.nim
Normal file
66
Task/Text-processing-2/Nim/text-processing-2.nim
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import strutils, tables
|
||||
|
||||
const NumFields = 49
|
||||
const DateField = 0
|
||||
const FlagGoodValue = 1
|
||||
|
||||
var badRecords: int # the number of records that have invalid formatted values
|
||||
var totalRecords: int # the total number of records in the file
|
||||
var badInstruments: int # the total number of records that have at least one instrument showing error
|
||||
var seenDates = newTable[string,bool]() # table that keeps track of what dates we have seen
|
||||
|
||||
# ensure we can parse all records as floats (except the date stamp)
|
||||
proc checkFloats(floats:seq[string]): bool =
|
||||
for index in 1..NumFields-1:
|
||||
try:
|
||||
# we're assuming all instrument flags are floats not integers
|
||||
discard parseFloat(floats[index])
|
||||
except ValueError:
|
||||
return false
|
||||
true
|
||||
|
||||
# ensure that all sensor flags are ok
|
||||
proc areAllFlagsOk(instruments: seq[string]): bool =
|
||||
#flags start at index 2, and occur every 2 fields
|
||||
for index in countup(2,NumFields,2):
|
||||
# we're assuming all instrument flags are floats not integers
|
||||
var flag = parseFloat(instruments[index])
|
||||
if flag < FlagGoodValue: return false
|
||||
true
|
||||
|
||||
|
||||
# Note: we're not checking the format of the date stamp
|
||||
|
||||
# main
|
||||
var lines = readFile("readings.txt")
|
||||
var currentLine: int
|
||||
|
||||
for line in lines.splitLines:
|
||||
currentLine.inc
|
||||
#empty lines don't count as records
|
||||
if line.len == 0: continue
|
||||
|
||||
var tokens = line.split({' ','\t'})
|
||||
|
||||
totalRecords.inc
|
||||
|
||||
if tokens.len != NumFields:
|
||||
badRecords.inc
|
||||
continue
|
||||
|
||||
if not checkFloats(tokens):
|
||||
badRecords.inc
|
||||
continue
|
||||
|
||||
if not areAllFlagsOk(tokens):
|
||||
badInstruments.inc
|
||||
|
||||
if seenDates.hasKeyOrPut(tokens[DateField], true):
|
||||
echo tokens[DateField], " duplicated on line ", currentLine
|
||||
|
||||
var goodRecords = totalRecords - badRecords
|
||||
var goodInstruments = goodRecords - badInstruments
|
||||
|
||||
echo "Total Records:", totalRecords
|
||||
echo "Good Records:", goodRecords
|
||||
echo "Records where all instuments were OK:", goodInstruments
|
||||
14
Task/Text-processing-2/Sidef/text-processing-2.sidef
Normal file
14
Task/Text-processing-2/Sidef/text-processing-2.sidef
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var good_records = 0;
|
||||
var dates = Hash();
|
||||
|
||||
ARGF.each { |line|
|
||||
var m = /^(\d\d\d\d-\d\d-\d\d)((?:\h+\d+\.\d+\h+-?\d+){24})\s*$/.match(line);
|
||||
m || (warn "Bad format at line #{$.}"; next);
|
||||
dates{m[0]} := 0 ++;
|
||||
var i = 0;
|
||||
m[1].words.all{|n| i++.is_even || (n.to_num >= 1) } && ++good_records;
|
||||
}
|
||||
|
||||
say "#{good_records} good records out of #{$.} total";
|
||||
say 'Repeated timestamps:';
|
||||
say dates.to_a.grep{ .value > 1 }.map { .key }.sort.join("\n");
|
||||
1
Task/Text-processing-2/jq/text-processing-2-1.jq
Normal file
1
Task/Text-processing-2/jq/text-processing-2-1.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
$ jq -R '[splits("[ \t]+")]' Text_processing_2.txt
|
||||
16
Task/Text-processing-2/jq/text-processing-2-2.jq
Normal file
16
Task/Text-processing-2/jq/text-processing-2-2.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# Given any array, produce an array of [item, count] pairs for each run.
|
||||
def runs:
|
||||
reduce .[] as $item
|
||||
( [];
|
||||
if . == [] then [ [ $item, 1] ]
|
||||
else .[length-1] as $last
|
||||
| if $last[0] == $item then (.[0:length-1] + [ [$item, $last[1] + 1] ] )
|
||||
else . + [[$item, 1]]
|
||||
end
|
||||
end ) ;
|
||||
|
||||
def is_float: test("^[-+]?[0-9]*[.][0-9]*([eE][-+]?[0-9]+)?$");
|
||||
|
||||
def is_integral: test("^[-+]?[0-9]+$");
|
||||
|
||||
def is_date: test("[12][0-9]{3}-[0-9][0-9]-[0-9][0-9]");
|
||||
19
Task/Text-processing-2/jq/text-processing-2-3.jq
Normal file
19
Task/Text-processing-2/jq/text-processing-2-3.jq
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
# Report line and column numbers using conventional numbering (IO=1).
|
||||
def validate_line(nr):
|
||||
def validate_date:
|
||||
if is_date then empty else "field 1 in line \(nr) has an invalid date: \(.)" end;
|
||||
def validate_length(n):
|
||||
if length == n then empty else "line \(nr) has \(length) fields" end;
|
||||
def validate_pair(i):
|
||||
( .[2*i + 1] as $n
|
||||
| if ($n | is_float) then empty else "field \(2*i + 2) in line \(nr) is not a float: \($n)" end),
|
||||
( .[2*i + 2] as $n
|
||||
| if ($n | is_integral) then empty else "field \(2*i + 3) in line \(nr) is not an integer: \($n)" end);
|
||||
|
||||
(.[0] | validate_date),
|
||||
(validate_length(49)),
|
||||
(range(0; (length-1) / 2) as $i | validate_pair($i)) ;
|
||||
|
||||
def validate_lines:
|
||||
. as $in
|
||||
| range(0; length) as $i | ($in[$i] | validate_line($i + 1));
|
||||
2
Task/Text-processing-2/jq/text-processing-2-4.jq
Normal file
2
Task/Text-processing-2/jq/text-processing-2-4.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def duplicate_timestamps:
|
||||
[.[][0]] | sort | runs | map( select(.[1]>1) );
|
||||
11
Task/Text-processing-2/jq/text-processing-2-5.jq
Normal file
11
Task/Text-processing-2/jq/text-processing-2-5.jq
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# The following ignores any issues with respect to duplicate dates,
|
||||
# but does check the validity of the record, including the date format:
|
||||
def number_of_valid_readings:
|
||||
def check:
|
||||
. as $in
|
||||
| (.[0] | is_date)
|
||||
and length == 49
|
||||
and all(range(0; 24) | $in[2*. + 1] | is_float)
|
||||
and all(range(0; 24) | $in[2*. + 2] | (is_integral and tonumber >= 1) );
|
||||
|
||||
map(select(check)) | length ;
|
||||
4
Task/Text-processing-2/jq/text-processing-2-6.jq
Normal file
4
Task/Text-processing-2/jq/text-processing-2-6.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
validate_lines,
|
||||
"\nChecking for duplicate timestamps:",
|
||||
duplicate_timestamps,
|
||||
"\nThere are \(number_of_valid_readings) valid rows altogether."
|
||||
16
Task/Text-processing-2/jq/text-processing-2-7.jq
Normal file
16
Task/Text-processing-2/jq/text-processing-2-7.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
$ jq -R '[splits("[ \t]+")]' Text_processing_2.txt | jq -s -r -f Text_processing_2.jq
|
||||
field 1 in line 6 has an invalid date: 991-04-03
|
||||
line 6 has 47 fields
|
||||
field 2 in line 6 is not a float: 10000
|
||||
field 3 in line 6 is not an integer: 1.0
|
||||
field 47 in line 6 is not an integer: x
|
||||
|
||||
Checking for duplicate timestamps:
|
||||
[
|
||||
[
|
||||
"1991-03-31",
|
||||
2
|
||||
]
|
||||
]
|
||||
|
||||
There are 5 valid rows altogether.
|
||||
26
Task/Text-processing-2/jq/text-processing-2-8.jq
Normal file
26
Task/Text-processing-2/jq/text-processing-2-8.jq
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
$ jq -R '[splits("[ \t]+")]' readings.txt | jq -s -r -f Text_processing_2.jq
|
||||
Checking for duplicate timestamps:
|
||||
[
|
||||
[
|
||||
"1990-03-25",
|
||||
2
|
||||
],
|
||||
[
|
||||
"1991-03-31",
|
||||
2
|
||||
],
|
||||
[
|
||||
"1992-03-29",
|
||||
2
|
||||
],
|
||||
[
|
||||
"1993-03-28",
|
||||
2
|
||||
],
|
||||
[
|
||||
"1995-03-26",
|
||||
2
|
||||
]
|
||||
]
|
||||
|
||||
There are 5017 valid rows altogether.
|
||||
Loading…
Add table
Add a link
Reference in a new issue