Functional coverage tree: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: tweaks to code, but mostly just reformatted for clarity)
Line 1,573: Line 1,573:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<lang perl>use strict;

use strict;
use warnings;
use warnings;


print $_->[0] for walktree( do { local $/; <DATA> } );
sub walktree {
my @parts;
while( $_[0] =~ /(?<head> (\s*) \N+\n ) # split off one level as 'head' (or terminal 'leaf')
(?<body> (?:\2 \s\N+\n)*)/gx ) { # next sub-level is 'body' (defined by extra depth of indentation)


my($head, $body) = ($+{head}, $+{body});
sub walktree
$head =~ /^.*? \| # ignore name
{
(\S*) \s* \| # save weight
my @parts;
(\S*) /x; # save coverage
while( $_[0] =~ /(( *)\S.*\n)((?:\2 .*\n)*)/g )
my $weight = sprintf '%-8s', $1 || 1;
{
my ($head, $body, $w, $wsum) = ($1, $3, 0, 0);
my $coverage = sprintf '%-10s', $2 || 0;
$head =~ /^.*?\|(\S*) *\|(\S*) *\|/;
my($w, $wsum) = (0, 0);

my $weight = sprintf '%-8s', $1 || 1;
my $coverage = sprintf '%-10s', $2 || 0;
$head .= $_->[0],
$w += $_->[1], $wsum += $_->[1] * $_->[2], $head .= $_->[0]
$w += $_->[1],
for walktree( $body );
$wsum += $_->[1] * $_->[2]
for walktree( $body );
$w and $coverage = sprintf '%-10.8g', $wsum / $w;

push @parts, [ $head =~ s/\|.*/|$weight|$coverage|/r, $weight, $coverage ];
$coverage = sprintf '%-10.2g', $wsum/$w unless $w == 0;
push @parts, [ $head =~ s/\|.*/|$weight|$coverage|/r, $weight, $coverage ];
}
}
return @parts;
return @parts;
}
}

print $_->[0] for walktree( join '', <DATA> );


__DATA__
__DATA__
Line 1,643: Line 1,648:
</lang>
</lang>
{{out}}
{{out}}
<pre>NAME_HIERARCHY |WEIGHT |COVERAGE |
<pre>
NAME_HIERARCHY |WEIGHT |COVERAGE |
cleaning |1 |0.41 |
cleaning |1 |0.40916667|
house1 |40 |0.33 |
house1 |40 |0.33125 |
bedrooms |1 |0.25 |
bedrooms |1 |0.25 |
bathrooms |1 |0.5 |
bathrooms |1 |0.5 |
Line 1,662: Line 1,666:
garage |1 |0 |
garage |1 |0 |
garden |1 |0.8 |
garden |1 |0.8 |
house2 |60 |0.46111111|
house2 |60 |0.46 |
upstairs |1 |0.15 |
upstairs |1 |0.15 |
bedrooms |1 |0 |
bedrooms |1 |0 |
Line 1,672: Line 1,676:
toilet |1 |0 |
toilet |1 |0 |
attics |1 |0.6 |
attics |1 |0.6 |
groundfloor |1 |0.31666667|
groundfloor |1 |0.32 |
kitchen |1 |0 |
kitchen |1 |0 |
living_rooms |1 |0 |
living_rooms |1 |0 |
Line 1,683: Line 1,687:
garden |1 |0.9 |
garden |1 |0.9 |
hot_tub_suite |1 |1 |
hot_tub_suite |1 |1 |
basement |1 |0.91666667|
basement |1 |0.92 |
cellars |1 |1 |
cellars |1 |1 |
wine_cellar |1 |1 |
wine_cellar |1 |1 |
cinema |1 |0.75 |
cinema |1 |0.75 |</pre>

</pre>
=={{header|Python}}==
=={{header|Python}}==
===Python: Using lists and tuples===
===Python: Using lists and tuples===