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