Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
59
Task/Text-processing-1/Perl/text-processing-1-1.pl
Normal file
59
Task/Text-processing-1/Perl/text-processing-1-1.pl
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $nodata = 0; # Current run of consecutive flags<0 in lines of file
|
||||
my $nodata_max = -1; # Max consecutive flags<0 in lines of file
|
||||
my $nodata_maxline = "!"; # ... and line number(s) where it occurs
|
||||
|
||||
my $infiles = join ", ", @ARGV;
|
||||
|
||||
my $tot_file = 0;
|
||||
my $num_file = 0;
|
||||
|
||||
while (<>) {
|
||||
chomp;
|
||||
my $tot_line = 0; # sum of line data
|
||||
my $num_line = 0; # number of line data items with flag>0
|
||||
my $rejects = 0;
|
||||
|
||||
# extract field info, skipping initial date field
|
||||
my ($date, @fields) = split;
|
||||
while (@fields and my ($datum, $flag) = splice @fields, 0, 2) {
|
||||
if ($flag+1 < 2) {
|
||||
$nodata++;
|
||||
$rejects++;
|
||||
next;
|
||||
}
|
||||
|
||||
# check run of data-absent fields
|
||||
if($nodata_max == $nodata and $nodata > 0){
|
||||
$nodata_maxline = "$nodata_maxline, $date";
|
||||
}
|
||||
if($nodata_max < $nodata and $nodata > 0){
|
||||
$nodata_max = $nodata;
|
||||
$nodata_maxline = $date;
|
||||
}
|
||||
# re-initialise run of nodata counter
|
||||
$nodata = 0;
|
||||
# gather values for averaging
|
||||
$tot_line += $datum;
|
||||
$num_line++;
|
||||
}
|
||||
|
||||
# totals for the file so far
|
||||
$tot_file += $tot_line;
|
||||
$num_file += $num_line;
|
||||
|
||||
printf "Line: %11s Reject: %2i Accept: %2i Line_tot: %10.3f Line_avg: %10.3f\n",
|
||||
$date, $rejects, $num_line, $tot_line, ($num_line>0)? $tot_line/$num_line: 0;
|
||||
|
||||
}
|
||||
|
||||
printf "\n";
|
||||
printf "File(s) = %s\n", $infiles;
|
||||
printf "Total = %10.3f\n", $tot_file;
|
||||
printf "Readings = %6i\n", $num_file;
|
||||
printf "Average = %10.3f\n", $tot_file / $num_file;
|
||||
|
||||
printf "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s\n",
|
||||
$nodata_max, $nodata_maxline;
|
||||
115
Task/Text-processing-1/Perl/text-processing-1-2.pl
Normal file
115
Task/Text-processing-1/Perl/text-processing-1-2.pl
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
|
||||
use constant RESULT_TEMPLATE => "%-19s = %12.3f / %-6u = %.3f\n";
|
||||
|
||||
my $parser = Parser->new;
|
||||
|
||||
# parse lines and print results
|
||||
printf RESULT_TEMPLATE, $parser->parse(split)
|
||||
while <>;
|
||||
|
||||
$parser->finish;
|
||||
|
||||
# print total and summary
|
||||
printf "\n".RESULT_TEMPLATE."\n", $parser->result;
|
||||
printf "the maximum of %u consecutive bad values was reached %u time(s)\n",
|
||||
$parser->bad_max, scalar $parser->bad_ranges;
|
||||
|
||||
# print bad ranges
|
||||
print for map { ' '.join(' - ', @$_)."\n" } $parser->bad_ranges;
|
||||
|
||||
BEGIN {
|
||||
package main::Parser;
|
||||
|
||||
sub new {
|
||||
my $obj = {
|
||||
SUM => 0,
|
||||
COUNT => 0,
|
||||
CURRENT_DATE => undef,
|
||||
BAD_DATE => undef,
|
||||
BAD_RANGES => [],
|
||||
BAD_MAX => 0,
|
||||
BAD_COUNT => 0
|
||||
};
|
||||
|
||||
return bless $obj;
|
||||
}
|
||||
|
||||
sub _average {
|
||||
my ($sum, $count) = @_;
|
||||
return ($sum, $count, $count && $sum / $count);
|
||||
}
|
||||
|
||||
sub _push_bad_range_if_necessary {
|
||||
my ($parser) = @_;
|
||||
my ($count, $max) = @$parser{qw(BAD_COUNT BAD_MAX)};
|
||||
|
||||
return if $count < $max;
|
||||
|
||||
if ($count > $max) {
|
||||
$parser->{BAD_RANGES} = [];
|
||||
$parser->{BAD_MAX} = $count;
|
||||
}
|
||||
|
||||
push @{$parser->{BAD_RANGES}}, [ @$parser{qw(BAD_DATE CURRENT_DATE)} ];
|
||||
}
|
||||
|
||||
sub _check {
|
||||
my ($parser, $flag) = @_;
|
||||
if ($flag <= 0) {
|
||||
++$parser->{BAD_COUNT};
|
||||
$parser->{BAD_DATE} = $parser->{CURRENT_DATE}
|
||||
unless defined $parser->{BAD_DATE};
|
||||
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
$parser->_push_bad_range_if_necessary;
|
||||
$parser->{BAD_COUNT} = 0;
|
||||
$parser->{BAD_DATE} = undef;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
sub bad_max {
|
||||
my ($parser) = @_;
|
||||
return $parser->{BAD_MAX}
|
||||
}
|
||||
|
||||
sub bad_ranges {
|
||||
my ($parser) = @_;
|
||||
return @{$parser->{BAD_RANGES}}
|
||||
}
|
||||
|
||||
sub parse {
|
||||
my $parser = shift;
|
||||
my $date = shift;
|
||||
|
||||
$parser->{CURRENT_DATE} = $date;
|
||||
|
||||
my $sum = 0;
|
||||
my $count = 0;
|
||||
|
||||
while (my ($value, $flag) = splice @_, 0, 2) {
|
||||
next unless $parser->_check($flag);
|
||||
$sum += $value;
|
||||
++$count;
|
||||
}
|
||||
|
||||
$parser->{SUM} += $sum;
|
||||
$parser->{COUNT} += $count;
|
||||
|
||||
return ("average($date)", _average($sum, $count));
|
||||
}
|
||||
|
||||
sub result {
|
||||
my ($parser) = @_;
|
||||
return ('total-average', _average(@$parser{qw(SUM COUNT)}));
|
||||
}
|
||||
|
||||
sub finish {
|
||||
my ($parser) = @_;
|
||||
$parser->_push_bad_range_if_necessary
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue