Jump to content

Module:Repeat symbols

From Wikipedia, the free encyclopedia
This is the current revision of this page, as edited by Lemondoge (talk | contribs) at 22:05, 31 March 2023 (Removed redundant assertion; there is no need to check whether "base" exists because it'll default to 10 anyways). The present address (URL) is a permanent link to this version.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
require('strict')

local p = {}

function p.main(frame)
	local n = tonumber(frame.args.n)
	assert(n, 'You must provide a number for n')
	assert(n == math.floor(n), 'n must be an integer')
	local base = tonumber(frame.args.base or 10)
	assert(base == math.floor(base), 'base must be an integer')
	local digits, counts, outputs = {}, {}, {}
	for k,v in ipairs(frame.args) do
		digits[k] = v
		counts[k] = n % base
		n = (n - counts[k]) / base
	end
	counts[#counts] = counts[#counts] + n * base
	local tail = #digits + 1
	for k,v in ipairs(digits) do
		outputs[k] = v:rep(counts[tail - k])
	end
	return table.concat(outputs)
end

return p