Language detail: Lua

Coverage: 23.66%
number of '+' ratings
contribution for coverage

Unsolved challenges

codes

Feed

Used modules

next >>

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)
Hello, world!その2 (Nested Flatten)
1
print "Hel\108o, w\111\114\108d!"
文字変換表に基く文字列の変換 (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さんのコードそのままになりました。
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)
1
print(arg[0])
データの整列 (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:\>
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)
 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
ダブル完全数 (Nested Flatten)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
local function sum_divisors(n)
  local r = 0
  for i = 1,math.sqrt(n) do
    if n % i == 0 then
      if i*i == n then
        r = r + i
      else
        r = r + i + n/i
      end
    end
  end
  return r
end

for i = 1,10000 do
  if sum_divisors(i) == 3*i then
    print(i)
  end
end
分数を小数に展開 (Nested Flatten)
 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
function decimal(a,b)
  local r,t,i,z,p = {},{},1,1
  while a >= b do
    b,z = b*10,z+1
  end
  while a ~= 0 do
    t[a],i,a = i,i+1,a*10
    table.insert(r,math.floor(a/b))
    a = a%b
    if t[a] then
      p = t[a]
      break
    end
  end
  if p then
    r[p] = "{"..r[p]
    table.insert(r,"}")
  end
  table.insert(r,z,".")
  if z == 1 then
    table.insert(r,1,"0")
  end
  return table.concat(r)
end
assert(decimal(3,8) == "0.375")
assert(decimal(3,14) == "0.2{142857}")
n日後を返す関数を返す関数 (Nested Flatten)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function n_days_later(n)
  return function (time)
           return time + n*3600*24
         end
end

tm = os.time()
print(os.date("%c", tm))
print(n_days_later(3))
print(os.date("%c", n_days_later(3)(tm)))
呼んだのは誰? (Nested Flatten)
tail call optimizationをやるのでわざとstring.formatを呼んでみる。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
function foo()
  return string.gsub(debug.traceback(), "^.+\n.+\n.+`(.+)'.+$", "%1")
end
function bar()
  return string.format("%s",foo())
end
function baz()
  return string.format("%s",foo())
end

print(bar())
print(baz())
リストを逆順に表示 (Nested Flatten)
1
2
3
4
5
6
7
function print_reverse(given_list)
  for i = table.getn(given_list), 1, -1 do
    print(given_list[i])
  end
end

print_reverse({1,2,3,4,5})
ダブル完全数 (Nested Flatten)
 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
function divisors(n)
  ret = {}
  for i = 1, n, 1 do
    if math.mod(n, i) == 0 then
      table.insert(ret, i)
    end
  end
  return ret
end

function sum(ary)
  s = 0
  foreachi(ary, function(i, x) s = s+x end)
  return s
end

function double_complete_number_p(n)
  return n*3 == sum(divisors(n))
end

for i = 1, 10000, 1 do
  if double_complete_number_p(i) then
    print(i)
  end
end
HTTPでGET (Nested Flatten)
LuaSocket <http://luaforge.net/projects/luasocket/> を使用。
1
print(socket.http.request("http://ja.doukaku.org/feeds/comments/"))
next >>

Index

Feed

Other

Link

Pathtraq

loading...