Permutations: Difference between revisions

Content added Content deleted
m (→‎{{header|Sidef}}: updated code)
Line 9,322: Line 9,322:
=={{header|Sidef}}==
=={{header|Sidef}}==
===Built-in===
===Built-in===
<syntaxhighlight lang="ruby">[0,1,2].permutations { |p|
<syntaxhighlight lang="ruby">[0,1,2].permutations { |*a|
say p
say a
}</syntaxhighlight>
}</syntaxhighlight>


Line 9,331: Line 9,331:


loop {
loop {
callback([idx...])
callback(idx...)


var p = n-1
var p = n-1
Line 9,347: Line 9,347:
}
}


forperm({|p| say p }, 3)</syntaxhighlight>
forperm({|*p| say p }, 3)</syntaxhighlight>


===Recursive===
===Recursive===
<syntaxhighlight lang="ruby">func permutations(callback, set, perm=[]) {
<syntaxhighlight lang="ruby">func permutations(callback, set, perm=[]) {
set.is_empty && callback(perm)
set || callback(perm)
for i in ^set {
for i in ^set {
__FUNC__(callback, [
__FUNC__(callback, [
set[(0 ..^ i)..., (i+1 ..^ set.len)...]
set[^i, i+1 ..^ set.len]
], [perm..., set[i]])
], [perm..., set[i]])
}
}