Order disjoint list items: Difference between revisions

m
→‎{{header|REXX}}: add/changed whitespace and comments, changed wording in the REXX section header, made other cosmetic changes.
m (→‎{{header|REXX}}: add/changed whitespace and comments, changed wording in the REXX section header, made other cosmetic changes.)
Line 780:
 
=={{header|REXX}}==
Items in &nbsp; <big>'''N'''</big> &nbsp; needn't be in &nbsp; <big>'''M'''</big>.
<lang rexx>/*REXX program orders a disjoint list of M items with a list of N items.*/
used = '0'x /*indicates that a word has been parsed.*/
@. = /*placeholder indicates e─o─array end─of─array, */
@.1 = " the cat sat on the mat | mat cat " /*a string. */
@.2 = " the cat sat on the mat | cat mat " /*string." " */
@.3 = " A B C A B C A B C | C A C A " /*string." " */
@.4 = " A B C A B D A B E | E A D A " /*string." " */
@.5 = " A B | B " /*string." " */
@.6 = " A B | B A " /*string." " */
@.7 = " A B B A | B A " /*string." " */
@.8 = " | " /*string." " */
@.9 = " A | A " /*string." " */
@.10 = " A B | " /*string." " */
@.11 = " A B B A | A B " /*string." " */
@.12 = " A B A B | A B " /*string." " */
@.13 = " A B A B | B A B A " /*string." " */
@.14 = " A B C C B A | A C A C " /*string." " */
@.15 = " A B C C B A | C A C A " /*string." " */
/* ════════════M═══════════ ════N════ /* [↓] process each input string*/
do j=1 while @.j\==''; r.= /*nullify the[↓] replacement stringprocess each input strings. */
parsedo j=1 while var @.j m \=='|' n ; r.= /*parsenullify inputthe replacement string into [R.] M & N.*/
parse var @.j m '|' n /*parse input string into M and N. */
mw=words(m); do i=mw for mw by -1; x=word(m,i); !.i=x; $.x=i; end
#=words(m) /*#: [↑] buildnumber of !words andin $the arrays.M list.*/
do k=1 for words(n)%2 by 2 do i=# /* [↓]for # process theby -1 N array./*process list items in reverse order. */
_=word(nm,ki); v=word(n,k+1) /*getobtain ana ordersoecific word &from the replacementlist.*/
mp= !.i=_; $._=i /*construct the !. /*the MP (M')and string$. (so far)arrays.*/
p1=wordpos(_,m); p2=wordpos(v,m) /*positions of word & replacement*/
if p1==0 | p2==0 then iterate /*if eitherend not found, skip 'em. /*i*/
 
if $._>>$.v then do; r.p2=!.p1; r.p1=!.p2; end /*switch words.*/
do k=1 for words(n)%2 by 2 else do;/* [↓] process the N r.p1=!.p1; rarray.p2=!.p2; end /*don't switch. */
!.p1_=usedword(n,k); !.p2=used; mv=word(n,k+1) /*get an order word and the /*mark as used.replacement*/
p1=wordpos(_,m); p2=wordpos(v,m) /*positions of " do i=1" for mw;" m=m !.i; x=word(m,i); !.i=x; " $.x=i; end*/
end /*k*/ if p1==0 | p2==0 then iterate /*if [↑]either not rebuildfound, thethen !skip &them. $ arrays.*/
 
mp= /*the MP (M') string (so far).*/
if $._>>$.v then do; q=1 for mwr.p2=!.p1; if !r.qp1==used!.p2; thenend mp=mp r.q /*useswitch original.the words.*/
else do; r.p1=!.p1; r.p2=!.p2; end /*don't else mp=mp !switch.q /*use substitute*/
 
end /*q*/ /* [↑] re-build the (output) str*/
!.p1=used; !.p2=used /*═══════════════════════════════mark 'em as used.*/
m=
say @.j '───►' space(mp) /*display the new text reordered.*/
end /*j*/ do i=1 for #; m=m !.i; /*_=word(m,i); [↑]!.i=_; end$._=i; of processing N words*/end
end /*k*/ /* [↑] rebuild the !. and /*stick a fork in$. it, we're donearrays.*/</lang>
 
'''output''' using the internal (input) strings:
mp= /*the MP (aka M') string (so far). */
do q=1 for #; if !.q==used then mp=mp r.q /*use the original.*/
else mp=mp !.q /*use substitute. */
end /*q*/ /* [↑] re-buildre─build the (output) strstring. */
 
say @.j '───►' space(mp) /*display the new re─ordered text reordered──► term.*/
end /*j*/ /* [↑] end of processing for N words*/
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; using the internal (input) strings:
<pre>
the cat sat on the mat | mat cat ───► the mat sat on the cat
the cat sat on the mat | cat mat ───► the cat sat on the mat
A B C A B C A B C | C A C A ───► C B A C B A A B C
A B C A B D A B E | E A D A ───► E B C A B D A B A
A B | B ───► A B
A B | B A ───► B A
A B B A | B A ───► B A B A
| ───►
A | A ───► A
A B | ───► A B
A B B A | A B ───► A B B A
A B A B | A B ───► A B A B
A B A B | B A B A ───► B A B A
A B C C B A | A C A C ───► A B C A B C
A B C C B A | C A C A ───► C B A C B A
</pre>