Purpose: measure your digit span (verbal working memory).
Usage: digitspan [length]
To test your verbal working memory, you are shown a sequence of digits and challenged to type them back, in the same order, at the end. length is the length of that sequence; if none is given, various lengths will be tried to figure out your limits.
Changelog:
2023-227 initial development
2023-235 reformat/redocument a tad
Source code (perhaps slightly corrupted) is as follows.
local function clear()
io.write("x1b[2Jx1b[H")
io.flush()
end
local function trial(len)
io.write("Trial with " .. len .. " digits
")
io.read()
-- pad out len to some number of sets of digits 0-9
local ds = {}
for i = 1, 10 * math.ceil(len / 10) do
table.insert(ds, (i - 1) % 10)
end
-- https://www.programming-idioms.org/idiom/10/shuffle-a-list/2019/lua
for i = #ds, 2, -1 do
local j = math.random(i)
ds[i], ds[j] = ds[j], ds[i]
end
-- clip off excess after shuffling
for i = len + 1, #ds do
ds[i] = nil
end
for _, d in ipairs(ds) do
clear()
io.write(d .. "
")
os.execute("sleep 1")
end
clear()
io.write("> ")
io.flush()
local rs = io.read()
local rd = {}
for i = 1, #rs do
local c = rs:sub(i, i)
if c >= "0" and c <= "9" then
table.insert(rd, tonumber(c))
end
end
local uc = true
for i = 1, #ds do
if ds[REVERSE and (#ds - i + 1) or i] ~= rd[i] then
uc = false
break
end
end
if uc then
io.write("x1b[92mCORRECTx1b[0m
")
else
io.write("x1b[91mWRONGx1b[0m
")
end
return uc
end
REVERSE = false
local d = math.floor(tonumber(arg[1]))
if d then
-- ask for reps
io.write("Number of trials: ")
io.flush()
local t = tonumber(io.read()) or 5
-- go
local c = 0
for i = 1, t do
if trial(d) then
c = c + 1
end
end
io.write("You succeeded on " .. c .. "/" .. t .. " trials (" .. (100 * c // t) .. "%).
")
else
-- doubling
local k = 1
while trial(k) do
k = 2 * k
end
-- binary search
local j = k // 2
while k - j > 1 do
if trial((j + k) // 2) then
j = (j + k) // 2
else
k = (j + k) // 2
end
end
-- linear descent
while not trial(k) do
k = k - 1
end
io.write("Your digit span is " .. k .. ".
")
end