Functional coverage tree: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (→{{header|Perl}}: tweaks to code, but mostly just reformatted for clarity) |
|||
Line 1,573: | Line 1,573: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
<lang perl> |
<lang perl>use strict; |
||
use strict; |
|||
use warnings; |
use warnings; |
||
sub walktree { |
|||
⚫ | |||
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*) /x; # save coverage |
|||
while( $_[0] =~ /(( *)\S.*\n)((?:\2 .*\n)*)/g ) |
|||
⚫ | |||
{ |
|||
my $coverage = sprintf '%-10s', $2 || 0; |
|||
my($w, $wsum) = (0, 0); |
|||
⚫ | |||
$head .= $_->[0], |
|||
$w |
$w += $_->[1], |
||
$wsum += $_->[1] * $_->[2] |
|||
for walktree( $body ); |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
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> |
|||
cleaning |1 |0.41 | |
|||
house1 |40 |0.33 | |
|||
⚫ | |||
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. |
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. |
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. |
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=== |