Anonymous user
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 <big>'''N'''</big> needn't be in <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
@.1 = " the cat sat on the mat | mat cat " /*a string. */
@.2 = " the cat sat on the mat | cat mat " /*
@.3 = " A B C A B C A B C | C A C A " /*
@.4 = " A B C A B D A B E | E A D A " /*
@.5 = " A B | B " /*
@.6 = " A B | B A " /*
@.7 = " A B B A | B A " /*
@.8 = " | " /*
@.9 = " A | A " /*
@.10 = " A B | " /*
@.11 = " A B B A | A B " /*
@.12 = " A B A B | A B " /*
@.13 = " A B A B | B A B A " /*
@.14 = " A B C C B A | A C A C " /*
@.15 = " A B C C B A | C A C A " /*
/* ════════════M═══════════ ════N════
parse var @.j m '|' n /*parse input string into M and N. */
_=word(
do k=1 for words(n)%2 by 2
p1=wordpos(_,m); p2=wordpos(v,m) /*positions of "
▲ mp= /*the MP (M') string (so far).*/
if $._>>$.v then do;
else do; r.p1=!.p1; r.p2=!.p2; end /*don't
end /*q*/ /* [↑] re-build the (output) str*/▼
!.p1=used; !.p2=used /*
m=
say @.j '───►' space(mp) /*display the new text reordered.*/▼
end /*k*/ /* [↑] rebuild the !. and
'''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 /*j*/ /* [↑] end of processing for N words*/
/*stick a fork in it, we're all done. */</lang>
▲'''output''' 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>
|