Set consolidation: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: removed STYLE from the PRE html tag.)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed a DO FOREVER loop into a DO UNTIL.)
Line 1,374: Line 1,374:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program shows how to consolidate a sample bunch of sets. */
<lang rexx>/*REXX program demonstrates how to consolidate a sample bunch of sets.*/
sets.= /*assign all SETS. to null. */
sets. = /*assign all SETS. to null.*/
sets.1 = '{A,B} {C,D}'
sets.1 = '{A,B} {C,D}'
sets.2 = "{A,B} {B,D}"
sets.2 = "{A,B} {B,D}"
Line 1,382: Line 1,382:
sets.5 = '{snow,ice,slush,frost,fog} {iceburgs,icecubes} {rain,fog,sleet}'
sets.5 = '{snow,ice,slush,frost,fog} {iceburgs,icecubes} {rain,fog,sleet}'


do j=1 while sets.j\=='' /*traipse through the sample sets*/
do j=1 while sets.j\=='' /*traipse through the sample sets*/
call SETcombo sets.j /*have the other guy do the work.*/
call SETcombo sets.j /*have the other guy do the work.*/
end /*j*/
end /*j*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SETcombo subroutine─────────────────*/
/*──────────────────────────────────SETcombo subroutine─────────────────*/
SETcombo: procedure; parse arg bunch; n=words(bunch); newBunch=
SETcombo: procedure; parse arg bunch; n=words(bunch); newBunch=
say ' the old sets=' space(bunch)
say ' the old sets=' space(bunch)


do k=1 for n /*change all commas to a blank. */
do k=1 for n /* [↓] change commas to a blank.*/
@.k=translate(word(bunch,k),,'},{') /*create a list of words (=a set)*/
@.k=translate(word(bunch,k),,'},{') /*create a list of words (=a set)*/
end /*k*/ /*... and also remove the braces.*/
end /*k*/ /*··· and also remove the braces.*/


do until \changed; changed=0 /*consolidate some sets (maybe).*/
do until \changed; changed=0 /*consolidate some sets (maybe).*/
do set=1 for n-1
do set=1 for n-1
do item=1 for words(@.set); x=word(@.set,item)
do item=1 for words(@.set); x=word(@.set,item)
do other=set+1 to n
do other=set+1 to n
if isIn(x,@.other) then do; changed=1
if isIn(x,@.other) then do; changed=1 /*has changed.*/
@.set=@.set @.other; @.other=
@.set=@.set @.other; @.other=
iterate set
iterate set
end
end
end /*other*/
end /*other*/
end /*item*/
end /*item*/
Line 1,407: Line 1,407:
end /*until ¬changed*/
end /*until ¬changed*/


do set=1 for n; new= /*remove duplicates in a set. */
do set=1 for n; new= /*remove duplicates in a set. */
do items=1 for words(@.set)
do items=1 for words(@.set); x=word(@.set, items)
x=word(@.set,items); if x==',' then iterate; if x=='' then leave
if x==',' then iterate; if x=='' then leave
new=new x /*start building the new set. */
new=new x /*start building the new set. */
do forever; if \isIn(x,@.set) then leave
do until \isIn(x, @.set)
_=wordpos(x,@.set)
_=wordpos(x, @.set)
@.set=subword(@.set,1,_-1) ',' subword(@.set,_+1) /*purify set.*/
@.set=subword(@.set,1,_-1) ',' subword(@.set,_+1) /*purify set.*/
end /*forever*/
end /*until ¬isIn*/
end /*items*/
end /*items*/
@.set=translate(strip(new),','," ")
@.set=translate(strip(new), ',', " ")
end /*set*/
end /*set*/


do new=1 for n; if @.new=='' then iterate
do new=1 for n; if @.new=='' then iterate
newBunch=space(newbunch '{'@.new"}")
newBunch=space(newbunch '{'@.new"}")
end /*new*/
end /*new*/
Line 1,425: Line 1,425:
say ' the new sets=' newBunch; say
say ' the new sets=' newBunch; say
return
return
/*──────────────────────────────────isIn subroutine─────────────────────*/
/*──────────────────────────────────iSIN subroutine─────────────────────*/
isIn: return wordpos(arg(1),arg(2))\==0 /*is (word) arg1 in set arg2? */</lang>
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2? */</lang>
'''output''' when using the default supplied sample sets:
'''output''' when using the default supplied sample sets:
<pre>
<pre>