Ordered partitions: Difference between revisions

Content added Content deleted
m (Thundergnat moved page Ordered Partitions to Ordered partitions: Follow normal task title capitalization policy)
(Added Wren)
Line 2,325: Line 2,325:
<<2,4>,<>,<1,3>>,
<<2,4>,<>,<1,3>>,
<<3,4>,<>,<1,2>>></pre>
<<3,4>,<>,<1,2>>></pre>

=={{header|Wren}}==
{{trans|Go}}
<lang ecmascript>import "os" for Process

var genPart // recursive so predeclare
genPart = Fn.new { |n, res, pos|
if (pos == res.count) {
var x = List.filled(n.count, null)
for (i in 0...x.count) x[i] = []
var i = 0
for (c in res) {
x[c].add(i+1)
i = i + 1
}
System.print(x)
return
}
for (i in 0...n.count) {
if (n[i] != 0) {
n[i] = n[i] - 1
res[pos] = i
genPart.call(n, res, pos+1)
n[i] = n[i] + 1
}
}
}

var orderedPart = Fn.new { |nParts|
System.print("Ordered %(nParts)")
var sum = 0
for (c in nParts) sum = sum + c
genPart.call(nParts, List.filled(sum, 0), 0)
}

var args = Process.arguments
if (args.count == 0) {
orderedPart.call([2, 0, 2])
return
}
var n = List.filled(args.count, 0)
var i = 0
for (a in args) {
n[i] = Num.fromString(a)
if (n[i] < 0) {
System.print("negative partition size not meaningful")
return
}
i = i + 1
}
orderedPart.call(n)</lang>

{{out}}
<pre>
$ wren_cli ordered_partitions.wren
Ordered [2, 0, 2]
[[1, 2], [], [3, 4]]
[[1, 3], [], [2, 4]]
[[1, 4], [], [2, 3]]
[[2, 3], [], [1, 4]]
[[2, 4], [], [1, 3]]
[[3, 4], [], [1, 2]]

$ wren_cli ordered_partitions.wren 1 1 1
Ordered [1, 1, 1]
[[1], [2], [3]]
[[1], [3], [2]]
[[2], [1], [3]]
[[3], [1], [2]]
[[2], [3], [1]]
[[3], [2], [1]]

$ wren_cli ordered_partitions.wren 1 2 3 4 | head
Ordered [1, 2, 3, 4]
[[1], [2, 3], [4, 5, 6], [7, 8, 9, 10]]
[[1], [2, 3], [4, 5, 7], [6, 8, 9, 10]]
[[1], [2, 3], [4, 5, 8], [6, 7, 9, 10]]
[[1], [2, 3], [4, 5, 9], [6, 7, 8, 10]]
[[1], [2, 3], [4, 5, 10], [6, 7, 8, 9]]
[[1], [2, 3], [4, 6, 7], [5, 8, 9, 10]]
[[1], [2, 3], [4, 6, 8], [5, 7, 9, 10]]
[[1], [2, 3], [4, 6, 9], [5, 7, 8, 10]]
[[1], [2, 3], [4, 6, 10], [5, 7, 8, 9]]
</pre>


=={{header|zkl}}==
=={{header|zkl}}==