Skip to content

Instantly share code, notes, and snippets.

@truemedian
Created April 11, 2023 23:36
Show Gist options
  • Save truemedian/5594a9528c51f188242d32e3d212cffe to your computer and use it in GitHub Desktop.
Save truemedian/5594a9528c51f188242d32e3d212cffe to your computer and use it in GitHub Desktop.
local ffi = require 'ffi'
local function permute_inner(arr, l, r)
if l == r then
coroutine.yield(ffi.string(arr))
else
for i = l, r do
arr[l], arr[i] = arr[i], arr[l]
permute_inner(arr, l + 1, r)
arr[l], arr[i] = arr[i], arr[l]
end
end
end
local function permute(str)
local arr = ffi.new('char[?]', #str + 1, str)
return coroutine.wrap(function() permute_inner(arr, 0, #str - 1) end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment