Order disjoint list items: Difference between revisions
Content deleted Content added
m →{{header|Haskell}}: Tidied |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 46: | Line 46: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F order_disjoint_list_items(&data, items) |
||
[Int] itemindices |
[Int] itemindices |
||
L(item) Set(items) |
L(item) Set(items) |
||
Line 81: | Line 81: | ||
print(‘Data M: #<24 Order N: #<9’.format(tostring(data), tostring(items)), end' ‘ ’) |
print(‘Data M: #<24 Order N: #<9’.format(tostring(data), tostring(items)), end' ‘ ’) |
||
order_disjoint_list_items(&data, items) |
order_disjoint_list_items(&data, items) |
||
print(‘-> M' #.’.format(tostring(data)))</ |
print(‘-> M' #.’.format(tostring(data)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 103: | Line 103: | ||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">order(list a, b) |
||
{ |
{ |
||
integer j; |
integer j; |
||
Line 143: | Line 143: | ||
0; |
0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>the cat sat on the mat | mat cat -> the mat sat on the cat |
<pre>the cat sat on the mat | mat cat -> the mat sat on the cat |
||
Line 158: | Line 158: | ||
Accumulate a segmentation of M over a fold/reduce, and zip with N: |
Accumulate a segmentation of M over a fold/reduce, and zip with N: |
||
< |
<syntaxhighlight lang="applescript">---------------------- DISJOINT ORDER -------------------- |
||
-- disjointOrder :: String -> String -> String |
-- disjointOrder :: String -> String -> String |
||
Line 467: | Line 467: | ||
map(pair, items 1 thru minimum([length of xs, length of ys]) of xs) |
map(pair, items 1 thru minimum([length of xs, length of ys]) of xs) |
||
end zip</ |
end zip</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>the cat sat on the mat -> mat cat -> the mat sat on the cat |
<pre>the cat sat on the mat -> mat cat -> the mat sat on the cat |
||
Line 480: | Line 480: | ||
===Idiomatic=== |
===Idiomatic=== |
||
< |
<syntaxhighlight lang="applescript">(* |
||
The task description talks about items in lists, but the examples are space-delimited substrings of strings. |
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. |
The handler here deals with items in lists and leaves it to the calling code to sort out the rest. |
||
Line 524: | Line 524: | ||
set output to output as text |
set output to output as text |
||
set AppleScript's text item delimiters to astid |
set AppleScript's text item delimiters to astid |
||
return output</ |
return output</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">"Data M: 'the cat sat on the mat' Order N: 'mat cat' --> 'the mat sat on the cat' |
||
Data M: 'the cat sat on the mat' Order N: 'cat mat' --> 'the cat sat on the mat' |
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' |
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 533: | Line 533: | ||
Data M: 'A B' Order N: 'B' --> 'A B' |
Data M: 'A B' Order N: 'B' --> 'A B' |
||
Data M: 'A B' Order N: 'B A' --> 'B A' |
Data M: 'A B' Order N: 'B A' --> 'B A' |
||
Data M: 'A B B A' Order N: 'B A' --> 'B A B A'"</ |
Data M: 'A B B A' Order N: 'B A' --> 'B A B A'"</syntaxhighlight> |
||
Or with, say, lists instead of substrings: |
Or with, say, lists instead of substrings: |
||
< |
<syntaxhighlight lang="applescript">set listOfLists1 to {{1, 2, 3, 4, 5}, {5, 4, 3, 2, 1}, {"aardvark", "duck-billed platypus", "banana"}} |
||
set listOfLists2 to {{"aardvark", "duck-billed platypus", "banana"}, {1, 2, 3, 4, 5}} |
set listOfLists2 to {{"aardvark", "duck-billed platypus", "banana"}, {1, 2, 3, 4, 5}} |
||
return odli(listOfLists1, listOfLists2)</ |
return odli(listOfLists1, listOfLists2)</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight lang="applescript">{{"aardvark", "duck-billed platypus", "banana"}, {5, 4, 3, 2, 1}, {1, 2, 3, 4, 5}}</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="rebol">orderDisjoint: function [m,n][ |
||
ms: split.words m |
ms: split.words m |
||
ns: split.words n |
ns: split.words n |
||
Line 577: | Line 577: | ||
process "A B" "B" |
process "A B" "B" |
||
process "A B" "B A" |
process "A B" "B A" |
||
process "A B B A" "B A"</ |
process "A B B A" "B A"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 591: | Line 591: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{works with|AutoHotkey 1.1}} |
{{works with|AutoHotkey 1.1}} |
||
< |
<syntaxhighlight lang="autohotkey">Data := [ {M: "the cat sat on the mat", N: "mat cat"} |
||
, {M: "the cat sat on the mat", N: "cat mat"} |
, {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 C A B C", N: "C A C A"} |
||
Line 611: | Line 611: | ||
Result .= (ItemsN[A_LoopField]-- > 0 ? N.Remove(1) : A_LoopField) " " |
Result .= (ItemsN[A_LoopField]-- > 0 ? N.Remove(1) : A_LoopField) " " |
||
return RTrim(Result) |
return RTrim(Result) |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre>the cat sat on the mat :: mat cat -> the mat sat on the cat |
<pre>the cat sat on the mat :: mat cat -> the mat sat on the cat |
||
Line 622: | Line 622: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( ( odli |
||
= M N NN item A Z R |
= M N NN item A Z R |
||
. !arg:(?M.?N) |
. !arg:(?M.?N) |
||
Line 656: | Line 656: | ||
& out$(\t odli$(!M.!N)) |
& out$(\t odli$(!M.!N)) |
||
) |
) |
||
);</ |
);</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>Data M: the cat sat on the mat Order N: mat cat the mat sat on the cat |
<pre>Data M: the cat sat on the mat Order N: mat cat the mat sat on the cat |
||
Line 667: | Line 667: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 734: | Line 734: | ||
std::cin.get(); |
std::cin.get(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 746: | Line 746: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun order-disjoint (data order) |
||
(let ((order-b (make-hash-table :test 'equal))) |
(let ((order-b (make-hash-table :test 'equal))) |
||
(loop :for n :in order :do (incf (gethash n order-b 0))) |
(loop :for n :in order :do (incf (gethash n order-b 0))) |
||
Line 753: | Line 753: | ||
(decf (gethash m order-b)) |
(decf (gethash m order-b)) |
||
(pop order)) |
(pop order)) |
||
(t m)))))</ |
(t m)))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>CL-USER> (order-disjoint '(the cat sat on the mat) '(mat cat)) |
<pre>CL-USER> (order-disjoint '(the cat sat on the mat) '(mat cat)) |
||
Line 773: | Line 773: | ||
{{trans|Python}} |
{{trans|Python}} |
||
This version is not efficient. |
This version is not efficient. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.string, std.algorithm, std.array, std.range, |
||
std.conv; |
std.conv; |
||
Line 823: | Line 823: | ||
orderDisjointArrayItems(a, b)); |
orderDisjointArrayItems(a, b)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>the cat sat on the mat | mat cat -> the mat sat on the cat |
<pre>the cat sat on the mat | mat cat -> the mat sat on the cat |
||
Line 842: | Line 842: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(lib 'list) ;; for list-delete |
(lib 'list) ;; for list-delete |
||
Line 874: | Line 874: | ||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 890: | Line 890: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Order do |
||
def disjoint(m,n) do |
def disjoint(m,n) do |
||
IO.write "#{Enum.join(m," ")} | #{Enum.join(n," ")} -> " |
IO.write "#{Enum.join(m," ")} | #{Enum.join(n," ")} -> " |
||
Line 928: | Line 928: | ||
|> Enum.each(fn {m,n} -> |
|> Enum.each(fn {m,n} -> |
||
Order.disjoint(String.split(m),String.split(n)) |
Order.disjoint(String.split(m),String.split(n)) |
||
end)</ |
end)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 943: | Line 943: | ||
=={{header|Factor}}== |
=={{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, |
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, |
||
< |
<syntaxhighlight lang="factor">qw{ the cat sat on the mat } qw{ mat cat } make-slots</syntaxhighlight> |
||
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. |
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 950: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="factor">USING: assocs combinators combinators.smart effects formatting |
||
fry kernel qw sequences ; |
fry kernel qw sequences ; |
||
IN: rosetta-code.order-disjoint-list |
IN: rosetta-code.order-disjoint-list |
||
Line 976: | Line 976: | ||
{ qw{ A B B A } qw{ B A } } |
{ qw{ A B B A } qw{ B A } } |
||
} |
} |
||
[ show-reordering ] assoc-each</ |
[ show-reordering ] assoc-each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 989: | Line 989: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,045: | Line 1,045: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,059: | Line 1,059: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (mapAccumL, sort) |
||
order |
order |
||
Line 1,091: | Line 1,091: | ||
, ["A B", "B A"] |
, ["A B", "B A"] |
||
, ["A B B A", "B A"] |
, ["A B B A", "B A"] |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,107: | Line 1,107: | ||
{{Trans|JavaScript}} |
{{Trans|JavaScript}} |
||
< |
<syntaxhighlight lang="haskell">import Control.Monad (join) |
||
import Data.Bifunctor (bimap) |
import Data.Bifunctor (bimap) |
||
import Data.List (delete, transpose) |
import Data.List (delete, transpose) |
||
Line 1,181: | Line 1,181: | ||
] |
] |
||
) |
) |
||
<$> tests</ |
<$> tests</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>the cat sat on the mat -> mat cat -> the mat sat on the cat |
<pre>the cat sat on the mat -> mat cat -> the mat sat on the cat |
||
Line 1,195: | Line 1,195: | ||
Works in both languages. Assumes a single blank separates items: |
Works in both languages. Assumes a single blank separates items: |
||
< |
<syntaxhighlight lang="unicon">procedure main(A) |
||
every write(" -> ",odli("the cat sat on the mat","mat cat")) |
every write(" -> ",odli("the cat sat on the mat","mat cat")) |
||
every write(" -> ",odli("the cat sat on the mat","cat mat")) |
every write(" -> ",odli("the cat sat on the mat","cat mat")) |
||
Line 1,217: | Line 1,217: | ||
} |
} |
||
return Mp |
return Mp |
||
end</ |
end</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,236: | Line 1,236: | ||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">disjorder=:3 :0&.;: |
||
: |
: |
||
clusters=. (</. i.@#) x |
clusters=. (</. i.@#) x |
||
Line 1,244: | Line 1,244: | ||
to=. ;need {.!._ each order{clusters |
to=. ;need {.!._ each order{clusters |
||
(from{x) to} x |
(from{x) to} x |
||
)</ |
)</syntaxhighlight> |
||
Task examples: |
Task examples: |
||
< |
<syntaxhighlight lang="j"> 'the cat sat on the mat' disjorder 'mat cat' |
||
the mat sat on the cat |
the mat sat on the cat |
||
'the cat sat on the mat' disjorder 'cat mat' |
'the cat sat on the mat' disjorder 'cat mat' |
||
Line 1,261: | Line 1,261: | ||
B A |
B A |
||
'A B B A' disjorder 'B A' |
'A B B A' disjorder 'B A' |
||
B A B A</ |
B A B A</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Doesn't handle the case when an item of N is not a member of M. |
Doesn't handle the case when an item of N is not a member of M. |
||
< |
<syntaxhighlight lang="java">import java.util.Arrays; |
||
import java.util.BitSet; |
import java.util.BitSet; |
||
import org.apache.commons.lang3.ArrayUtils; |
import org.apache.commons.lang3.ArrayUtils; |
||
Line 1,314: | Line 1,314: | ||
return m; |
return m; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 1,333: | Line 1,333: | ||
Accumulating a segmentation of M over a fold/reduce, and zipping with N: |
Accumulating a segmentation of M over a fold/reduce, and zipping with N: |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
"use strict"; |
"use strict"; |
||
Line 1,510: | Line 1,510: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,525: | Line 1,525: | ||
Usage: <tt>M | disjoint_order(N)</tt> |
Usage: <tt>M | disjoint_order(N)</tt> |
||
< |
<syntaxhighlight lang="jq">def disjoint_order(N): |
||
# The helper function, indices, ensures that successive occurrences |
# The helper function, indices, ensures that successive occurrences |
||
# of a particular value in N are matched by successive occurrences |
# of a particular value in N are matched by successive occurrences |
||
Line 1,539: | Line 1,539: | ||
. as $in |
. as $in |
||
| (indices | sort) as $sorted |
| (indices | sort) as $sorted |
||
| reduce range(0; N|length) as $i ($in; .[$sorted[$i]] = N[$i] ) ;</ |
| reduce range(0; N|length) as $i ($in; .[$sorted[$i]] = N[$i] ) ;</syntaxhighlight> |
||
'''Examples''': |
'''Examples''': |
||
Line 1,545: | Line 1,545: | ||
(scrollable) |
(scrollable) |
||
<div style="overflow:scroll; height:400px;"> |
<div style="overflow:scroll; height:400px;"> |
||
< |
<syntaxhighlight lang="jq">["the", "cat", "sat", "on", "the", "mat"] | indices( ["mat", "cat"] ) |
||
#=> ["the","mat","sat","on","the","cat"]</ |
#=> ["the","mat","sat","on","the","cat"]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">["the", "cat", "sat", "on", "the", "mat"] | disjoint_order( ["cat", "mat"] ) |
||
#=> ["the","cat","sat","on","the","mat"]</ |
#=> ["the","cat","sat","on","the","mat"]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">["A", "B", "C", "A", "B", "C", "A", "B", "C"] | disjoint_order( ["C", "A", "C", "A"] ) |
||
#=> ["C","B","A","C","B","A","A","B","C"]</ |
#=> ["C","B","A","C","B","A","A","B","C"]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">["A", "B", "C", "A", "B", "D", "A", "B", "E"] | disjoint_order( ["E", "A", "D", "A"] ) |
||
#=> ["E","B","C","A","B","D","A","B","A"]</ |
#=> ["E","B","C","A","B","D","A","B","A"]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">["A", "B"] | disjoint_order( ["B"] ) |
||
#=> ["A","B"]</ |
#=> ["A","B"]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">["A", "B"] | disjoint_order( ["B", "A"] ) |
||
#=> ["B","A"]</ |
#=> ["B","A"]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">["A", "B", "B", "A"] | disjoint_order( ["B", "A"] ) |
||
#=> ["B","A","B","A"]</ |
#=> ["B","A","B","A"]</syntaxhighlight> |
||
< |
<syntaxhighlight lang="jq">["X", "X", "Y"] | disjoint_order(["X"]) |
||
#=> [X, X, Y]</ |
#=> [X, X, Y]</syntaxhighlight> |
||
</div> |
</div> |
||
Line 1,574: | Line 1,574: | ||
'''Function''' |
'''Function''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
function order_disjoint{T<:AbstractArray}(m::T, n::T) |
function order_disjoint{T<:AbstractArray}(m::T, n::T) |
||
rlen = length(n) |
rlen = length(n) |
||
Line 1,593: | Line 1,593: | ||
return p |
return p |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Main''' |
'''Main''' |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
testm = {["the", "cat", "sat", "on", "the", "mat"], |
testm = {["the", "cat", "sat", "on", "the", "mat"], |
||
["the", "cat", "sat", "on", "the", "mat"], |
["the", "cat", "sat", "on", "the", "mat"], |
||
Line 1,620: | Line 1,620: | ||
println(" (", m, ", ", n, ") => ", p) |
println(" (", m, ", ", n, ") => ", p) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,634: | Line 1,634: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
const val NULL = "\u0000" |
const val NULL = "\u0000" |
||
Line 1,672: | Line 1,672: | ||
for (i in 0 until m.size) |
for (i in 0 until m.size) |
||
println("${m[i].padEnd(22)} -> ${n[i].padEnd(7)} -> ${orderDisjointList(m[i], n[i])}") |
println("${m[i].padEnd(22)} -> ${n[i].padEnd(7)} -> ${orderDisjointList(m[i], n[i])}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,686: | Line 1,686: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- Split str on any space characters and return as a table |
||
function split (str) |
function split (str) |
||
local t = {} |
local t = {} |
||
Line 1,727: | Line 1,727: | ||
for _, example in pairs(testCases) do |
for _, example in pairs(testCases) do |
||
print(table.concat(orderList(unpack(example)), " ")) |
print(table.concat(orderList(unpack(example)), " ")) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>the mat sat on the cat |
<pre>the mat sat on the cat |
||
Line 1,738: | Line 1,738: | ||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
===Simple=== |
===Simple=== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Function Checkit$ { |
Function Checkit$ { |
||
Document Ret$ |
Document Ret$ |
||
Line 1,773: | Line 1,773: | ||
Report Checkit$() |
Report Checkit$() |
||
Clipboard Checkit$() |
Clipboard Checkit$() |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Using a third array, sorted=== |
===Using a third array, sorted=== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Function Checkit2$ { |
Function Checkit2$ { |
||
Document Ret$ |
Document Ret$ |
||
Line 1,811: | Line 1,811: | ||
Report Checkit2$() |
Report Checkit2$() |
||
Clipboard Checkit2$() |
Clipboard Checkit2$() |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,824: | Line 1,824: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">order[m_, n_] := |
||
ReplacePart[m, |
ReplacePart[m, |
||
MapThread[ |
MapThread[ |
||
Line 1,842: | Line 1,842: | ||
Print[StringRiffle[order[{"A", "B"}, {"B"}]]]; |
Print[StringRiffle[order[{"A", "B"}, {"B"}]]]; |
||
Print[StringRiffle[order[{"A", "B"}, {"B", "A"}]]]; |
Print[StringRiffle[order[{"A", "B"}, {"B", "A"}]]]; |
||
Print[StringRiffle[order[{"A", "B", "B", "A"}, {"B", "A"}]]];</ |
Print[StringRiffle[order[{"A", "B", "B", "A"}, {"B", "A"}]]];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>the mat sat on the cat |
<pre>the mat sat on the cat |
||
Line 1,853: | Line 1,853: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import algorithm, strutils |
||
Line 1,889: | Line 1,889: | ||
process("A B", "B") |
process("A B", "B") |
||
process("A B", "B A") |
process("A B", "B A") |
||
process("A B B A", "B A")</ |
process("A B B A", "B A")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,901: | Line 1,901: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">sub dsort { |
||
my ($m, $n) = @_; |
my ($m, $n) = @_; |
||
my %h; |
my %h; |
||
Line 1,921: | Line 1,921: | ||
my ($a, $b) = map([split], split '\|'); |
my ($a, $b) = map([split], split '\|'); |
||
print "@$a | @$b -> @{[dsort($a, $b)]}\n"; |
print "@$a | @$b -> @{[dsort($a, $b)]}\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,937: | Line 1,937: | ||
{{Trans|Julia}} |
{{Trans|Julia}} |
||
Modified to support/skip missing elements |
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;">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: #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> |
||
Line 1,984: | Line 1,984: | ||
<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: #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> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,008: | Line 2,008: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de orderDisjoint (M N) |
||
(for S N |
(for S N |
||
(and (memq S M) (set @ NIL)) ) |
(and (memq S M) (set @ NIL)) ) |
||
(mapcar |
(mapcar |
||
'((S) (or S (pop 'N))) |
'((S) (or S (pop 'N))) |
||
M ) )</ |
M ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp">: (orderDisjoint '(the cat sat on the mat) '(mat cat)) |
||
-> (the mat sat on the cat) |
-> (the mat sat on the cat) |
||
Line 2,034: | Line 2,034: | ||
: (orderDisjoint '(A B B A) '(B A)) |
: (orderDisjoint '(A B B A) '(B A)) |
||
-> (B A B A)</ |
-> (B A B A)</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function sublistsort($M, $N) { |
function sublistsort($M, $N) { |
||
$arr = $M.Split(' ') |
$arr = $M.Split(' ') |
||
Line 2,084: | Line 2,084: | ||
sublistsort $M6 $N6 |
sublistsort $M6 $N6 |
||
sublistsort $M7 $N7 |
sublistsort $M7 $N7 |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 2,099: | Line 2,099: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">from __future__ import print_function |
||
def order_disjoint_list_items(data, items): |
def order_disjoint_list_items(data, items): |
||
Line 2,135: | Line 2,135: | ||
print('Data M: %-24r Order N: %-9r' % (tostring(data), tostring(items)), end=' ') |
print('Data M: %-24r Order N: %-9r' % (tostring(data), tostring(items)), end=' ') |
||
order_disjoint_list_items(data, items) |
order_disjoint_list_items(data, items) |
||
print("-> M' %r" % tostring(data))</ |
print("-> M' %r" % tostring(data))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,155: | Line 2,155: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define disjorder |
(define disjorder |
||
(match-lambda** |
(match-lambda** |
||
Line 2,191: | Line 2,191: | ||
(report-disjorder '(A B A B) '(B A B A)) |
(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) '(A C A C)) |
||
(report-disjorder '(A B C C B A) '(C A C A))</ |
(report-disjorder '(A B C C B A) '(C A C A))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,213: | Line 2,213: | ||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|2018.03}} |
{{works with|Rakudo|2018.03}} |
||
<lang |
<syntaxhighlight lang="raku" line>sub order-disjoint-list-items(\M, \N) { |
||
my \bag = N.BagHash; |
my \bag = N.BagHash; |
||
M.map: { bag{$_}-- ?? N.shift !! $_ } |
M.map: { bag{$_}-- ?? N.shift !! $_ } |
||
Line 2,231: | Line 2,231: | ||
A X Y A |
A X Y A |
||
--- |
--- |
||
-> $m, $n { say "\n$m ==> $n\n", order-disjoint-list-items($m, $n) }</ |
-> $m, $n { say "\n$m ==> $n\n", order-disjoint-list-items($m, $n) }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>the cat sat on the mat ==> mat cat |
<pre>the cat sat on the mat ==> mat cat |
||
Line 2,262: | Line 2,262: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Note: items in <big>'''N'''</big> needn't be in <big>'''M'''</big>. |
Note: items in <big>'''N'''</big> needn't be in <big>'''M'''</big>. |
||
< |
<syntaxhighlight 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 2,307: | Line 2,307: | ||
say @.j ' ────► ' space(mp) /*display new re─ordered text ──► term.*/ |
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. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= when using the internal default inputs:}} |
{{out|output|text= when using the internal default inputs:}} |
||
<pre> |
<pre> |
||
Line 2,328: | Line 2,328: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def order_disjoint(m,n) |
||
print "#{m} | #{n} -> " |
print "#{m} | #{n} -> " |
||
m, n = m.split, n.split |
m, n = m.split, n.split |
||
Line 2,352: | Line 2,352: | ||
['A B' , 'B A' ], |
['A B' , 'B A' ], |
||
['A B B A' , 'B A' ] |
['A B B A' , 'B A' ] |
||
].each {|m,n| order_disjoint(m,n)}</ |
].each {|m,n| order_disjoint(m,n)}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,364: | Line 2,364: | ||
</pre> |
</pre> |
||
===sprintf version=== |
===sprintf version=== |
||
< |
<syntaxhighlight lang="ruby">ar = [ |
||
['the cat sat on the mat', 'mat cat'], |
['the cat sat on the mat', 'mat cat'], |
||
['the cat sat on the mat', 'cat mat'], |
['the cat sat on the mat', 'cat mat'], |
||
Line 2,382: | Line 2,382: | ||
puts res |
puts res |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">def order[T](input: Seq[T], using: Seq[T], used: Seq[T] = Seq()): Seq[T] = |
||
if (input.isEmpty || used.size >= using.size) input |
if (input.isEmpty || used.size >= using.size) input |
||
else if (using diff used contains input.head) |
else if (using diff used contains input.head) |
||
using(used.size) +: order(input.tail, using, used :+ input.head) |
using(used.size) +: order(input.tail, using, used :+ input.head) |
||
else input.head +: order(input.tail, using, used)</ |
else input.head +: order(input.tail, using, used)</syntaxhighlight> |
||
'''Test:''' |
'''Test:''' |
||
< |
<syntaxhighlight lang="scala">val tests = List( |
||
"the cat sat on the mat" -> "mat cat", |
"the cat sat on the mat" -> "mat cat", |
||
"the cat sat on the mat" -> "cat mat", |
"the cat sat on the mat" -> "cat mat", |
||
Line 2,404: | Line 2,404: | ||
val done = order(input.split(" "), using.split(" ")) |
val done = order(input.split(" "), using.split(" ")) |
||
println(f"""Data M: $input%-24s Order N: $using%-9s -> Result M': ${done mkString " "}""") |
println(f"""Data M: $input%-24s Order N: $using%-9s -> Result M': ${done mkString " "}""") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Data M: the cat sat on the mat Order N: mat cat -> Result M': the mat sat on the cat |
<pre>Data M: the cat sat on the mat Order N: mat cat -> Result M': the mat sat on the cat |
||
Line 2,416: | Line 2,416: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">func dsort(m, n) { |
||
var h = Hash() |
var h = Hash() |
||
n.each {|item| h{item} := 0 ++ } |
n.each {|item| h{item} := 0 ++ } |
||
Line 2,433: | Line 2,433: | ||
var (a, b) = line.split('|').map{.words}... |
var (a, b) = line.split('|').map{.words}... |
||
say "#{a.to_s} | #{b.to_s} -> #{dsort(a.clone, b.clone).to_s}" |
say "#{a.to_s} | #{b.to_s} -> #{dsort(a.clone, b.clone).to_s}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,447: | Line 2,447: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">func disjointOrder<T: Hashable>(m: [T], n: [T]) -> [T] { |
||
let replaceCounts = n.reduce(into: [T: Int](), { $0[$1, default: 0] += 1 }) |
let replaceCounts = n.reduce(into: [T: Int](), { $0[$1, default: 0] += 1 }) |
||
let reduced = m.reduce(into: ([T](), n, replaceCounts), {cur, el in |
let reduced = m.reduce(into: ([T](), n, replaceCounts), {cur, el in |
||
Line 2,463: | Line 2,463: | ||
print(disjointOrder(m: ["A", "B"], n: ["B"])) |
print(disjointOrder(m: ["A", "B"], n: ["B"])) |
||
print(disjointOrder(m: ["A", "B"], n: ["B", "A"])) |
print(disjointOrder(m: ["A", "B"], n: ["B", "A"])) |
||
print(disjointOrder(m: ["A", "B", "B", "A"], n: ["B", "A"]))</ |
print(disjointOrder(m: ["A", "B", "B", "A"], n: ["B", "A"]))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,476: | Line 2,476: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
This is a simple version that assumes that ''all'' items in the order list are present in the list to be arranged: |
This is a simple version that assumes that ''all'' items in the order list are present in the list to be arranged: |
||
< |
<syntaxhighlight lang="tcl">proc orderDisjoint {theList theOrderList} { |
||
foreach item $theOrderList {incr n($item)} |
foreach item $theOrderList {incr n($item)} |
||
set is {} |
set is {} |
||
Line 2,488: | Line 2,488: | ||
foreach item $theOrderList i $is {lset theList $i $item} |
foreach item $theOrderList i $is {lset theList $i $item} |
||
return $theList |
return $theList |
||
}</ |
}</syntaxhighlight> |
||
This is a more sophisticated version that handles items in the order list not being present in the list to be arranged: |
This is a more sophisticated version that handles items in the order list not being present in the list to be arranged: |
||
< |
<syntaxhighlight lang="tcl">proc orderDisjoint {theList theOrderList} { |
||
foreach item $theOrderList {incr n($item)} |
foreach item $theOrderList {incr n($item)} |
||
set is - |
set is - |
||
Line 2,507: | Line 2,507: | ||
} |
} |
||
return $theList |
return $theList |
||
}</ |
}</syntaxhighlight> |
||
Demonstration code (produces the same output from both implementations): |
Demonstration code (produces the same output from both implementations): |
||
< |
<syntaxhighlight lang="tcl">foreach {items order} { |
||
"the cat sat on the mat" "mat cat" |
"the cat sat on the mat" "mat cat" |
||
"the cat sat on the mat" "cat mat" |
"the cat sat on the mat" "cat mat" |
||
Line 2,519: | Line 2,519: | ||
} { |
} { |
||
puts "'$items' with '$order' => '[orderDisjoint $items $order]'" |
puts "'$items' with '$order' => '[orderDisjoint $items $order]'" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,534: | Line 2,534: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var NULL = "\0" |
var NULL = "\0" |
||
Line 2,578: | Line 2,578: | ||
for (i in 0...ma.count) { |
for (i in 0...ma.count) { |
||
Fmt.print("$-22s -> $-7s -> $s", ma[i], na[i], orderDisjointList.call(ma[i], na[i])) |
Fmt.print("$-22s -> $-7s -> $s", ma[i], na[i], orderDisjointList.call(ma[i], na[i])) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,592: | Line 2,592: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn disOrder(sm,sn){ |
||
M:=sm.split(" "); |
M:=sm.split(" "); |
||
N:=sn.split(" "); nc:=Walker.cycle(Utils.Helpers.listUnique(N)); |
N:=sn.split(" "); nc:=Walker.cycle(Utils.Helpers.listUnique(N)); |
||
Line 2,601: | Line 2,601: | ||
else { nc.next(); w } // exhausted |
else { nc.next(); w } // exhausted |
||
}, String.fp(" ") )[1,*] // remove leading blank |
}, String.fp(" ") )[1,*] // remove leading blank |
||
}</ |
}</syntaxhighlight> |
||
A dictionary is used to hold count of the words in N, which is decremented as the words are used up. |
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. |
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()). |
It is assumed that there are no leading/trailing/consecutive spaces (easy to cover with a .filter()). |
||
< |
<syntaxhighlight lang="zkl">sets:=T(T("the cat sat on the mat","mat cat"), |
||
T("the cat sat on the mat","cat mat"), |
T("the cat sat on the mat","cat mat"), |
||
T("A B C A B C A B C","C A C A"), |
T("A B C A B C A B C","C A C A"), |
||
Line 2,612: | Line 2,612: | ||
foreach m,n in (sets){ |
foreach m,n in (sets){ |
||
m.println(" / ",n," --> ",disOrder(m,n)); |
m.println(" / ",n," --> ",disOrder(m,n)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |