Module:Rank-n scale: Difference between revisions

Akselai (talk | contribs)
Created page with "local infobox = require('Module:Infobox') local p = {} function p.rotate_word(str, n) -- rotate word s by n steps return string.sub(str, n+1, #str) .. string.sub(str, 1,..."
 
Akselai (talk | contribs)
No edit summary
Line 3: Line 3:
local p = {}
local p = {}


function p.rotate_word(str, n) -- rotate word s by n steps
local function rotate_word(str, n) -- rotate word s by n steps
     return string.sub(str, n+1, #str) .. string.sub(str, 1, n)
     return string.sub(str, n+1, #str) .. string.sub(str, 1, n)
end     
end     


function p.rotate_word_full(str)
local function rotate_word_full(str)
local full = {}
local full = {}
for i = 1, #str do
for i = 1, #str do
full[i] = p.rotate_word(str, i-1)
full[i] = rotate_word(str, i-1)
end
end
return full
return full
end
end


function p.bright_dark_chiral(str)
local function bright_dark_chiral(str)
local full = p.rotate_word_full(str)
local full = rotate_word_full(str)
local bright, dark = str, str
local bright, dark = str, str
for i = 2, #str do
for i = 2, #str do
Line 46: Line 46:
end
end


function p.table_length(T)
local function table_length(T)
local count = 0
local count = 0
for _ in pairs(T) do count = count + 1 end
for _ in pairs(T) do count = count + 1 end
Line 52: Line 52:
end
end


function p.abelianize(str) -- sorts string by letter. from rosetta code because I'm lazy.
local function abelianize(str) -- sorts string by letter. from rosetta code because I'm lazy.
local t = {} str:gsub("(%S)", function(c) t[#t+1]=c end) -- use "(.)" as pattern to preserve whitespace
local t = {} str:gsub("(%S)", function(c) t[#t+1]=c end) -- use "(.)" as pattern to preserve whitespace
     table.sort(t, function(a,b) return a<b end) -- implicitly
     table.sort(t, function(a,b) return a<b end) -- implicitly
Line 58: Line 58:
end
end


function p.list_step_varieties(str)
local function list_step_varieties(str)
local strstr = str .. str
local strstr = str .. str
local step_vars = {}
local step_vars = {}
Line 64: Line 64:
local r = {}
local r = {}
for j = 1, #str do
for j = 1, #str do
local u = p.abelianize(string.sub(strstr, j, j+i-1))
local u = abelianize(string.sub(strstr, j, j+i-1))
r[u] = true
r[u] = true
end
end
Line 72: Line 72:
end
end


function p.max_variety(str)
local function max_variety(str)
local lsv = p.list_step_varieties(str)
local lsv = list_step_varieties(str)
local max_var = 0
local max_var = 0
local is_strict = "yes"
local is_strict = "yes"
for i = 1, math.floor(#str/2) do
for i = 1, math.floor(#str/2) do
i_step_var = p.table_length(lsv[i])
i_step_var = table_length(lsv[i])
if i_step_var > max_var then
if i_step_var > max_var then
if max_var ~= 0 then
if max_var ~= 0 then
Line 89: Line 89:


function p.create_infobox(str)
function p.create_infobox(str)
local mv = p.max_variety(str)
local mv = max_variety(str)
local bdc = p.bright_dark_chiral(str)
local bdc = bright_dark_chiral(str)
local brightest_mode
local brightest_mode