Set consolidation: Difference between revisions

Content added Content deleted
(Added AutoHotkey)
(Removed AutoHotkey, needs tweaking)
Line 320: Line 320:
{A, B, C, D}
{A, B, C, D}
{A, B, C, D}, {F, G, H, I, K}</pre>
{A, B, C, D}, {F, G, H, I, K}</pre>

=={{header|AutoHotkey}}==
<lang AutoHotkey>Consolidate(Sets){
for i, set in Sets
{
thisSet := Sets[i]
loop, % Sets.MaxIndex() - i
if (intersected(Sets[i], Sets[j := Sets.MaxIndex() - A_Index + 1]))
list .= (list ~= "~" i "|" j "~") ? i "~" j "~" : "`n~" i "~" j "~"
}

loop, parse, list, `n
{
Line := A_Index, CSet%Line% := []
loop, parse, A_LoopField, ~
aa .= (aa?"`n":"") line , CSet%Line% := Union(Sets[A_LoopField], CSet%Line%)
}
Sort, aa, U
loop, parse, aa, `n
{
out .= A_Index=1?"":"}`n"
for k , v in CSet%A_LoopField%
out .= (A_Index=1?"{":", ") v
}
return out .= "}"
}


Union(SetA,SetB:=""){
SetC:=[], Temp:=[]
for i, val in SetA
SetC.Insert(val), Temp[val] := true
for i, val in SetB
if !Temp[val]
SetC.Insert(val)
return SetC
}

intersected(SetA,SetB){
SetC:=[], Temp:=[]
for i, val in SetA
Temp[val] := true
for i, val in SetB
if Temp[val]
return 1
return 0
}</lang>
Examples:<lang AutoHotkey>A:=["H","I","K"]
B:=["A","B"]
C:=["C","D"]
D:=["D","B"]
E:=["F","G","H"]
Sets := [A,B,C,D,E,F]

MsgBox % Consolidate(Sets)</lang>
Outputs:<pre>{F, G, H, I, K}
{D, B, C, A}</pre>


=={{header|Bracmat}}==
=={{header|Bracmat}}==