Jump to content

Functional coverage tree: Difference between revisions

m (→‎{{header|Perl 6}}: simplified regex, relocate input table to end)
Line 1,807:
wine_cellar |1 |1 |
cinema |1 |0.75 |</pre>
 
=={{header|Phix}}==
<lang Phix>constant data = """
NAME_HIERARCHY | WEIGHT | COVERAGE |
cleaning | | |
house1 |40 | |
bedrooms | |0.25 |
bathrooms | | |
bathroom1 | |0.5 |
bathroom2 | | |
outside_lavatory | |1 |
attic | |0.75 |
kitchen | |0.1 |
living_rooms | | |
lounge | | |
dining_room | | |
conservatory | | |
playroom | |1 |
basement | | |
garage | | |
garden | |0.8 |
house2 |60 | |
upstairs | | |
bedrooms | | |
suite_1 | | |
suite_2 | | |
bedroom_3 | | |
bedroom_4 | | |
bathroom | | |
toilet | | |
attics | |0.6 |
groundfloor | | |
kitchen | | |
living_rooms | | |
lounge | | |
dining_room | | |
conservatory | | |
playroom | | |
wet_room_&_toilet | | |
garage | | |
garden | |0.9 |
hot_tub_suite | |1 |
basement | | |
cellars | |1 |
wine_cellar | |1 |
cinema | |0.75 |
"""
sequence lines = split(data,"\n",no_empty:=true),
pi = {}, -- indents (to locate parents)
pdx = {}, -- indexes for ""
children
string desc, weights, covers
integer parent, child
atom weight, coverage, childw = 0
enum DESC, WEIGHT, COVERAGE, PARENT, CHILDREN, CHILDW
lines[1] &= " SHARE OF RESIDUE"
for i=2 to length(lines) do
-- decode text to useable data, link up parents & children
{desc,weights,covers} = split(lines[i],"|")
-- (nb this assumes /totally/ consistent indenting)
integer indent = length(desc)-length(trim_head(desc)),
k = find(indent,pi)
if k=0 then
pi = append(pi,indent)
pdx = append(pdx,0)
k = length(pi)
end if
pdx[k] = i
parent = 0
if k>1 then
parent = pdx[k-1]
lines[parent][CHILDREN] &= i
end if
children = {}
weight = to_number(trim(weights),1)
coverage = to_number(trim(covers),0)
lines[i] = {desc, weight, coverage, parent, children, childw}
end for
for i=length(lines) to 2 by -1 do
-- calculate the parent coverages, and save child weight sums
children = lines[i][CHILDREN]
if length(children) then
coverage = 0
childw = 0
for c=1 to length(children) do
child = children[c]
atom w = lines[child][WEIGHT]
coverage += lines[child][COVERAGE]*w
childw += w
end for
lines[i][COVERAGE] = coverage/childw
lines[i][CHILDW] = childw -- (save for next loop)
end if
end for
for i=length(lines) to 2 by -1 do
-- calculate the share of residue, and format lines
child = i
{desc, weight, coverage, parent} = lines[i]
atom residue = 1-coverage
while parent do
residue *= lines[child][WEIGHT]/lines[parent][CHILDW]
{child, parent} = {parent, lines[parent][PARENT]}
end while
lines[i] = sprintf("%-32s| %6d | %-8g | %g",{desc,weight,coverage,residue})
end for
puts(1,join(lines,"\n")&"\n")</lang>
{{out}}
<pre>
NAME_HIERARCHY | WEIGHT | COVERAGE | SHARE OF RESIDUE
cleaning | 1 | 0.409167 | 0.590833
house1 | 40 | 0.33125 | 0.2675
bedrooms | 1 | 0.25 | 0.0375
bathrooms | 1 | 0.5 | 0.025
bathroom1 | 1 | 0.5 | 0.00833333
bathroom2 | 1 | 0 | 0.0166667
outside_lavatory | 1 | 1 | 0
attic | 1 | 0.75 | 0.0125
kitchen | 1 | 0.1 | 0.045
living_rooms | 1 | 0.25 | 0.0375
lounge | 1 | 0 | 0.0125
dining_room | 1 | 0 | 0.0125
conservatory | 1 | 0 | 0.0125
playroom | 1 | 1 | 0
basement | 1 | 0 | 0.05
garage | 1 | 0 | 0.05
garden | 1 | 0.8 | 0.01
house2 | 60 | 0.461111 | 0.323333
upstairs | 1 | 0.15 | 0.17
bedrooms | 1 | 0 | 0.05
suite_1 | 1 | 0 | 0.0125
suite_2 | 1 | 0 | 0.0125
bedroom_3 | 1 | 0 | 0.0125
bedroom_4 | 1 | 0 | 0.0125
bathroom | 1 | 0 | 0.05
toilet | 1 | 0 | 0.05
attics | 1 | 0.6 | 0.02
groundfloor | 1 | 0.316667 | 0.136667
kitchen | 1 | 0 | 0.0333333
living_rooms | 1 | 0 | 0.0333333
lounge | 1 | 0 | 0.00833333
dining_room | 1 | 0 | 0.00833333
conservatory | 1 | 0 | 0.00833333
playroom | 1 | 0 | 0.00833333
wet_room_&_toilet | 1 | 0 | 0.0333333
garage | 1 | 0 | 0.0333333
garden | 1 | 0.9 | 0.00333333
hot_tub_suite | 1 | 1 | 0
basement | 1 | 0.916667 | 0.0166667
cellars | 1 | 1 | 0
wine_cellar | 1 | 1 | 0
cinema | 1 | 0.75 | 0.0166667
</pre>
 
=={{header|Python}}==
7,818

edits

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