Functional coverage tree: Difference between revisions
→{{header|Perl}}: tweaks to code, but mostly just reformatted for clarity
SqrtNegInf (talk | contribs) (→{{header|Perl}}: tweaks to code, but mostly just reformatted for clarity) |
|||
Line 1,573:
=={{header|Perl}}==
<lang perl>
use warnings;
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});
$head =~ /^.*? \| # ignore name
▲ my @parts;
(\S*) /x; # save coverage
my $weight = sprintf '%-8s', $1 || 1;▼
▲ my $weight = sprintf '%-8s', $1 || 1;
$w
for walktree( $body );
$w and $coverage = sprintf '%-10.8g', $wsum / $w;▼
push @parts, [ $head =~ s/\|.*/|$weight|$coverage|/r, $weight, $coverage ];▼
▲ 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 |
▲ 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.
upstairs |1 |0.15 |
bedrooms |1 |0 |
Line 1,672 ⟶ 1,676:
toilet |1 |0 |
attics |1 |0.6 |
groundfloor |1 |0.
kitchen |1 |0 |
living_rooms |1 |0 |
Line 1,683 ⟶ 1,687:
garden |1 |0.9 |
hot_tub_suite |1 |1 |
basement |1 |0.
cellars |1 |1 |
wine_cellar |1 |1 |
cinema |1 |0.75 |</pre>
=={{header|Python}}==
===Python: Using lists and tuples===
|