Алексей Безбородов
2 years ago
commit
a32633bb83
5 changed files with 255 additions and 0 deletions
@ -0,0 +1,24 @@ |
|||||||
|
This is free and unencumbered software released into the public domain. |
||||||
|
|
||||||
|
Anyone is free to copy, modify, publish, use, compile, sell, or |
||||||
|
distribute this software, either in source code form or as a compiled |
||||||
|
binary, for any purpose, commercial or non-commercial, and by any |
||||||
|
means. |
||||||
|
|
||||||
|
In jurisdictions that recognize copyright laws, the author or authors |
||||||
|
of this software dedicate any and all copyright interest in the |
||||||
|
software to the public domain. We make this dedication for the benefit |
||||||
|
of the public at large and to the detriment of our heirs and |
||||||
|
successors. We intend this dedication to be an overt act of |
||||||
|
relinquishment in perpetuity of all present and future rights to this |
||||||
|
software under copyright law. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
||||||
|
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
||||||
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
||||||
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
||||||
|
OTHER DEALINGS IN THE SOFTWARE. |
||||||
|
|
||||||
|
For more information, please refer to <https://unlicense.org> |
@ -0,0 +1,3 @@ |
|||||||
|
# Программа для тестирования на языке программирования Python |
||||||
|
|
||||||
|
* Условия, математические операции, циклы |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,217 @@ |
|||||||
|
<!-- Copyright 2022 by Alexei Bezborodov <AlexeiBv+mirocod@narod.ru> --> |
||||||
|
<!DOCTYPE html> |
||||||
|
<html> |
||||||
|
|
||||||
|
<head> |
||||||
|
<meta charset="UTF-8"> |
||||||
|
<title>Проверка знаний кода на питоне</title> |
||||||
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.10.5/brython.min.js"></script> |
||||||
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.10.5/brython_stdlib.js"></script> |
||||||
|
|
||||||
|
<!-- code class="python" --> |
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script> |
||||||
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/python.min.js"></script> |
||||||
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/ocean.min.css" /> |
||||||
|
|
||||||
|
</head> |
||||||
|
<body onload="brython()"> |
||||||
|
|
||||||
|
<table> |
||||||
|
<tr> <td width = 300 height = 200> |
||||||
|
<pre><code class="hljs python" id="out-code"></code></pre> |
||||||
|
</td> </tr> |
||||||
|
</table> |
||||||
|
|
||||||
|
<table> |
||||||
|
<tr> <td width = 150> <input type="button" id="button_1" value="" width = 150></input> </td> <td width = 150> <input type="button" id="button_3" value="" width = 150></input> </td> </tr> |
||||||
|
<tr> <td width = 150> <input type="button" id="button_2" value="" width = 150></input> </td> <td width = 150> <input type="button" id="button_4" value="" width = 150></input> </td> </tr> |
||||||
|
</table> |
||||||
|
|
||||||
|
<p id="print_out"></p> |
||||||
|
<p id="good_answer" style="color:#FFFFFF";></p> |
||||||
|
|
||||||
|
<h3>Результаты</h3> |
||||||
|
<p id="result_out"></p> |
||||||
|
|
||||||
|
<script type="text/python"> |
||||||
|
|
||||||
|
from browser import document, html, window |
||||||
|
import random |
||||||
|
|
||||||
|
global g_good_answer |
||||||
|
global buttons |
||||||
|
buttons = ["button_1", "button_2", "button_3", "button_4"] |
||||||
|
|
||||||
|
global g_CodeTemplates |
||||||
|
g_CodeTemplates = [ |
||||||
|
"print(@rand_int)", |
||||||
|
"print(@rand_int @rand_math @rand_int)", |
||||||
|
"print(@rand_int @rand_math @rand_int @rand_math @rand_int)", |
||||||
|
"print(@rand_int @rand_math (@rand_int @rand_math @rand_int))", |
||||||
|
"print((@rand_int @rand_math @rand_int) @rand_math @rand_int)", |
||||||
|
"x = @rand_int\nprint(x)", |
||||||
|
"x = @rand_int\nfor i in range(@rand_int):\n\tx += i\nprint(x)" |
||||||
|
] |
||||||
|
|
||||||
|
global g_Result |
||||||
|
g_Result = [[]] |
||||||
|
|
||||||
|
global g_Code |
||||||
|
g_Code = "" |
||||||
|
|
||||||
|
def my_prnt(s): |
||||||
|
document["print_out"].innerHTML += str(s) |
||||||
|
|
||||||
|
def CleanPrint(): |
||||||
|
document["print_out"].innerHTML = "" |
||||||
|
|
||||||
|
def GetPrintResult(): |
||||||
|
return document["print_out"].innerHTML |
||||||
|
|
||||||
|
def SetGoodAnswer(s): |
||||||
|
document["good_answer"].innerHTML = str(s) |
||||||
|
|
||||||
|
def CleanGoodAnswer(): |
||||||
|
document["good_answer"].innerHTML = "" |
||||||
|
|
||||||
|
def GetGoodAnswer(): |
||||||
|
return document["good_answer"].innerHTML |
||||||
|
|
||||||
|
def SaveCode(code): |
||||||
|
global g_Code |
||||||
|
g_Code = str(code) |
||||||
|
|
||||||
|
def GetCode(): |
||||||
|
global g_Code |
||||||
|
return g_Code |
||||||
|
|
||||||
|
def AddToResult(a_Code, a_Value, a_Result): |
||||||
|
global g_Result |
||||||
|
g_Result += [[a_Code, a_Value, a_Result]] |
||||||
|
|
||||||
|
res = "<table border = 1> <tr> <th>Код</th> <th>Ответ</th> <th>Результат</th> </tr>" |
||||||
|
for i in range(len(g_Result)): |
||||||
|
r = g_Result[i] |
||||||
|
if len(r) < 3: |
||||||
|
continue |
||||||
|
code = r[0] |
||||||
|
value = r[1] |
||||||
|
result = r[2] |
||||||
|
vcolor = "#FF0000" |
||||||
|
if result == "Верно": |
||||||
|
vcolor = "#00FF00" |
||||||
|
res += "<tr>" + "<td>" + str(code) + "</td>" +"<td>" + str(value) + "</td>" + "<td " + "bgcolor = " + vcolor + ">" + str(result) + "</td></tr>" |
||||||
|
|
||||||
|
document["result_out"].innerHTML = res + "</table>" |
||||||
|
|
||||||
|
def ReplaceRandInt(code): |
||||||
|
return code.replace("@rand_int", str(random.randint(-15, 15)), 1) |
||||||
|
|
||||||
|
def ReplaceRandMath(code): |
||||||
|
math_op = ["+", "-", "*", "/", "%"] |
||||||
|
return code.replace("@rand_math", math_op[random.randint(0, len(math_op))], 1) |
||||||
|
|
||||||
|
def PrepareTemplate(code_template): |
||||||
|
code = code_template |
||||||
|
|
||||||
|
new_code = ReplaceRandInt(code) |
||||||
|
while code != new_code: |
||||||
|
code = new_code |
||||||
|
new_code = ReplaceRandInt(code) |
||||||
|
|
||||||
|
new_code = ReplaceRandMath(code) |
||||||
|
while code != new_code: |
||||||
|
code = new_code |
||||||
|
new_code = ReplaceRandMath(code) |
||||||
|
|
||||||
|
return code |
||||||
|
|
||||||
|
def GetTemplate(): |
||||||
|
global g_CodeTemplates |
||||||
|
template = g_CodeTemplates[random.randint(0, len(g_CodeTemplates))] |
||||||
|
return template |
||||||
|
|
||||||
|
def GenTest(): |
||||||
|
CleanPrint() |
||||||
|
code_template = GetTemplate() |
||||||
|
code = PrepareTemplate(code_template) |
||||||
|
|
||||||
|
code_prnt = code.replace("print", "my_prnt") |
||||||
|
|
||||||
|
try: |
||||||
|
exec(code_prnt) |
||||||
|
except SyntaxError: |
||||||
|
return False |
||||||
|
|
||||||
|
SaveCode(code) |
||||||
|
document["out-code"].innerHTML = window.hljs.highlightAuto(code).value |
||||||
|
|
||||||
|
result = GetPrintResult() |
||||||
|
CleanPrint() |
||||||
|
|
||||||
|
butt_labels = [""] * 4 |
||||||
|
for i in range(4): |
||||||
|
r = random.randint(-5, 5) |
||||||
|
if r == 0: |
||||||
|
r = 1 |
||||||
|
butt_labels[i] = int(result) + r |
||||||
|
|
||||||
|
good_answer = random.randint(0, 3) |
||||||
|
SetGoodAnswer(good_answer) |
||||||
|
butt_labels[good_answer] = result |
||||||
|
|
||||||
|
for i in range(4): |
||||||
|
document[buttons[i]].value = butt_labels[i] |
||||||
|
|
||||||
|
return True |
||||||
|
|
||||||
|
def TrueGenTest(): |
||||||
|
while not GenTest(): |
||||||
|
break |
||||||
|
return |
||||||
|
|
||||||
|
def TestResult(num): |
||||||
|
good_answer = int(GetGoodAnswer()) |
||||||
|
s_res = "Неверно" |
||||||
|
if good_answer == num: |
||||||
|
s_res = "Верно" |
||||||
|
|
||||||
|
AddToResult(GetCode(), document[buttons[num]].value, s_res) |
||||||
|
|
||||||
|
TrueGenTest() |
||||||
|
return |
||||||
|
|
||||||
|
def show_values0(event): |
||||||
|
TestResult(0) |
||||||
|
return |
||||||
|
|
||||||
|
def show_values1(event): |
||||||
|
TestResult(1) |
||||||
|
return |
||||||
|
|
||||||
|
def show_values2(event): |
||||||
|
TestResult(2) |
||||||
|
return |
||||||
|
|
||||||
|
def show_values3(event): |
||||||
|
TestResult(3) |
||||||
|
return |
||||||
|
|
||||||
|
document[buttons[0]].bind("click", show_values0) |
||||||
|
document[buttons[1]].bind("click", show_values1) |
||||||
|
document[buttons[2]].bind("click", show_values2) |
||||||
|
document[buttons[3]].bind("click", show_values3) |
||||||
|
|
||||||
|
TrueGenTest() |
||||||
|
|
||||||
|
</script> |
||||||
|
|
||||||
|
<p > |
||||||
|
|
||||||
|
</p> |
||||||
|
|
||||||
|
</body> |
||||||
|
|
||||||
|
</html> |
||||||
|
|
||||||
|
|
Loading…
Reference in new issue