Set consolidation: Difference between revisions

m (→‎{{header|Ring}}: Remove vanity tags)
Line 1,542:
set(H, I, K) set(A, B) set(C, D) set(D, B) set(F, G, H)
==> set(A, B, C, D) set(H, I, K, F, G)</pre>
 
=={{header|Phix}}==
Using strings to represent sets of characters
<lang Phix>function has_intersection(sequence set1, sequence set2)
sequence res = {}
for i=1 to length(set1) do
if find(set1[i],set2) then
return true
end if
end for
return false
end function
 
function union(sequence set1, sequence set2)
for i=1 to length(set2) do
if not find(set2[i],set1) then
set1 = append(set1,set2[i])
end if
end for
return set1
end function
 
function consolidate(sequence sets)
for i=length(sets) to 1 by -1 do
for j=length(sets) to i+1 by -1 do
if has_intersection(sets[i],sets[j]) then
sets[i] = union(sets[i],sets[j])
sets[j..j] = {}
end if
end for
end for
return sets
end function
 
?consolidate({"AB","CD"})
?consolidate({"AB","BD"})
?consolidate({"AB","CD","DB"})
?consolidate({"HIK","AB","CD","DB","FGH"})</lang>
{{out}}
<pre>
{"AB","CD"}
{"ABD"}
{"ABCD"}
{"HIKFG","ABCD"}
</pre>
 
=={{header|PicoLisp}}==
7,820

edits