Order disjoint list items: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→sprintf version: Add comment) |
m (→{{header|Wren}}: Minor tidy) |
||
(11 intermediate revisions by 9 users not shown) | |||
Line 42:
* [[Sort disjoint sublist]]
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F order_disjoint_list_items(&data, items)
[Int] itemindices
L(item) Set(items)
V itemcount = items.count(item)
V lastindex = [-1]
L(i) 0 .< itemcount
lastindex.append(data.index(item, lastindex.last + 1))
itemindices [+]= lastindex[1..]
itemindices.sort()
L(index, item) zip(itemindices, items)
data[index] = item
F slist(s)
R Array(s).map(String)
F tostring(l)
R ‘'’l.join(‘ ’)‘'’
L(data, items) [(‘the cat sat on the mat’.split(‘ ’), (‘mat cat’).split(‘ ’)),
(‘the cat sat on the mat’.split(‘ ’), (‘cat mat’).split(‘ ’)),
(slist(‘ABCABCABC’), slist(‘CACA’)),
(slist(‘ABCABDABE’), slist(‘EADA’)),
(slist(‘AB’), slist(‘B’)),
(slist(‘AB’), slist(‘BA’)),
(slist(‘ABBA’), slist(‘BA’)),
(slist(‘’), slist(‘’)),
(slist(‘A’), slist(‘A’)),
(slist(‘AB’), slist(‘’)),
(slist(‘ABBA’), slist(‘AB’)),
(slist(‘ABAB’), slist(‘AB’)),
(slist(‘ABAB’), slist(‘BABA’)),
(slist(‘ABCCBA’), slist(‘ACAC’)),
(slist(‘ABCCBA’), slist(‘CACA’))]
print(‘Data M: #<24 Order N: #<9’.format(tostring(data), tostring(items)), end' ‘ ’)
order_disjoint_list_items(&data, items)
print(‘-> M' #.’.format(tostring(data)))</syntaxhighlight>
{{out}}
<pre>
Data M: 'the cat sat on the mat' Order N: 'mat cat' -> M' 'the mat sat on the cat'
Data M: 'the cat sat on the mat' Order N: 'cat mat' -> M' 'the cat sat on the mat'
Data M: 'A B C A B C A B C' Order N: 'C A C A' -> M' 'C B A C B A A B C'
Data M: 'A B C A B D A B E' Order N: 'E A D A' -> M' 'E B C A B D A B A'
Data M: 'A B' Order N: 'B' -> M' 'A B'
Data M: 'A B' Order N: 'B A' -> M' 'B A'
Data M: 'A B B A' Order N: 'B A' -> M' 'B A B A'
Data M: '' Order N: '' -> M' ''
Data M: 'A' Order N: 'A' -> M' 'A'
Data M: 'A B' Order N: '' -> M' 'A B'
Data M: 'A B B A' Order N: 'A B' -> M' 'A B B A'
Data M: 'A B A B' Order N: 'A B' -> M' 'A B A B'
Data M: 'A B A B' Order N: 'B A B A' -> M' 'B A B A'
Data M: 'A B C C B A' Order N: 'A C A C' -> M' 'A B C A B C'
Data M: 'A B C C B A' Order N: 'C A C A' -> M' 'C B A C B A'
</pre>
=={{header|Aime}}==
<
{
integer j;
Line 84 ⟶ 143:
0;
}</
{{out}}
<pre>the cat sat on the mat | mat cat -> the mat sat on the cat
Line 99 ⟶ 158:
Accumulate a segmentation of M over a fold/reduce, and zip with N:
<
-- disjointOrder :: String -> String -> String
Line 408 ⟶ 467:
map(pair, items 1 thru minimum([length of xs, length of ys]) of xs)
end zip</
{{Out}}
<pre>the cat sat on the mat -> mat cat -> the mat sat on the cat
Line 421 ⟶ 480:
===Idiomatic===
<
The task description talks about items in lists, but the examples are space-delimited substrings of strings.
The handler here deals with items in lists and leaves it to the calling code to sort out the rest.
Line 465 ⟶ 524:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
Data M: 'the cat sat on the mat' Order N: 'cat mat' --> 'the cat sat on the mat'
Data M: 'A B C A B C A B C' Order N: 'C A C A' --> 'C B A C B A A B C'
Line 474 ⟶ 533:
Data M: 'A B' Order N: 'B' --> 'A B'
Data M: 'A B' Order N: 'B A' --> 'B A'
Data M: 'A B B A' Order N: 'B A' --> 'B A B A'"</
Or with, say, lists instead of substrings:
<
set listOfLists2 to {{"aardvark", "duck-billed platypus", "banana"}, {1, 2, 3, 4, 5}}
return odli(listOfLists1, listOfLists2)</
{{output}}
<
=={{header|Arturo}}==
{{trans|Nim}}
<syntaxhighlight lang="rebol">orderDisjoint: function [m,n][
ms: split.words m
ns: split.words n
indexes: new []
loop ns 'item [
idx: index ms item
unless null? idx [
'indexes ++ idx
ms\[idx]: ""
]
]
sort 'indexes
loop.with:'i indexes 'idx ->
ms\[idx]: ns\[i]
return join.with:" " ms
]
process: function [a,b][
print [a "|" b "->" orderDisjoint a b]
]
process "the cat sat on the mat" "mat cat"
process "the cat sat on the mat" "cat mat"
process "A B C A B C A B C" "C A C A"
process "A B C A B D A B E" "E A D A"
process "A B" "B"
process "A B" "B A"
process "A B B A" "B A"</syntaxhighlight>
{{out}}
<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</pre>
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<
, {M: "the cat sat on the mat", N: "cat mat"}
, {M: "A B C A B C A B C", N: "C A C A"}
Line 507 ⟶ 611:
Result .= (ItemsN[A_LoopField]-- > 0 ? N.Remove(1) : A_LoopField) " "
return RTrim(Result)
}</
{{Output}}
<pre>the cat sat on the mat :: mat cat -> the mat sat on the cat
Line 518 ⟶ 622:
=={{header|Bracmat}}==
<
= M N NN item A Z R
. !arg:(?M.?N)
Line 552 ⟶ 656:
& out$(\t odli$(!M.!N))
)
);</
Output:
<pre>Data M: the cat sat on the mat Order N: mat cat the mat sat on the cat
Line 563 ⟶ 667:
=={{header|C++}}==
<
#include <iostream>
#include <vector>
Line 630 ⟶ 734:
std::cin.get();
return 0;
}</
{{out}}
Line 642 ⟶ 746:
=={{header|Common Lisp}}==
<
(let ((order-b (make-hash-table :test 'equal)))
(loop :for n :in order :do (incf (gethash n order-b 0)))
Line 649 ⟶ 753:
(decf (gethash m order-b))
(pop order))
(t m)))))</
{{out}}
<pre>CL-USER> (order-disjoint '(the cat sat on the mat) '(mat cat))
Line 669 ⟶ 773:
{{trans|Python}}
This version is not efficient.
<
std.conv;
Line 719 ⟶ 823:
orderDisjointArrayItems(a, b));
}
}</
{{out}}
<pre>the cat sat on the mat | mat cat -> the mat sat on the cat
Line 738 ⟶ 842:
=={{header|EchoLisp}}==
<
(lib 'list) ;; for list-delete
Line 770 ⟶ 874:
</syntaxhighlight>
{{out}}
<pre>
Line 786 ⟶ 890:
=={{header|Elixir}}==
<
def disjoint(m,n) do
IO.write "#{Enum.join(m," ")} | #{Enum.join(n," ")} -> "
Line 824 ⟶ 928:
|> Enum.each(fn {m,n} ->
Order.disjoint(String.split(m),String.split(n))
end)</
{{out}}
Line 839 ⟶ 943:
=={{header|Factor}}==
This solution is a tad bit whimsical (and a testament to the flexibility of the language that it allows something like this). <code>make-slots</code> replaces elements from ''M'' with <code>_</code> from the <code>fry</code> vocabulary according to the elements in ''N''. For example,
<
produces <code>{ "the" _ "sat" "on" "the" _ }</code>. Then, <code>reorder</code> fries elements from ''N'' into the sequence. This is much like a regular fried quotation.
Line 846 ⟶ 950:
Finally, <code>input<sequence</code> is a smart combinator (a combinator that infers the stack effect of one or more of its inputs) that takes a sequence and a quotation and makes it so that from inside the quotation, you can think of sequence elements as though they were data stack objects. This is precisely what we want so that we can fry them.
<
fry kernel qw sequences ;
IN: rosetta-code.order-disjoint-list
Line 872 ⟶ 976:
{ qw{ A B B A } qw{ B A } }
}
[ show-reordering ] assoc-each</
{{out}}
<pre>
Line 885 ⟶ 989:
=={{header|Go}}==
<
import (
Line 941 ⟶ 1,045:
}
}</
{{out}}
<pre>
Line 955 ⟶ 1,059:
=={{header|Haskell}}==
<
order
Line 987 ⟶ 1,091:
, ["A B", "B A"]
, ["A B B A", "B A"]
]</
{{out}}
<pre>
Line 1,003 ⟶ 1,107:
{{Trans|JavaScript}}
<syntaxhighlight lang
import Data.Bifunctor (bimap)
import Data.List (delete, transpose)
import Data.Text hiding
( concat,
foldl,
maximum,
transpose,
zipWith,
)
import Prelude hiding (length, unlines, unwords, words)
disjointOrder ::
[a] ->
[a]
disjointOrder m n = concat $ zipWith (<>) ms ns
where
ms = segments m n
ns = ((: []) <$> n) <> [[]]
[a] ->
[[a]]
segments m n = _m <> [_acc]
where
(_m, _, _acc) = foldl split ([], n, []) m
split ::
a ->
([[a]], [a], [a])
split (ms, ns, acc) x
| x `elem` ns = (ms
| otherwise = (ms, ns, acc
tests :: [(Text, Text)]
tests =
<$> [ ("the cat sat on the mat", "mat cat"),
]
table :: Text -> [[Text]] -> Text
table delim rows =
unlines $
intercalate delim
<$> transpose
( ( \col ->
let width = (length $ maximum col)
in justifyLeft width ' ' <$> col
<$> transpose rows
)
main :: IO ()
main =
(putStr . unpack) $
table (pack " -> ") $
[ m,
n,
unwords (disjointOrder (words m) (words n))
]
)
<$> tests</syntaxhighlight>
{{Out}}
<pre>the cat sat on the mat -> mat cat -> the mat sat on the cat
Line 1,072 ⟶ 1,195:
Works in both languages. Assumes a single blank separates items:
<
every write(" -> ",odli("the cat sat on the mat","mat cat"))
every write(" -> ",odli("the cat sat on the mat","cat mat"))
Line 1,094 ⟶ 1,217:
}
return Mp
end</
Output:
Line 1,113 ⟶ 1,236:
Implementation:
<
:
clusters=. (</. i.@#) x
Line 1,121 ⟶ 1,244:
to=. ;need {.!._ each order{clusters
(from{x) to} x
)</
Task examples:
<
the mat sat on the cat
'the cat sat on the mat' disjorder 'cat mat'
Line 1,138 ⟶ 1,261:
B A
'A B B A' disjorder 'B A'
B A B A</
=={{header|Java}}==
Doesn't handle the case when an item of N is not a member of M.
<
import java.util.BitSet;
import org.apache.commons.lang3.ArrayUtils;
Line 1,191 ⟶ 1,314:
return m;
}
}</
Output:
Line 1,210 ⟶ 1,333:
Accumulating a segmentation of M over a fold/reduce, and zipping with N:
<
// ------------ ORDER DISJOINT LIST ITEMS ------------
// disjointOrder :: [String] -> [String] -> [String]
const disjointOrder = ms =>
ns => zipWith(
a => b => [...a, b]
)(
segments(ms)(ns)
)(
ns.concat("")
)
.flat();
// segments :: [String] -> [String] -> [String]
const segments = ms =>
ns => {
const dct = ms.reduce((a, x) => {
const
wds = a.words,
found = wds.indexOf(x) !== -1;
return {
parts: [
...a.parts,
...(found ? [a.current] : [])
],
current: found ? [] : [...a.current, x],
words: found ? deleteFirst(x)(wds) : wds
};
}, {
words: ns,
parts: [],
current: []
});
return [...dct.parts, dct.current];
};
// ---------------------- TEST -----------------------
const main = () =>
transpose(transpose([{
M: "the cat sat on the mat",
N: "mat cat"
}, {
M: "the cat sat on the mat",
N: "cat mat"
}, {
M: "A B C A B C A B C",
N: "C A C A"
}, {
M: "A B C A B D A B E",
N: "E A D A"
}, {
M: "A B",
N: "B"
}, {
M: "A B",
N: "B A"
}, {
M: "A B B A",
N: "B A"
}].map(dct => [
dct.M, dct.N,
unwords(
disjointOrder(
words(dct.M)
)(
words(dct.N)
)
)
]))
.map(col => {
const
w = maximumBy(
comparing(x => x.length)
)(col).length;
return col.map(justifyLeft(w)(" "));
}))
.map(
([a, b, c]) => `${a} -> ${b} -> ${c}`
)
.join("\n");
// ---------------- GENERIC FUNCTIONS ----------------
// comparing :: (a -> b) -> (a -> a -> Ordering)
const comparing = f =>
// The ordering of f(x) and f(y) as a value
// drawn from {-1, 0, 1}, representing {LT, EQ, GT}.
x => y => {
const
a = f(x),
b = f(y);
return a < b ? -1 : (a > b ? 1 : 0);
};
// deleteFirst :: a -> [a] -> [a]
const deleteFirst =
const go = xs
x === xs[0] ? (
xs.slice(1)
) : [xs[0]].concat(
) : [];
};
// unwords :: [String] -> String
const unwords = xs =>
// A space-separated string derived
// from a list of words.
xs.join(" ");
// words :: String -> [String]
const words = s =>
// List of space-delimited sub-strings.
s.split(/\s+/u);
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith =
// default tuple constructor.
xs => ys => xs.map(
(x, i) => f(x)(ys[i])
).slice(
0, Math.min(xs.length, ys.length)
);
// ---------------- FORMATTING OUTPUT ----------------
// justifyLeft :: Int -> Char -> String
const justifyLeft = n =>
// The string s, followed by enough padding (with
// the character c) to reach the string length n.
c => s =>
)
// maximumBy :: (a -> a -> Ordering) -> [a] -> a
const maximumBy =
xs
x
) : a,
xs[0]
)
) : undefined;
//
const
// The columns of a matrix of consistent row length,
// transposed into corresponding rows.
Boolean(rows.length) ?
(_, i)
v => v[i]
)
) : [];
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
Line 1,374 ⟶ 1,525:
Usage: <tt>M | disjoint_order(N)</tt>
<
# The helper function, indices, ensures that successive occurrences
# of a particular value in N are matched by successive occurrences
Line 1,388 ⟶ 1,539:
. as $in
| (indices | sort) as $sorted
| reduce range(0; N|length) as $i ($in; .[$sorted[$i]] = N[$i] ) ;</
'''Examples''':
Line 1,394 ⟶ 1,545:
(scrollable)
<div style="overflow:scroll; height:400px;">
<
#=> ["the","mat","sat","on","the","cat"]</
<
#=> ["the","cat","sat","on","the","mat"]</
<
#=> ["C","B","A","C","B","A","A","B","C"]</
<
#=> ["E","B","C","A","B","D","A","B","A"]</
<
#=> ["A","B"]</
<
#=> ["B","A"]</
<
#=> ["B","A","B","A"]</
<
#=> [X, X, Y]</
</div>
Line 1,423 ⟶ 1,574:
'''Function'''
<syntaxhighlight lang="julia">
function order_disjoint{T<:AbstractArray}(m::T, n::T)
rlen = length(n)
Line 1,442 ⟶ 1,593:
return p
end
</syntaxhighlight>
'''Main'''
<syntaxhighlight lang="julia">
testm = {["the", "cat", "sat", "on", "the", "mat"],
["the", "cat", "sat", "on", "the", "mat"],
Line 1,469 ⟶ 1,620:
println(" (", m, ", ", n, ") => ", p)
end
</syntaxhighlight>
{{out}}
Line 1,483 ⟶ 1,634:
=={{header|Kotlin}}==
<
const val NULL = "\u0000"
Line 1,521 ⟶ 1,672:
for (i in 0 until m.size)
println("${m[i].padEnd(22)} -> ${n[i].padEnd(7)} -> ${orderDisjointList(m[i], n[i])}")
}</
{{out}}
Line 1,535 ⟶ 1,686:
=={{header|Lua}}==
<
function split (str)
local t = {}
Line 1,576 ⟶ 1,727:
for _, example in pairs(testCases) do
print(table.concat(orderList(unpack(example)), " "))
end</
{{out}}
<pre>the mat sat on the cat
Line 1,587 ⟶ 1,738:
=={{header|M2000 Interpreter}}==
===Simple===
<syntaxhighlight lang="m2000 interpreter">
Function Checkit$ {
Document Ret$
Line 1,622 ⟶ 1,773:
Report Checkit$()
Clipboard Checkit$()
</syntaxhighlight>
===Using a third array, sorted===
<syntaxhighlight lang="m2000 interpreter">
Function Checkit2$ {
Document Ret$
Line 1,660 ⟶ 1,811:
Report Checkit2$()
Clipboard Checkit2$()
</syntaxhighlight>
{{out}}
<pre>
Line 1,672 ⟶ 1,823:
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
ReplacePart[m,
MapThread[
Line 1,691 ⟶ 1,842:
Print[StringRiffle[order[{"A", "B"}, {"B"}]]];
Print[StringRiffle[order[{"A", "B"}, {"B", "A"}]]];
Print[StringRiffle[order[{"A", "B", "B", "A"}, {"B", "A"}]]];</
{{out}}
<pre>the mat sat on the cat
Line 1,700 ⟶ 1,851:
B A
B A B A</pre>
=={{header|Nim}}==
<syntaxhighlight lang="nim">import algorithm, strutils
proc orderDisjoint(m, n: string): string =
# Build the list of items.
var m = m.splitWhitespace()
let n = n.splitWhitespace()
# Find the indexes of items to replace.
var indexes: seq[int]
for item in n:
let idx = m.find(item)
if idx >= 0:
indexes.add idx
m[idx] = "" # Set to empty string for next searches.
indexes.sort()
# Do the replacements.
for i, idx in indexes:
m[idx] = n[i]
result = m.join(" ")
when isMainModule:
template process(a, b: string) =
echo a, " | ", b, " → ", orderDisjoint(a, b)
process("the cat sat on the mat", "mat cat")
process("the cat sat on the mat", "cat mat")
process("A B C A B C A B C", "C A C A")
process("A B C A B D A B E", "E A D A")
process("A B", "B")
process("A B", "B A")
process("A B B A", "B A")</syntaxhighlight>
{{out}}
<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</pre>
=={{header|Perl}}==
<
my ($m, $n) = @_;
my %h;
Line 1,722 ⟶ 1,921:
my ($a, $b) = map([split], split '\|');
print "@$a | @$b -> @{[dsort($a, $b)]}\n";
}</
{{out}}
<pre>
Line 1,738 ⟶ 1,937:
{{Trans|Julia}}
Modified to support/skip missing elements
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">order_disjoint</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rlen</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">rdis</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rlen</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">rlen</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rdis</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">rdis</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">j</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">rdis</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rdis</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">rlen</span> <span style="color: #008080;">and</span> <span style="color: #000000;">rdis</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">rdis</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rdis</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #000000;">rlen</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">rlen</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">m</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdis</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]]=</span><span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"the cat sat on the mat"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"mat cat"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"the cat sat on the mat"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"cat mat"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B C A B C A B C"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C A C A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B C A B D A B E"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"E A D A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"B"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"B A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B B A"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"B A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B B A"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"A B"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B A B"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"A B"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B A B"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"B A B A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B C C B A"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"A C A C"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A B C C B A"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"C A C A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Y A"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Y A X"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A X"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Y X A"</span><span style="color: #0000FF;">}}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\"%s\",\"%s\" => \"%s\"\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">order_disjoint</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,806 ⟶ 2,008:
=={{header|PicoLisp}}==
<
(for S N
(and (memq S M) (set @ NIL)) )
(mapcar
'((S) (or S (pop 'N)))
M ) )</
Test:
<
-> (the mat sat on the cat)
Line 1,832 ⟶ 2,034:
: (orderDisjoint '(A B B A) '(B A))
-> (B A B A)</
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function sublistsort($M, $N) {
$arr = $M.Split(' ')
Line 1,882 ⟶ 2,084:
sublistsort $M6 $N6
sublistsort $M7 $N7
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 1,897 ⟶ 2,099:
=={{header|Python}}==
<
def order_disjoint_list_items(data, items):
Line 1,933 ⟶ 2,135:
print('Data M: %-24r Order N: %-9r' % (tostring(data), tostring(items)), end=' ')
order_disjoint_list_items(data, items)
print("-> M' %r" % tostring(data))</
{{out}}
Line 1,953 ⟶ 2,155:
=={{header|Racket}}==
<
(define disjorder
(match-lambda**
Line 1,989 ⟶ 2,191:
(report-disjorder '(A B A B) '(B A B A))
(report-disjorder '(A B C C B A) '(A C A C))
(report-disjorder '(A B C C B A) '(C A C A))</
{{out}}
Line 2,011 ⟶ 2,213:
(formerly Perl 6)
{{works with|Rakudo|2018.03}}
<syntaxhighlight lang="raku"
my \bag = N.BagHash;
M.map: { bag{$_}-- ?? N.shift !! $_ }
Line 2,029 ⟶ 2,231:
A X Y A
---
-> $m, $n { say "\n$m ==> $n\n", order-disjoint-list-items($m, $n) }</
{{out}}
<pre>the cat sat on the mat ==> mat cat
Line 2,060 ⟶ 2,262:
=={{header|REXX}}==
Note: items in <big>'''N'''</big> needn't be in <big>'''M'''</big>.
<
used = '0'x /*indicates that a word has been parsed*/
@. = /*placeholder indicates end─of─array, */
Line 2,105 ⟶ 2,307:
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. */</
{{out|output|text= when using the internal default inputs:}}
<pre>
the cat sat on the mat | mat cat ───► the mat sat on the cat
Line 2,126 ⟶ 2,328:
=={{header|Ruby}}==
<
print "#{m} | #{n} -> "
m, n = m.split, n.split
Line 2,150 ⟶ 2,352:
['A B' , 'B A' ],
['A B B A' , 'B A' ]
].each {|m,n| order_disjoint(m,n)}</
{{out}}
<pre>
Line 2,162 ⟶ 2,364:
</pre>
===sprintf version===
<
['the cat sat on the mat', 'mat cat'],
['the cat sat on the mat', 'cat mat'],
Line 2,180 ⟶ 2,382:
puts res
</syntaxhighlight>
=={{header|Scala}}==
<
if (input.isEmpty || used.size >= using.size) input
else if (using diff used contains input.head)
using(used.size) +: order(input.tail, using, used :+ input.head)
else input.head +: order(input.tail, using, used)</
'''Test:'''
<
"the cat sat on the mat" -> "mat cat",
"the cat sat on the mat" -> "cat mat",
Line 2,202 ⟶ 2,404:
val done = order(input.split(" "), using.split(" "))
println(f"""Data M: $input%-24s Order N: $using%-9s -> Result M': ${done mkString " "}""")
}</
{{out}}
<pre>Data M: the cat sat on the mat Order N: mat cat -> Result M': the mat sat on the cat
Line 2,214 ⟶ 2,416:
=={{header|Sidef}}==
{{trans|Perl}}
<
var h = Hash()
n.each {|item| h{item} := 0 ++ }
Line 2,230 ⟶ 2,432:
EOT
var (a, b) = line.split('|').map{.words}...
say "#{a.
}</
{{out}}
<pre>
Line 2,245 ⟶ 2,447:
=={{header|Swift}}==
<
let replaceCounts = n.reduce(into: [T: Int](), { $0[$1, default: 0] += 1 })
let reduced = m.reduce(into: ([T](), n, replaceCounts), {cur, el in
Line 2,261 ⟶ 2,463:
print(disjointOrder(m: ["A", "B"], n: ["B"]))
print(disjointOrder(m: ["A", "B"], n: ["B", "A"]))
print(disjointOrder(m: ["A", "B", "B", "A"], n: ["B", "A"]))</
{{out}}
Line 2,274 ⟶ 2,476:
=={{header|Tcl}}==
This is a simple version that assumes that ''all'' items in the order list are present in the list to be arranged:
<
foreach item $theOrderList {incr n($item)}
set is {}
Line 2,286 ⟶ 2,488:
foreach item $theOrderList i $is {lset theList $i $item}
return $theList
}</
This is a more sophisticated version that handles items in the order list not being present in the list to be arranged:
<
foreach item $theOrderList {incr n($item)}
set is -
Line 2,305 ⟶ 2,507:
}
return $theList
}</
Demonstration code (produces the same output from both implementations):
<
"the cat sat on the mat" "mat cat"
"the cat sat on the mat" "cat mat"
Line 2,317 ⟶ 2,519:
} {
puts "'$items' with '$order' => '[orderDisjoint $items $order]'"
}</
{{out}}
<pre>
Line 2,332 ⟶ 2,534:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<
var NULL = "\0"
Line 2,376 ⟶ 2,578:
for (i in 0...ma.count) {
Fmt.print("$-22s -> $-7s -> $s", ma[i], na[i], orderDisjointList.call(ma[i], na[i]))
}</
{{out}}
Line 2,390 ⟶ 2,592:
=={{header|zkl}}==
<
M:=sm.split(" ");
N:=sn.split(" "); nc:=Walker.cycle(Utils.Helpers.listUnique(N));
Line 2,399 ⟶ 2,601:
else { nc.next(); w } // exhausted
}, String.fp(" ") )[1,*] // remove leading blank
}</
A dictionary is used to hold count of the words in N, which is decremented as the words are used up.
A cycle of the words is consumed to track the replacement values.
It is assumed that there are no leading/trailing/consecutive spaces (easy to cover with a .filter()).
<
T("the cat sat on the mat","cat mat"),
T("A B C A B C A B C","C A C A"),
Line 2,410 ⟶ 2,612:
foreach m,n in (sets){
m.println(" / ",n," --> ",disOrder(m,n));
}</
{{out}}
<pre>
|