DKL9 GitList
Repositories
DKL9 home
computation_practice
Code
Commits
Branches
Tags
Search
Tree:
7504023
Branches
Tags
master
computation_practice
main.lua
Enormously generalise problem-handling
John Smith
commited
7504023
at 2022-337 22:15:41
main.lua
Blame
History
Raw
#!/usr/bin/env lua local function identity(x) return x end local function cup(x) if type(x) == "table" then return table.unpack(x) else return x end end local TYPES = { ln = { -- how to generate the problem (returns problem-object) gen = function() return math.exp(6.9 * (2 * math.random() - 1)) end, -- how to present the problem (for string.format) disp = "ln(%.2f)", -- produce correct answer-object from problem-object func = math.log, -- check if guess g for problem p is correct good = function(p, g) return math.abs(math.log(p) - g) < 0.05 end, }, } -- ideally, configure these parameters with options/inputs local COUNT = 10 local TYPE = TYPES.ln local TRIES = 1 local TIME = 4 local sc = 0 local oc = 0 for i = 1,COUNT do local rp = TYPE.gen() local ans = TYPE.func(rp) io.write(string.format(TYPE.disp .. " ? ", cup(rp))) io.flush() local st = os.time() local inp local fails = 0 while true do inp = tonumber(io.read("l")) or 0 if os.time() - st > TIME then if TYPE.good(rp, inp) then oc = oc + 1 end io.write(string.format("too slow! (%.2f)\n", ans)) break elseif TYPE.good(rp, inp) then io.write("✓ correct\n") sc = sc + 1 break elseif fails < TRIES then io.write("wrong, try again ") io.flush() fails = fails + 1 else io.write(string.format("wrong (%.2f)\n", ans)) break end end end io.write(string.format("%d/%d correct, of which %d were too slow\n", (sc + oc), COUNT, oc))