Jump to content

Functional coverage tree: Difference between revisions

→‎{{header|Perl}}: tweaks to code, but mostly just reformatted for clarity
(→‎{{header|Perl}}: tweaks to code, but mostly just reformatted for clarity)
Line 1,573:
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perluse strict;
 
use strict;
use warnings;
 
print $_->[0] forsub walktree( do { local $/; <DATA> } );
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
{
house1 (\S*) \s* \|40 |0.33125# 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, my $wsum)coverage = ($1sprintf '%-10s', $3,2 0,|| 0);
$head =~ /^.*?\| my(\S*$w, $wsum) *\|= (\S*)0, *\|/0);
 
my $weight = sprintf '%-8s', $1 || 1;
my $coverage = sprintf '%-10s', $2head ||.= $_->[0;],
$w += $_->[1], $wsum += $_->[1] * $_->[2], $head .= $_->[0]
for walktree( $bodywsum );+= $_->[1] * $_->[2]
for walktree( $body );
$w and $coverage = sprintf '%-10.8g', $wsum / $w;
 
push @parts, [ $head =~ s/\|.*/|$weight|$coverage|/r, $weight, $coverage ];
$w and $coverage = sprintf '%-10.8g2g', $wsum /$w unless $w == 0;
push @parts, [ $head =~ s/\|.*/|$weight|$coverage|/r, $weight, $coverage ];
}
return @parts;
}
 
print $_->[0] for walktree( join '', <DATA> );
 
__DATA__
Line 1,643 ⟶ 1,648:
</lang>
{{out}}
<pre>NAME_HIERARCHY |WEIGHT |COVERAGE |
<pre>
NAME_HIERARCHYcleaning |WEIGHT1 |COVERAGE0.41 |
cleaning house1 |1 |40 |0.4091666733 |
house1 |40 |0.33125 |
bedrooms |1 |0.25 |
bathrooms |1 |0.5 |
Line 1,662 ⟶ 1,666:
garage |1 |0 |
garden |1 |0.8 |
house2 |60 |0.4611111146 |
upstairs |1 |0.15 |
bedrooms |1 |0 |
Line 1,672 ⟶ 1,676:
toilet |1 |0 |
attics |1 |0.6 |
groundfloor |1 |0.3166666732 |
kitchen |1 |0 |
living_rooms |1 |0 |
Line 1,683 ⟶ 1,687:
garden |1 |0.9 |
hot_tub_suite |1 |1 |
basement |1 |0.9166666792 |
cellars |1 |1 |
wine_cellar |1 |1 |
cinema |1 |0.75 |</pre>
 
</pre>
=={{header|Python}}==
===Python: Using lists and tuples===
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.