2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
30
Task/Ordered-Partitions/Elixir/ordered-partitions.elixir
Normal file
30
Task/Ordered-Partitions/Elixir/ordered-partitions.elixir
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
defmodule Ordered do
|
||||
def partition([]), do: [[]]
|
||||
def partition(mask) do
|
||||
sum = Enum.sum(mask)
|
||||
if sum == 0 do
|
||||
[Enum.map(mask, fn _ -> [] end)]
|
||||
else
|
||||
Enum.to_list(1..sum)
|
||||
|> permute
|
||||
|> Enum.reduce([], fn perm,acc ->
|
||||
{_, part} = Enum.reduce(mask, {perm,[]}, fn num,{pm,a} ->
|
||||
{p, rest} = Enum.split(pm, num)
|
||||
{rest, [Enum.sort(p) | a]}
|
||||
end)
|
||||
[Enum.reverse(part) | acc]
|
||||
end)
|
||||
|> Enum.uniq
|
||||
end
|
||||
end
|
||||
|
||||
defp permute([]), do: [[]]
|
||||
defp permute(list), do: for x <- list, y <- permute(list -- [x]), do: [x|y]
|
||||
end
|
||||
|
||||
Enum.each([[],[0,0,0],[1,1,1],[2,0,2]], fn test_case ->
|
||||
IO.puts "\npartitions #{inspect test_case}:"
|
||||
Enum.each(Ordered.partition(test_case), fn part ->
|
||||
IO.inspect part
|
||||
end)
|
||||
end)
|
||||
22
Task/Ordered-Partitions/J/ordered-partitions-3.j
Normal file
22
Task/Ordered-Partitions/J/ordered-partitions-3.j
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
+/\. 2 0 2
|
||||
4 2 2
|
||||
({@comb&.> +/\.) 2 0 2
|
||||
┌─────────────────────────┬──┬─────┐
|
||||
│┌───┬───┬───┬───┬───┬───┐│┌┐│┌───┐│
|
||||
││0 1│0 2│0 3│1 2│1 3│2 3││││││0 1││
|
||||
│└───┴───┴───┴───┴───┴───┘│└┘│└───┘│
|
||||
└─────────────────────────┴──┴─────┘
|
||||
>@,@{@({@comb&.> +/\.) 2 0 2
|
||||
┌───┬┬───┐
|
||||
│0 1││0 1│
|
||||
├───┼┼───┤
|
||||
│0 2││0 1│
|
||||
├───┼┼───┤
|
||||
│0 3││0 1│
|
||||
├───┼┼───┤
|
||||
│1 2││0 1│
|
||||
├───┼┼───┤
|
||||
│1 3││0 1│
|
||||
├───┼┼───┤
|
||||
│2 3││0 1│
|
||||
└───┴┴───┘
|
||||
12
Task/Ordered-Partitions/J/ordered-partitions-4.j
Normal file
12
Task/Ordered-Partitions/J/ordered-partitions-4.j
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
([,] {L:0 (i.@#@, -. [)&;)/0 1;0 1
|
||||
┌───┬───┐
|
||||
│0 1│2 3│
|
||||
└───┴───┘
|
||||
([,] {L:0 (i.@#@, -. [)&;)/0 1;0 1;0 1
|
||||
┌───┬───┬───┐
|
||||
│0 1│2 3│4 5│
|
||||
└───┴───┴───┘
|
||||
([,] {L:0 (i.@#@, -. [)&;)/0 1;0 1;0 1;0 1
|
||||
┌───┬───┬───┬───┐
|
||||
│0 1│2 3│4 5│6 7│
|
||||
└───┴───┴───┴───┘
|
||||
4
Task/Ordered-Partitions/J/ordered-partitions-5.j
Normal file
4
Task/Ordered-Partitions/J/ordered-partitions-5.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(<0 1) ([,] {L:0 (i.@#@, -. [)&;)0 1;2 3;4 5
|
||||
┌───┬───┬───┬───┐
|
||||
│0 1│2 3│4 5│6 7│
|
||||
└───┴───┴───┴───┘
|
||||
61
Task/Ordered-Partitions/JavaScript/ordered-partitions-1.js
Normal file
61
Task/Ordered-Partitions/JavaScript/ordered-partitions-1.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// [n] -> [[[n]]]
|
||||
function partitions(a1, a2, a3) {
|
||||
var n = a1 + a2 + a3;
|
||||
|
||||
return combos(range(1, n), n, [a1, a2, a3]);
|
||||
}
|
||||
|
||||
function combos(s, n, xxs) {
|
||||
if (!xxs.length) return [[]];
|
||||
|
||||
var x = xxs[0],
|
||||
xs = xxs.slice(1);
|
||||
|
||||
return mb( choose(s, n, x), function (l_rest) {
|
||||
return mb( combos(l_rest[1], (n - x), xs), function (r) {
|
||||
// monadic return/injection requires 1 additional
|
||||
// layer of list nesting:
|
||||
return [ [l_rest[0]].concat(r) ];
|
||||
|
||||
})});
|
||||
}
|
||||
|
||||
function choose(aa, n, m) {
|
||||
if (!m) return [[[], aa]];
|
||||
|
||||
var a = aa[0],
|
||||
as = aa.slice(1);
|
||||
|
||||
return n === m ? (
|
||||
[[aa, []]]
|
||||
) : (
|
||||
choose(as, n - 1, m - 1).map(function (xy) {
|
||||
return [[a].concat(xy[0]), xy[1]];
|
||||
}).concat(choose(as, n - 1, m).map(function (xy) {
|
||||
return [xy[0], [a].concat(xy[1])];
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
// GENERIC
|
||||
|
||||
// Monadic bind (chain) for lists
|
||||
function mb(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// [m..n]
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
|
||||
// EXAMPLE
|
||||
|
||||
return partitions(2, 0, 2);
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
[[[1, 2], [], [3, 4]],
|
||||
[[1, 3], [], [2, 4]],
|
||||
[[1, 4], [], [2, 3]],
|
||||
[[2, 3], [], [1, 4]],
|
||||
[[2, 4], [], [1, 3]],
|
||||
[[3, 4], [], [1, 2]]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue