Permutations: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) |
|||
(7 intermediate revisions by 5 users not shown) | |||
Line 3,583:
<syntaxhighlight lang="easylang">
proc permlist k . list[] .
if k = len list[]▼
print list[]▼
return
.▼
for i = k to len list[]
swap list[i] list[k]
permlist k + 1 list[]
swap list[k] list[i]
▲ .
▲ if k = len list[]
▲ print list[]
.
.
Line 5,690 ⟶ 5,691:
This follows the Go language non-recursive example, but is not limited to integers, or even to numbers.
▲<syntaxhighlight lang="langur">val .factorial = f if(.x < 2: 1; .x x self(.x - 1))
▲val .permute = f(.arr) {
▲ if not isArray(.arr): throw "expected array"
val .limit = 10
if len(.
var .elements = .
var .ordinals = pseries len .elements
Line 5,707 ⟶ 5,705:
var .i, .j
for[.p=[.
.i = .n - 1
.j = .n
Line 5,731 ⟶ 5,729:
for .e in .permute([1, 3.14, 7]) {
writeln .e
}
{{out}}
Line 9,321 ⟶ 9,320:
=={{header|Sidef}}==
===Built-in===
<syntaxhighlight lang="ruby">[0,1,2].permutations { |
say
}</syntaxhighlight>
Line 9,330 ⟶ 9,329:
loop {
callback(
var p = n-1
Line 9,346 ⟶ 9,345:
}
forperm({|*p| say p }, 3)</syntaxhighlight>
===Recursive===
<syntaxhighlight lang="ruby">func permutations(callback, set, perm=[]) {
set
for i in ^set {
__FUNC__(callback, [
set[
], [perm..., set[i]])
}
Line 9,410 ⟶ 9,409:
st> 'Abc' permutations contents
('bcA' 'cbA' 'cAb' 'Acb' 'bAc' 'Abc' )
</syntaxhighlight>
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">
fun interleave x [] = [[x]]
| interleave x (y::ys) = (x::y::ys) :: (List.map (fn a => y::a) (interleave x ys))
fun perms [] = [[]]
| perms (x::xs) = List.concat (List.map (interleave x) (perms xs))
</syntaxhighlight>
Line 9,885 ⟶ 9,893:
===Recursive===
{{trans|Kotlin}}
<syntaxhighlight lang="
permute = Fn.new { |input|
if (input.count == 1) return [input]
Line 9,921 ⟶ 9,929:
{{libheader|Wren-math}}
Output modified to follow the pattern of the recursive version.
<syntaxhighlight lang="
var input = [1, 2, 3]
Line 9,963 ⟶ 9,971:
===Library based===
{{libheader|Wren-perm}}
<syntaxhighlight lang="
var a = [1, 2, 3]
|