Skip to content

Commit

Permalink
1st commit on 042_lua
Browse files Browse the repository at this point in the history
  • Loading branch information
stuart-little committed Jul 16, 2021
1 parent 0908914 commit 36822c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions challenge-042/stuart-little/lua/ch-1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env lua

-- run <script> <largest decimal number to convert; defaults to 50>

for n=0,(#arg>0 and tonumber(arg[1]) or 50) do
print(("Decimal: %d Octal: %o"):format(n,n))
end
26 changes: 26 additions & 0 deletions challenge-042/stuart-little/lua/ch-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env lua

-- run <script> <string or nothing to default to a random one>

function bal(s)
local stk={}
for i=1,s:len() do
if #stk>0 and stk[#stk]=='(' and s:sub(i,i)==')'
then table.remove(stk)
else table.insert(stk,s:sub(i,i))
end
end
return (#stk==0) and 'OK' or 'NOT OK'
end

function genRndPars(n)
local s=""
for i=1,math.random(n) do
s=s..math.random(0,1)
end
return s:gsub(".",{['0']='(', ['1']=')'})
end

local s=#arg>0 and arg[1] or genRndPars(10)
print(("Your string: %s"):format(s))
print(bal(s))

0 comments on commit 36822c6

Please sign in to comment.