Permutations: Difference between revisions

(Applesoft, BASIC heading)
 
(11 intermediate revisions by 6 users not shown)
Line 2,092:
1342 3142 4132 1432 3412 4312 4321 3421 2431 4231 3241 2341</pre>
 
==={{header|FutureBasic}}===
 
==== With recursion ====
Here's a sweet and short solution adapted from Robert Sedgewick's 'Algorithms' (1989, p. 628). It generates its own array of integers.
 
<syntaxhighlight lang="futurebasic">
void local fn perm( k as Short)
static Short w( 4 ), i = -1
Short j
i ++ : w( k ) = i
if i = 4
for j = 1 to 4 : print w( j ),
next : print
else
for j = 1 to 4 : if w( j ) = 0 then fn perm( j )
next
end if
i -- : w( k ) = 0
end fn
 
fn perm(0)
 
handleevents
 
</syntaxhighlight>
 
==== With iteration ====
We can also do it by brute force:
<syntaxhighlight lang="futurebasic">
void local fn perm( w as CFStringRef )
Short a, b, c, d
for a = 0 to 3 : for b = 0 to 3 : for c = 0 to 3 : for d = 0 to 3
if a != b and a != c and a != d and b != c and b != d and c != d
print mid(w,a,1); mid(w,b,1); mid(w,c,1); mid(w,d,1)
end if
next : next : next : next
end fn
 
fn perm (@"abel")
 
handleevents
</syntaxhighlight>
 
==={{header|IS-BASIC}}===
Line 3,624 ⟶ 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]
call permlist k + 1 list[]
swap list[k] list[i]
.
if k = len list[]
print list[]
.
.
l[] = [ 1 2 3 ]
call permlist 1 l[]
</syntaxhighlight>
 
Line 4,687 ⟶ 4,647:
4 3 2 1
</pre>
 
=={{header|FutureBasic}}==
 
=== With recursion ===
Here's a sweet and short solution adapted from Robert Sedgewick's 'Algorithms' (1989, p. 628). It generates its own array of integers.
 
<syntaxhighlight lang="futurebasic">
void local fn perm( k as Short)
static Short w( 4 ), i = -1
Short j
i ++ : w( k ) = i
if i = 4
for j = 1 to 4 : print w( j ),
next : print
else
for j = 1 to 4 : if w( j ) = 0 then fn perm( j )
next
end if
i -- : w( k ) = 0
end fn
 
fn perm(0)
 
handleevents
 
</syntaxhighlight>
 
=== With iteration ===
We can also do it by brute force:
<syntaxhighlight lang="futurebasic">
void local fn perm( w as CFStringRef )
Short a, b, c, d
for a = 0 to 3 : for b = 0 to 3 : for c = 0 to 3 : for d = 0 to 3
if a != b and a != c and a != d and b != c and b != d and c != d
print mid(w,a,1); mid(w,b,1); mid(w,c,1); mid(w,d,1)
end if
next : next : next : next
end fn
 
fn perm (@"abel")
 
handleevents
</syntaxhighlight>
 
=={{header|GAP}}==
Line 5,688 ⟶ 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 = fn .x: if(.x < 2: 1; .x * self(.x - 1))
{{works with|langur|0.10}}
Prior to 0.10, multi-variable declaration/assignment would use parentheses around variable names and values. 0.10 also parses the increment section of a for loop as a multi-variable assignment, not as a list of assignments.
 
val .permute = fn(.list) {
<syntaxhighlight lang="langur">val .factorial = f if(.x < 2: 1; .x x self(.x - 1))
if .list is not list: throw "expected list"
 
val .permute = f(.arr) {
if not isArray(.arr): throw "expected array"
 
val .limit = 10
if len(.arrlist) > .limit: throw $"permutation limit exceeded (currently \{{.limit;}})"
 
var .elements = .arrlist
var .ordinals = pseries len .elements
 
Line 5,705:
var .i, .j
 
for[.p=[.arrlist]] of .factorial(len .arrlist)-1 {
.i = .n - 1
.j = .n
Line 5,729:
for .e in .permute([1, 3.14, 7]) {
writeln .e
}
}</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
Line 9,319 ⟶ 9,320:
=={{header|Sidef}}==
===Built-in===
<syntaxhighlight lang="ruby">[0,1,2].permutations { |p*a|
say pa
}</syntaxhighlight>
 
Line 9,328 ⟶ 9,329:
 
loop {
callback([idx...])
 
var p = n-1
Line 9,344 ⟶ 9,345:
}
 
forperm({|*p| say p }, 3)</syntaxhighlight>
 
===Recursive===
<syntaxhighlight lang="ruby">func permutations(callback, set, perm=[]) {
set.is_empty &&|| callback(perm)
for i in ^set {
__FUNC__(callback, [
set[(0 ..^ i)..., (i+1 ..^ set.len)...]
], [perm..., set[i]])
}
Line 9,408 ⟶ 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,883 ⟶ 9,893:
===Recursive===
{{trans|Kotlin}}
<syntaxhighlight lang="ecmascriptwren">var permute // recursive
permute = Fn.new { |input|
if (input.count == 1) return [input]
Line 9,919 ⟶ 9,929:
{{libheader|Wren-math}}
Output modified to follow the pattern of the recursive version.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
 
var input = [1, 2, 3]
Line 9,961 ⟶ 9,971:
===Library based===
{{libheader|Wren-perm}}
<syntaxhighlight lang="ecmascriptwren">import "./perm" for Perm
 
var a = [1, 2, 3]
890

edits