Jump to content

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 &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 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\==''; r.= /*nullify the replacement string /* [R.] process each input string (@.).*/
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 /*construct the !. and $. arrays.*/
end /*i*/
r.= /*nullify [↓]the replacement processstring each input strings[R. ] */
 
do k=1 by 2 for words(n) %2 by 2 /* [↓] process the N array. */
_=word(n, k); v=word(n, k+1) /*get an order word and the replacement*/
p1=wordpos(_, m); p2=wordpos(v, m) /*positions of " " " " */
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 /*i*/
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 '───► ────► ' space(mp) /*display new re─ordered text ──► term.*/
end /*j*/ /* [↑] end of processing for N words*/
/*stick a fork in it, we're all done. */</lang>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.