function perm(ar, l = 0, r = ar.length - 1, opts = []) {
if (l === r) {
opts.push(ar);
}
for (let i = l; i <= r; i++) {
swap(ar, i, l)
perm(ar.slice(0), l + 1, r, opts)
swap(ar, l, i)
}
return opts;
}
function swap(arr, i, j) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
def perm(ar, l = 0, r = ar.length - 1, opts = [])
if l == r
opts.push(ar.dup)
end
(l..r).each do |i|
ar[l], ar[i] = ar[i], ar[l]
perm(ar, l + 1, r, opts)
ar[i], ar[l] = ar[l], ar[i]
end
opts
end