{"id":252,"date":"2016-07-04T23:03:41","date_gmt":"2016-07-04T22:03:41","guid":{"rendered":"https:\/\/solidt.eu\/blog\/?p=252"},"modified":"2023-05-12T10:28:26","modified_gmt":"2023-05-12T09:28:26","slug":"lua-math-questions","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/lua-math-questions\/","title":{"rendered":"Lua math questions"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"lua\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">#!\/usr\/bin\/lua\nlocal app = {}\nlocal fmt = string.format\nlocal write = io.write\nlocal exam = {}\n\n\nlocal color = {\n\tNoColor = 0,\n\tBlack = 30,\n\tRed = 31,\n\tGreen = 32,\n\tBrown = 33,\n\tBlue = 34,  \n\tPurple = 35,\n\tCyan = 36, \n\tLightGray = 37\n};\n\nfunction setColor(color, light)\n\tif color == 0 then\n\t\twrite('\\x1B[0m');\n\telseif light then\n\t\twrite(fmt('\\x1B[1;%dm', color));\n\telse\n\t\twrite(fmt('\\x1B[0;%dm', color));\n\tend\nend\n\n--exam.__index = exam\nfunction exam:new()\n\tlocal n = {}\n\tsetmetatable(n, exam)\n\texam:init()\n\treturn n\nend\n\nfunction exam:init()\n\tself.start = os.time()\n\tself.finished = 0\n\tself.correct = 0\n\tself.wrong = 0\nend\n\nlocal minVal = 0;\nlocal maxVal = 20;\nfunction getVal(notZero)\n\tlocal v = math.random(minVal, maxVal)\n\twhile notZero and v == 0 do\n\t\tv = math.random(minVal, maxVal)\n\tend\n\treturn v;\nend\n\nfunction exam:getQuestion()\n\tqt = math.random(1,3)\n\tx = getVal()\n\ty = getVal()\n\tif (qt == 1) then\n\t    q = fmt(\"# %d + %d = ?\", x, y)\n\t\ta = x + y\n\telseif (qt == 2) then\n\t    q = fmt(\"# %d - %d = ?\", x, y)\n\t\ta = x - y\n\telseif (qt == 3) then\n\t    q = fmt(\"# %d * %d = ?\", x, y)\n\t\ta = x * y\n\telseif (qt == 4) then\n\t    y = getVal(true)\n\t    q = fmt(\"# %d \/ %d = ?\", x, y)\n\t\ta = x \/ y\n\tend\n\treturn q, a\nend\n\nfunction exam:finish()\n\tself.finished = os.time()\nend\n\nfunction exam:printResults()\n\tlocal diff = os.difftime(self.finished, self.start)\n\twrite( fmt(\"Exam time: %d sec.\\n\", diff) )\n\twrite( fmt(\"%f sec. per correct answer\\n\", diff \/ self.correct))\n\twrite( fmt(\"%d total,  %d correct, %d wrong\\n\", self.correct+self.wrong, self.correct, self.wrong) )\nend\n\nfunction app.main()\n\tmath.randomseed(os.time())\n\tlocal myExam = exam:new()\n\n\trepeat\n\t\tlocal q, a = exam:getQuestion()\n\t\twrite(q, \"\\n\")\n\t\tanswerStr = io.read()\n\t\tanswer = tonumber(answerStr)\n\t\tif (answer) then\n\t\t\t\tif (answer >= a - 0.01 and answer &lt;= a + 0.01) then\n\t\t\t\t\tsetColor(color.Green);\n\t\t\t\t\twrite(\"Correct!\\n\")\t\t\t\t\t\n\t\t\t\t\texam.correct = exam.correct + 1\n\t\t\t\telse\n\t\t\t\t\tsetColor(color.Red);\n\t\t\t\t\twrite(\"Wrong, correct answer: \", a,\"\\n\")\n\t\t\t\t\texam.wrong = exam.wrong + 1\n\t\t\t\tend\n\t\t\tsetColor(color.NoColor);\n\t\t\twrite(string.rep(\"-\", 30), \"\\n\")\n\t\tend\n\tuntil answerStr == \"\"\n\n\texam:finish()\n\texam:printResults()\nend\n\napp.main()\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[9],"tags":[],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-lua"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"predecessor-version":[{"id":7780,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/252\/revisions\/7780"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}