Set consolidation: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed a DO FOREVER loop into a DO UNTIL.
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:
 
=={{header|REXX}}==
<lang rexx>/*REXX program showsdemonstrates how to consolidate a sample bunch of sets. */
sets.= = /*assign all SETS. to null. */
sets.1 = '{A,B} {C,D}'
sets.2 = "{A,B} {B,D}"
Line 1,382:
sets.5 = '{snow,ice,slush,frost,fog} {iceburgs,icecubes} {rain,fog,sleet}'
 
do j=1 while sets.j\=='' /*traipse through the sample sets*/
call SETcombo sets.j /*have the other guy do the work.*/
end /*j*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────SETcombo subroutine─────────────────*/
SETcombo: procedure; parse arg bunch; n=words(bunch); newBunch=
say ' the old sets=' space(bunch)
 
do k=1 for n /*change all[↓] change commas to a blank. */
@.k=translate(word(bunch,k),,'},{') /*create a list of words (=a set)*/
end /*k*/ /*...··· and also remove the braces.*/
 
do until \changed; changed=0 /*consolidate some sets (maybe).*/
do set=1 for n-1
do item=1 for words(@.set); x=word(@.set,item)
do other=set+1 to n
if isIn(x,@.other) then do; changed=1 /*has changed.*/
@.set=@.set @.other; @.other=
iterate set
end
end /*other*/
end /*item*/
Line 1,407:
end /*until ¬changed*/
 
do set=1 for n; new= /*remove duplicates in a set. */
do items=1 for words(@.set); x=word(@.set, items)
x=word(@.set,items); if x==',' then iterate; if x=='' then leave
new=new x /*start building the new set. */
do forever; until if \isIn(x, @.set) then leave
_=wordpos(x, @.set)
@.set=subword(@.set,1,_-1) ',' subword(@.set,_+1) /*purify set.*/
end /*foreveruntil ¬isIn*/
end /*items*/
@.set=translate(strip(new), ',', " ")
end /*set*/
 
do new=1 for n; if @.new=='' then iterate
newBunch=space(newbunch '{'@.new"}")
end /*new*/
Line 1,425:
say ' the new sets=' newBunch; say
return
/*──────────────────────────────────isIn──────────────────────────────────iSIN subroutine─────────────────────*/
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2? */</lang>
'''output''' when using the default supplied sample sets:
<pre>