Zum Inhalt springen

Modul:Mathe für Nicht-Freaks/Umfrage

Aus Wikibooks

Informationen zu dieser Dokumentation
Modul Mathe für Nicht-Freaks

Es existiert keine Dokumentation. Wenn du eine anlegen willst, solltest du keine „/Doku“-Unteseite benutzen, sondern LuaDokumentation.


Information


local Modul = {}

local utils = require("Modul:Utils")

function mean(numbers)
    local result = 0
    for k, v in ipairs(numbers) do
        result = result + v
    end
    return result / #numbers
end

function std(numbers)
    local mean = mean(numbers)
    local result = 0
    for k, v in ipairs(numbers) do
        result = result + (v-mean)*(v-mean)
    end
    return math.sqrt(result / #numbers)
end

function parse_question(question, answer, section)
    local result = { question = question,
                     answer_code = answer,
                     section = section }

    local lines  = string.gmatch(answer, "[^\r\n]+")

    function parse_lines()
        while true do
            local line = lines()

            if line then
                if utils.starts(line, "*") then
                    return tonumber(string.match(line, "^%*%s*(%d+)"))
                end
            elseif line == nil then
                return nil
            end
        end
    end

    result.answers = utils.to_table(parse_lines)
    result.answer_mean = mean(result.answers)
    result.answer_std = std(result.answers)

    return result
end

result_template = [[{| class="wikitable sortable"
! Bereich
! %s
! Mittelwert ± Standardabweichung
! Anzahl Antworten%s
|}]]

function Modul.umfrage(frame)
    local args = frame.args
    local section_index = 0
    local result = ""

    function get_arg(...)
        return args[mw.text.listToText(arg, "", "")]
    end

    function sections()
        section_index = section_index + 1
        return get_arg("bereich", section_index)
    end

    function questions()
        local section = sections()
        local question_index = 0

        return function()
            while section do
                question_index = question_index + 1
                
                question = get_arg("frage", section_index, "_", question_index)
                answer = get_arg("antwort", section_index, "_", question_index)

                if answer ~= nil then
                    return parse_question(question, answer, section)
                else
                    section = sections()
                    question_index = 0
                end
            end
            
            return nil
        end
    end

    for question in questions() do
        result = result .. "\n"
        result = result .. "|-\n"
        result = result .. "|" .. question.section .. "\n"
        result = result .. "|" .. question.question .. "\n"
        result = result .. "|" .. string.format("%3d", question.answer_mean) .. " ± " .. string.format("%3d", question.answer_std) .. "\n"
        result = result .. "|" .. #question.answers .. "\n"
    end

    return string.format(result_template, args.typ, result)
end

return Modul