How to resolve the algorithm Permutations by swapping step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Permutations by swapping step by step in the Wren programming language
Table of Contents
Problem Statement
Generate permutations of n items in which successive permutations differ from each other by the swapping of any two items. Also generate the sign of the permutation which is +1 when the permutation is generated from an even number of swaps from the initial state, and -1 for odd. Show the permutations and signs of three items, in order of generation here. Such data are of use in generating the determinant of a square matrix and any functions created should bear this in mind. Note: The Steinhaus–Johnson–Trotter algorithm generates successive permutations where adjacent items are swapped, but from this discussion adjacency is not a requirement.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Permutations by swapping step by step in the Wren programming language
Source code in the wren programming language
var johnsonTrotter = Fn.new { |n|
var p = List.filled(n, 0) // permutation
var q = List.filled(n, 0) // inverse permutation
for (i in 0...n) p[i] = q[i] = i
var d = List.filled(n, -1) // direction = 1 or -1
var sign = 1
var perms = []
var signs = []
var permute // recursive closure
permute = Fn.new { |k|
if (k >= n) {
perms.add(p.toList)
signs.add(sign)
sign = sign * -1
return
}
permute.call(k + 1)
for (i in 0...k) {
var z = p[q[k] + d[k]]
p[q[k]] = z
p[q[k] + d[k]] = k
q[z] = q[k]
q[k] = q[k] + d[k]
permute.call(k + 1)
}
d[k] = d[k] * -1
}
permute.call(0)
return [perms, signs]
}
var printPermsAndSigns = Fn.new { |perms, signs|
var i = 0
for (perm in perms) {
System.print("%(perm) -> sign = %(signs[i])")
i = i + 1
}
}
var res = johnsonTrotter.call(3)
var perms = res[0]
var signs = res[1]
printPermsAndSigns.call(perms, signs)
System.print()
res = johnsonTrotter.call(4)
perms = res[0]
signs = res[1]
printPermsAndSigns.call(perms, signs)
You may also check:How to resolve the algorithm Create an object at a given address step by step in the J programming language
You may also check:How to resolve the algorithm Video display modes step by step in the Lua programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Julia programming language
You may also check:How to resolve the algorithm Quine step by step in the Gema programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Racket programming language