Anonymous user
Order disjoint list items: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace.
(→{{header|Haskell}}: simplify cases) |
m (→{{header|REXX}}: added/changed comments and whitespace.) |
||
Line 1,627:
=={{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 end─of─array, */
Line 1,646:
@.15 = " A B C C B A | C A C A " /*" " */
/* ════════════M═══════════ ════N════ */
/* [↓] process each input strings. */▼
do j=1 while @.j\==''
parse var @.j m '|' n /*parse input string into M and N. */
#=words(m) /*#: number of words in the M list.*/
do i=# for # by -1 /*process list items in reverse order. */
_=word(m, i); !.i=_; $._=i
end /*i*/
do k=1 by 2 for words(n) %
_=word(n, k); v=word(n, k+1)
p1=wordpos(_, m);
if p1==0 | p2==0 then iterate /*if either not found, then skip them. */
if $._>>$.v then do; r.p2=!.p1; r.p1=!.p2; end /*switch the words.*/
Line 1,662:
!.p1=used; !.p2=used /*mark 'em as used.*/
m=
do i=1 for #; m=m !.i; _=word(m, i); !.i=_; $._=i; end
end /*k*/ /* [↑] rebuild the !. and $. arrays.*/
mp= /*the MP (aka M') string (so far). */
Line 1,669:
end /*q*/ /* [↑] re─build the (output) string. */
say @.j '
end /*j*/ /* [↑] end of processing for N words*/
/*stick a fork in it, we're all done. */</lang>
|