Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/Permutations/Wren/permutations-1.wren
Normal file
19
Task/Permutations/Wren/permutations-1.wren
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
var permute // recursive
|
||||
permute = Fn.new { |input|
|
||||
if (input.count == 1) return [input]
|
||||
var perms = []
|
||||
var toInsert = input[0]
|
||||
for (perm in permute.call(input[1..-1])) {
|
||||
for (i in 0..perm.count) {
|
||||
var newPerm = perm.toList
|
||||
newPerm.insert(i, toInsert)
|
||||
perms.add(newPerm)
|
||||
}
|
||||
}
|
||||
return perms
|
||||
}
|
||||
|
||||
var input = [1, 2, 3]
|
||||
var perms = permute.call(input)
|
||||
System.print("There are %(perms.count) permutations of %(input), namely:\n")
|
||||
perms.each { |perm| System.print(perm) }
|
||||
27
Task/Permutations/Wren/permutations-2.wren
Normal file
27
Task/Permutations/Wren/permutations-2.wren
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import "/math" for Int
|
||||
|
||||
var input = [1, 2, 3]
|
||||
var perms = [input]
|
||||
var a = input.toList
|
||||
var n = a.count - 1
|
||||
for (c in 1...Int.factorial(n+1)) {
|
||||
var i = n - 1
|
||||
var j = n
|
||||
while (a[i] > a[i+1]) i = i - 1
|
||||
while (a[j] < a[i]) j = j - 1
|
||||
var t = a[i]
|
||||
a[i] = a[j]
|
||||
a[j] = t
|
||||
j = n
|
||||
i = i + 1
|
||||
while (i < j) {
|
||||
t = a[i]
|
||||
a[i] = a[j]
|
||||
a[j] = t
|
||||
i = i + 1
|
||||
j = j - 1
|
||||
}
|
||||
perms.add(a.toList)
|
||||
}
|
||||
System.print("There are %(perms.count) permutations of %(input), namely:\n")
|
||||
perms.each { |perm| System.print(perm) }
|
||||
6
Task/Permutations/Wren/permutations-3.wren
Normal file
6
Task/Permutations/Wren/permutations-3.wren
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import "./perm" for Perm
|
||||
|
||||
var a = [1, 2, 3]
|
||||
System.print(Perm.list(a)) // not lexicographic
|
||||
System.print()
|
||||
System.print(Perm.listLex(a)) // lexicographic
|
||||
Loading…
Add table
Add a link
Reference in a new issue