1 Star 0 Fork 3

rwind_wh/lua

forked from fulinux/lua 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
permgen.lua 780 Bytes
一键复制 编辑 原始数据 按行查看 历史
fulinux 提交于 2015-09-21 16:52 +08:00 . add some lua files
#!/usr/bin/lua
function permgen (a, n)
n = n or #a
if n <= 1 then
coroutine.yield(a)
else
for i = 1, n do
-- put i-th element as the last one
a[n], a[i] = a[i], a[n]
-- generate all permutations of the other elements
permgen(a, n-1)
-- restore i-th element
a[n], a[i] = a[i], a[n]
end
end
end
function permutations (a)
local co = coroutine.create(function () permgen(a) end)
return function () -- iterator
local code, res = coroutine.resume(co)
return res
end
end
function printResult (a)
for i = 1, #a do
io.write(a[i], " ")
end
io.write("\n")
end
for p in permutations {"a", "b", "c"} do
printResult(p)
end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Lua
1
https://gitee.com/rwind/lua.git
git@gitee.com:rwind/lua.git
rwind
lua
lua
master

搜索帮助