Language detail: Lua
Coverage: 18.78%
|
number of '+' ratings |
contribution for coverage |
Unsolved challenges
- 文字列で+を表示する (Nested Flatten)
- 年賀はがきの当せん番号 (Nested Flatten)
- 箱詰めパズルの判定 (Nested Flatten)
- 関数やメソッドのソースの平均行数 (Nested Flatten)
- コレクションの実装 (Nested Flatten)
codes
九九の表示
(Nested
Flatten)
1 2 3 4 5 | for a = 1, 9 do
for b = 1, 9 do
print(("%d * %d = %2d"):format(a, b, a * b))
end
end
|
変形Fizz-Buzz問題
(Nested
Flatten)
1 2 3 4 | t = {"hoge", "Fizz", "Buzz", "FizzBuzz"}
for i = 1, 20 do
print(("%2d:%s"):format(i, t[2 - i * i % 3 + 2 * 0 ^ (i % 5)]))
end
|
疑似並行処理
(Nested
Flatten)
初チャレンジ。 Lua のコルーチンで for ループを素朴に使っています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function mkfunc (printfunc)
return function ()
for v = 1, 10 do
printfunc (v)
coroutine.yield ()
end
end
end
function printalpha (n)
print (string.char (n + string.byte ("A") - 1))
end
function costat (c)
return coroutine.status (c) == "suspended"
end
local c, d = coroutine.create (mkfunc (print)),
coroutine.create (mkfunc (printalpha))
while costat (c) or costat (d) do
coroutine.resume (c)
coroutine.resume (d)
end
|
LL Golf Hole 8 - 横向きのピラミッドを作る
(Nested
Flatten)
stdin からで 59B。
1 | n=io.read()for i=-n,n do print(('*'):rep(n-math.abs(i)))end
|
Luaでワンライナーです。(不要な半角空白を除けば76バイト。)
1 | (function (n) for i = 1 - n, n - 1 do print(string.rep('*', n - math.abs(i))) end end)(4)
|
年間カレンダー
(Nested
Flatten)
Lua始めました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | -- 閏年かどうか判定
L =
function (y)
if y % 400 == 0 then return 1 end
if y % 100 == 0 then return 0 end
if y % 4 == 0 then return 1 end
return 0
end
-- 指定された日の曜日を取得
W =
function (y, m, d)
return tonumber(os.date('%w', os.time({ year = y, month = m, day = d })))
end
(function (v)
local d = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
local w = { 'Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa' }
local m, t, y
if #v ~= 2 then
io.stderr:write('usage: ' .. v[0] .. ' [month] [year]\n')
return 1
end
m, y = tonumber(v[1]), tonumber(v[2])
if m == nil or y == nil then
io.stderr:write('usage: ' .. v[0] .. ' [month] [year]\n')
return 1
end
if m < 1 or m > 12 then
io.stderr:write('cal: illegal month value: use 1-12\n')
return 1
end
if y < 1 or y > 9999 then
io.stderr:write('cal: illegal year value: use 1-9999\n')
return 1
end
-- 閏日を補正
d[2] = d[2] + L(y)
t = string.format('%4d/%2d', y, m) .. '\n'
.. table.concat(w, ' ') .. '\n'
.. string.rep(' ', W(y, m, 1))
for i = 1, d[m] do
t = t .. string.format('%2d', i)
t = W(y, m, i) < 6 and t .. ' ' or t .. '\n'
end
print(t)
return 0
end)(arg)
|
LL Golf Hole 2 - 文字列に含まれる単語の最初の文字を大文字にする
(Nested
Flatten)
stdin → stdout
1 | r=io.read():gsub('(%w)(%w+)',function(h,t)return h:upper()..t end)print(r)
|
文字変換表に基く文字列の変換
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 | function tr(a, b, s)
r = ''
for i = 1, s:len() do
j = a:find(s:sub(i, i))
r = r .. (j and b:sub(j, j) or s:sub(i, i))
end
return r
end
print(tr('qwertyuiop', 'QWERTYUIOP', 'typewriter'))
|
不動点演算子
(Nested
Flatten)
以下のサイトが分かりやすかったです。 結果的に#5733さんのコードそのままになりました。
see: Y Combinator
1 2 3 4 5 6 | Y = function(f)
g = function(proc) return f(function(arg) return proc(proc)(arg) end) end
return g(g)
end
print(Y(function(f) return function(n) if n < 2 then return 1 else return n * f(n - 1) end end end)(10))
|
データの整列
(Nested
Flatten)
昨日から使い始めたLuaで投稿
1 2 3 4 5 6 7 8 9 10 11 | printt = function(t)
for k, v in pairs(t) do print(string.format('{%d, %d}', v[1], v[2])) end
print()
end
a = {{1,2}, {3,4}, {1,3}, {2,4}, {1,8}}
printt(a)
table.sort(a, function(b, c) return b[1] < c[1] or (b[1] == c[1] and b[2] < c[2]) end)
printt(a)
table.sort(a, function(b, c) return b[1]^2 + b[2]^2 < c[1]^2 + c[2]^2 end)
printt(a)
|
コマンドライン引数の取得
(Nested
Flatten)
-コマンドライン引数はargという名前のグローバルなテーブルに格納される。 -スクリプト名がインデックス0に格納される。 -最初の引数がスクリプト名の後のインデックス1に格納され、以下同様。 -フィールドnはスクリプト名の後の引数の数を持つ。 -スクリプト名の前のすべての引数(インタプリタ名やオプション)は負のインデックスに割り当てられる。 C:\>lua para.lua a b c d [-1] = lua [0] = para.lua [1] = a [2] = b [3] = c [4] = d C:\>
see: Lua: 5.0 リファレンスマニュアル/6 - スタンドアロンのLua
1 2 3 | for i = -1, arg.n do
print("[" .. i .. "] = " .. arg[i])
end
|
自然数の分割
(Nested
Flatten)
Lua です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function split(n, m)
if m == 0 then
if n == 0 then coroutine.yield{} end
else
for i=n,0,-1 do
for t in coroutine.wrap(function () split(n-i, m-1) end) do
table.insert(t, 1, i) -- prepend
coroutine.yield(t)
end
end
end
end
function split_iter(n, m)
return coroutine.wrap(function () split(n, m) end)
end
for i in split_iter(5, 3) do
print(table.concat(i, ", "))
end
|
与えられた文字列でピラミッド
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 | function pyramid(str)
len = string.len(str)
for i=len, 1, -1 do
io.stdout:write(string.rep(" ",i-1))
for j=i, len, 1 do io.stdout:write(string.sub(str,j,j), " ") end
io.stdout:write("\n")
end
end
pyramid("abracadabra")
|
隣り合う二項の差
(Nested
Flatten)
なまくらlua版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function print_array(ary)
s = "[ "
table.foreach(ary, function(k,v) s = s .. string.format("%s ",v) end)
s = s.."]"
print(s)
end
function diff(ary)
ret = {}
for i = 1, table.getn(ary)-1, 1 do
ret[i] = ary[i+1]-ary[i]
end
return ret
end
print_array(diff({3, 1, 4, 1, 5, 9, 2, 6, 5}))
|
複数行のコメントアウト
(Nested
Flatten)
Lua5.1 では -- が1行コメントで --[[ から --]] までがブロックコメントになります。この性質を利用すると --[[ を ---[[ と書き換えるだけでコメントアウトを解除できて便利です。ブロックコメントをネストするには --[=[ ... --]=] のように任意の数の等号を挟みます。この例では B だけが表示されます(シンタクスハイライトは残念ながらブロックコメントに対応していないようですが…)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | --[[
print("A")
--]]
---[[
print("B")
--]]
--[===[
--[==[
--[=[
--[[
print("C")
--]]
--]=]
--]==]
--]===]
|
呼んだのは誰?
(Nested
Flatten)
ちゃんとテーブルの形で情報もらえるので文字列から抽出する必要はないかと
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function foo() return debug.getinfo(2).name end
function bar()
local ret = foo()
return ret
end
function baz()
local ret = foo()
return ret
end
print(bar())
print(baz())
|
ローカル変数の一覧を取得
(Nested
Flatten)
next >>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | function locals()
local i = 1
local t = {}
while true do
local k, v = debug.getlocal(2, i)
if k == nil then break end
t[k] = v
i = i + 1
end
return t
end
function foo()
local x = 1
local y = "hello"
return locals(), nil
end
for k, v in pairs(foo()) do print(k, v) end
--> y hello
--> x 1
|






匿名
#9637()
[
Lua
]
Rating0/0=0.00
Rating0/0=0.00-0+
[ reply ]