John Smith commited on 2022-327 06:04:58
Showing 1 changed files, with 61 additions and 0 deletions.
Develop working prototype of computation practice game, currently only supporting natural logarithm.
| ... | ... |
@@ -0,0 +1,61 @@ |
| 1 |
+local function identity(x) return x end |
|
| 2 |
+ |
|
| 3 |
+local TYPES = {
|
|
| 4 |
+ ln = {
|
|
| 5 |
+ -- inputs to the operation |
|
| 6 |
+ argc = 1, |
|
| 7 |
+ -- bounds on inputs |
|
| 8 |
+ min = {0.01},
|
|
| 9 |
+ max = {1000},
|
|
| 10 |
+ -- how to present the problem (for string.format) |
|
| 11 |
+ disp = "ln(%.2f)", |
|
| 12 |
+ -- transform input to output |
|
| 13 |
+ func = math.log, |
|
| 14 |
+ -- check guess g to canonical answer c |
|
| 15 |
+ good = function(c, g) return math.abs(c - g) < 0.05 end, |
|
| 16 |
+ -- transform range [0, 1) to a good distribution |
|
| 17 |
+ bias = function(r) return r^2 end, |
|
| 18 |
+ }, |
|
| 19 |
+} |
|
| 20 |
+ |
|
| 21 |
+-- ideally, configure these parameters with options/inputs |
|
| 22 |
+local COUNT = 10 |
|
| 23 |
+local TYPE = TYPES.ln |
|
| 24 |
+local TRIES = 1 |
|
| 25 |
+local TIME = 3 |
|
| 26 |
+ |
|
| 27 |
+local sc = 0 |
|
| 28 |
+local oc = 0 |
|
| 29 |
+for i = 1,COUNT do |
|
| 30 |
+ local rarg = {}
|
|
| 31 |
+ for j = 1,TYPE.argc do |
|
| 32 |
+ table.insert(rarg, TYPE.min[j] + (TYPE.max[j] - TYPE.min[j]) * (TYPE.bias or identity)(math.random())) |
|
| 33 |
+ end |
|
| 34 |
+ local ans = TYPE.func(table.unpack(rarg)) |
|
| 35 |
+ io.write(string.format(TYPE.disp .. " ? ", table.unpack(rarg))) |
|
| 36 |
+ io.flush() |
|
| 37 |
+ local st = os.time() |
|
| 38 |
+ local inp |
|
| 39 |
+ local fails = 0 |
|
| 40 |
+ while os.time() - st < TIME do |
|
| 41 |
+ inp = tonumber(io.read("l")) or 0
|
|
| 42 |
+ if os.time() - st > TIME then |
|
| 43 |
+ if TYPE.good(ans, inp) then |
|
| 44 |
+ oc = oc + 1 |
|
| 45 |
+ end |
|
| 46 |
+ io.write(string.format("too slow! (%.2f)\n", ans))
|
|
| 47 |
+ elseif TYPE.good(ans, inp) then |
|
| 48 |
+ io.write("✓ correct\n")
|
|
| 49 |
+ sc = sc + 1 |
|
| 50 |
+ break |
|
| 51 |
+ elseif fails < TRIES then |
|
| 52 |
+ io.write("wrong, try again ")
|
|
| 53 |
+ io.flush() |
|
| 54 |
+ fails = fails + 1 |
|
| 55 |
+ else |
|
| 56 |
+ io.write(string.format("wrong (%.2f)\n", ans))
|
|
| 57 |
+ break |
|
| 58 |
+ end |
|
| 59 |
+ end |
|
| 60 |
+end |
|
| 61 |
+io.write(string.format("%d/%d correct, of which %d were too slow\n", (sc + oc), COUNT, oc))
|
|
| 0 | 62 |