[lua] add old snippets
This commit is contained in:
parent
8f06213af4
commit
467359fda3
|
@ -0,0 +1,15 @@
|
|||
#+TITLE: Lua
|
||||
Most code snippets are from 2018..
|
||||
|
||||
- [[./calc.lua]]: Basic calculator
|
||||
- [[./gambler_ruin.lua]]: Gambler's ruin problem
|
||||
- [[./quadraticEqn.lua]]: Solving a quadratic equation
|
||||
- [[./co-routine.lua]]: Coroutines
|
||||
- [[./lua]]-dice.lua: Random number generation
|
||||
- [[./producer_consumer.lua]]: Producer consumer problem
|
||||
- [[./tables.lua]]: Using tables
|
||||
- [[./rev_table.lua]]: Reverse a table
|
||||
|
||||
#- [[./tail_call_debate.lua]]
|
||||
#- [[./sinlua]].lua
|
||||
#- [[./derivative.lua]]
|
|
@ -0,0 +1,31 @@
|
|||
io.write("Enter the operator: ")
|
||||
op = io.read()
|
||||
if op~='+' and op~='-' and op~='*' and op~='/' and op~='%' then
|
||||
io.write("Invalid operation!")
|
||||
else
|
||||
io.write("Enter 2 nos: \n")
|
||||
a = io.read()
|
||||
if tonumber(a)==nil then
|
||||
io.write("Invalid operand!")
|
||||
return
|
||||
end
|
||||
b = io.read()
|
||||
if tonumber(b)==nil then
|
||||
io.write("Invalid operand!")
|
||||
return
|
||||
end
|
||||
if op=='+' then
|
||||
res = a+b
|
||||
elseif op=='-' then
|
||||
res = a-b
|
||||
elseif op=='*' then
|
||||
res = a*b
|
||||
elseif op=='/' then
|
||||
if b==0 then
|
||||
io.write("Divide by zero error!")
|
||||
return
|
||||
end
|
||||
res = a/b
|
||||
end
|
||||
io.write("Result: " .. res .. "\n")
|
||||
end
|
|
@ -0,0 +1,12 @@
|
|||
co = coroutine.create( function ()
|
||||
for i=4, 7 do
|
||||
coroutine.yield(i, i^2)
|
||||
end
|
||||
end)
|
||||
|
||||
for i=4,7 do
|
||||
print(coroutine.resume(co))
|
||||
--io.write(coroutine.resume(co))
|
||||
end
|
||||
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
function deriv(fn, change)
|
||||
--change = change or 1e-3
|
||||
change = change or 0.001
|
||||
return
|
||||
function (val)
|
||||
return (fn(val + change) - fn(val))/change
|
||||
end
|
||||
end
|
||||
|
||||
d = deriv(math.sin) -- which should be approximately math.cos
|
||||
io.write("d: "..d(10)..", math: "..math.cos(10))
|
||||
|
||||
-- Output is:
|
||||
-- d: -0.83879937869802, math: -0.83907152907645
|
||||
-- approximately the same. You can try the other way around as well if you like.
|
|
@ -0,0 +1,42 @@
|
|||
function player_fn_builder_fn (player)
|
||||
return function ()
|
||||
local a, b, c
|
||||
io.write("Player ", player, "'s turn:\n")
|
||||
a = math.random(1,6)
|
||||
b = math.random(1,6)
|
||||
c = math.random(1,6)
|
||||
io.write("Die vals: ", a, ", ", b, ", ", c, "\tTotal: ", a+b+c, "\n")
|
||||
return a+b+c
|
||||
end
|
||||
end
|
||||
|
||||
score = {a = 12, b = 12}
|
||||
no_of_iterations = 0
|
||||
|
||||
afn = player_fn_builder_fn("John")
|
||||
bfn = player_fn_builder_fn("Jack")
|
||||
|
||||
while score.a>0 and score.b>0 do
|
||||
local a = afn()
|
||||
local b = bfn()
|
||||
if a==11 and b~=14 then --only if the other hasn't scored
|
||||
score.a = score.a + 1
|
||||
score.b = score.b - 1
|
||||
io.write("A scored! Scores: A: ", score.a, " B: ", score.b, "\n")
|
||||
elseif a~=11 and b==14 then
|
||||
score.b = score.b + 1
|
||||
score.a = score.a - 1
|
||||
io.write("B scored! Scores: A: ", score.a, " B: ", score.b, "\n")
|
||||
end
|
||||
io.write("\n")
|
||||
no_of_iterations = no_of_iterations + 1
|
||||
--io.stdin:read'*l'
|
||||
end
|
||||
|
||||
if a==0 then
|
||||
io.write("B won. A ruined.\n");
|
||||
else
|
||||
io.write("A won. B ruined.\n");
|
||||
end
|
||||
|
||||
io.write("No. of iterations: ", no_of_iterations, "\n")
|
|
@ -0,0 +1,21 @@
|
|||
require "math"
|
||||
|
||||
if package.loaded.math then
|
||||
print("math module loaded.")
|
||||
else
|
||||
print("math module not loaded.")
|
||||
end
|
||||
|
||||
math.randomseed(00)
|
||||
|
||||
--generate a random number between in the interval [1,6] and
|
||||
--continue looping till you get a 6
|
||||
local counter = 0
|
||||
|
||||
repeat
|
||||
local n = math.random(6)
|
||||
print("N: " .. tostring(n))
|
||||
counter = counter + 1
|
||||
until n==6
|
||||
|
||||
print("Counter: " .. counter)
|
|
@ -0,0 +1,62 @@
|
|||
function send (n)
|
||||
coroutine.yield(n)
|
||||
end
|
||||
|
||||
function receive ()
|
||||
local status, n = coroutine.resume(producer)
|
||||
return value
|
||||
end
|
||||
|
||||
producer = coroutine.create(
|
||||
function ()
|
||||
local n = io.read()
|
||||
send(n)
|
||||
end
|
||||
)
|
||||
|
||||
|
||||
consumer = coroutine.create(
|
||||
function ()
|
||||
local n = receive()
|
||||
end
|
||||
)
|
||||
|
||||
|
||||
--[[
|
||||
producer = coroutine.create(
|
||||
function ()
|
||||
io.write("Enter sth: ")
|
||||
local n = io.read()
|
||||
io.write("Yielding producer. Over to consumer...\n")
|
||||
coroutine.yield(n)
|
||||
end
|
||||
)
|
||||
|
||||
consumer = coroutine.create(
|
||||
function (n)
|
||||
local n = coroutine.resume(producer)
|
||||
io.write("Consumer got '", n, "' from producer.\n")
|
||||
io.write("Resuming producer...\n")
|
||||
coroutine.resume(producer)
|
||||
end
|
||||
)
|
||||
|
||||
function fn ()
|
||||
local n = coroutine.resume(producer)
|
||||
coroutine.consumer( )
|
||||
end
|
||||
|
||||
coroutine.resume(producer)
|
||||
]]--
|
||||
|
||||
|
||||
|
||||
--[[
|
||||
producer = coroutine.create(
|
||||
function ()
|
||||
while true do
|
||||
local x = io.read() --produce new value
|
||||
send(x)
|
||||
end
|
||||
end)
|
||||
]]--
|
|
@ -0,0 +1,32 @@
|
|||
function readNumber ()
|
||||
local a = io.read()
|
||||
if tonumber(a) == nil then
|
||||
os.exit()
|
||||
else
|
||||
return a
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
io.write("Enter a, b and c: ")
|
||||
local a = readNumber()
|
||||
local b = readNumber()
|
||||
local c = readNumber()
|
||||
io.write("a: " .. a .. ", b: " .. b .. ", c: " .. c .. "\n")
|
||||
d = b^2 - 4*a*c
|
||||
io.write("Discriminant: " .. d)
|
||||
if d>0 then
|
||||
--5,6,1 => -0.2 and -1
|
||||
print('Real distinct roots')
|
||||
local r1 = (-b + math.sqrt(d))/(2*a)
|
||||
local r2 = (-b - math.sqrt(d))/(2*a)
|
||||
print("Roots are " .. r1 .. " and " .. r2 .. "\n")
|
||||
elseif d<0 then
|
||||
--5,2,1 => -0.2 +/- 0.4i
|
||||
print('Complex roots.')
|
||||
else
|
||||
print('Real equal roots.')
|
||||
local r1 = (-b + math.sqrt(d))/(2*a)
|
||||
print("Roots are " .. r1 .. " and " .. r1 .. "\n")
|
||||
end
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
days = {"sun",
|
||||
"mon",
|
||||
"tue",
|
||||
"wed",
|
||||
"thu",
|
||||
"fri",
|
||||
"sat"}
|
||||
|
||||
rev_days = {}
|
||||
for index, value in ipairs(days) do
|
||||
rev_days[value] = index
|
||||
end
|
||||
|
||||
input = io.read()
|
||||
print( rev_days[input] )
|
|
@ -0,0 +1,8 @@
|
|||
angle = io.read()
|
||||
angle = math.rad(angle)
|
||||
|
||||
print( math.sin(angle)/math.cos(angle) )
|
||||
print( 1/math.sin(angle) )
|
||||
print( 1/math.cos(angle) )
|
||||
|
||||
print( 1/math.tan(angle) )
|
|
@ -0,0 +1,22 @@
|
|||
-- Check if a table has another table in it
|
||||
function is_nested_table(t)
|
||||
for _, value in pairs(t) do
|
||||
if type(value) == 'table' then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
-- Write and read to a table
|
||||
t = {}
|
||||
n = io.read()
|
||||
|
||||
for i=1,n do
|
||||
t[i] = io.read()
|
||||
end
|
||||
|
||||
for i=1,n do
|
||||
print(t[i])
|
||||
end
|
|
@ -0,0 +1,73 @@
|
|||
function a ()
|
||||
io.write("a has spoken.\n")
|
||||
local who_next = io.read()
|
||||
if who_next=='p' then
|
||||
return p()
|
||||
elseif who_next=='q' then
|
||||
return q()
|
||||
elseif who_next=='a' or who_next=='b' then
|
||||
io.write("Team 1 already spoke!\n")
|
||||
a()
|
||||
elseif who_next=='timeup' then
|
||||
io.write("Debate is over. Thank you.\n")
|
||||
else
|
||||
io.write("No such speaker!\n")
|
||||
a()
|
||||
end
|
||||
end
|
||||
|
||||
function b ()
|
||||
io.write("b has spoken.\n")
|
||||
local who_next = io.read()
|
||||
if who_next=='p' then
|
||||
return p()
|
||||
elseif who_next=='q' then
|
||||
return q()
|
||||
elseif who_next=='a' or who_next=='b' then
|
||||
io.write("Team 1 already spoke!\n")
|
||||
b()
|
||||
elseif who_next=='timeup' then
|
||||
io.write("Debate is over.\n")
|
||||
else
|
||||
io.write("No such speaker!\n")
|
||||
b()
|
||||
end
|
||||
end
|
||||
|
||||
function p ()
|
||||
io.write("p has spoken.\n")
|
||||
local who_next = io.read()
|
||||
if who_next=='a' then
|
||||
return a()
|
||||
elseif who_next=='b' then
|
||||
return b()
|
||||
elseif who_next=='p' or who_next=='q' then
|
||||
io.write("Team 2 already spoke!\n")
|
||||
p()
|
||||
elseif who_next=='timeup' then
|
||||
io.write("Debate is over. Thank you\n")
|
||||
else
|
||||
io.write("No such speaker!\n")
|
||||
p()
|
||||
end
|
||||
end
|
||||
|
||||
function q ()
|
||||
io.write("q has spoken.\n")
|
||||
local who_next = io.read()
|
||||
if who_next=='a' then
|
||||
return a()
|
||||
elseif who_next=='b' then
|
||||
return b()
|
||||
elseif who_next=='p' or who_next=='q' then
|
||||
io.write("Team 2 already spoke!\n")
|
||||
q()
|
||||
elseif who_next=='timeup' then
|
||||
io.write("Debate is over.\n")
|
||||
else
|
||||
io.write("No such speaker!\n")
|
||||
q()
|
||||
end
|
||||
end
|
||||
|
||||
a()
|
Loading…
Reference in New Issue