Permutations with some identical elements: Difference between revisions

→‎{{header|Wren}}: Now uses Wren-perm module.
m (→‎{{header|Phix}}: syntax coloured, use of new builtin)
(→‎{{header|Wren}}: Now uses Wren-perm module.)
Line 1,370:
 
=={{header|Wren}}==
{{translibheader|GoWren-perm}}
<lang ecmascript>varimport shouldSwap = Fn".new { |s,/perm" start,for curr|Perm
if (start < curr) {
for (i in start...curr) {
if (s[i] == s[curr]) return false
}
}
return true
}
 
var findPerms
findPerms = Fn.new { |s, index, n, res|
if (index >= n) {
res.add(s.join())
return
}
if (index >= n) return
for (i in index...n) {
var check = shouldSwap.call(s, index, i)
if (check) {
var t = s[index]
s[index] = s[i]
s[i] = t
System.write("") // guard against VM recursion bug
findPerms.call(s, index+1, n, res)
t = s[index]
s[index] = s[i]
s[i] = t
}
}
}
 
var createList = Fn.new { |nums, charSet|
Line 1,410 ⟶ 1,381:
}
 
var res = []
var nums = [2, 1]
var sa = createList.call(nums, "12")
System.print(Perm.listDistinct(a).map { |p| p.join() }.toList)
findPerms.call(s, 0, s.count, res)
System.print(res)
System.print()
 
res.clear()
nums = [2, 3, 1]
sa = createList.call(nums, "123")
System.print(Perm.listDistinct(a).map { |p| p.join() }.toList)
findPerms.call(s, 0, s.count, res)
System.print(res)
System.print()
 
sa = createList.call(nums, "ABC")
res.clear()
System.print(Perm.listDistinct(a).map { |p| p.join() }.toList)</lang>
s = createList.call(nums, "ABC")
findPerms.call(s, 0, s.count, res)
System.print(res)</lang>
 
{{out}}
9,492

edits