-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
80 lines (68 loc) · 2.31 KB
/
Copy pathmain.js
File metadata and controls
80 lines (68 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
let pyodide;
let currentProblem;
async function loadPyodideAndPackages() {
pyodide = await loadPyodide();
console.log("Pyodide loaded!");
}
loadPyodideAndPackages();
async function runCustomTest() {
const code = document.getElementById("code").value;
const input = document.getElementById("stdin").value;
try {
const wrappedCode = `
import sys
from io import StringIO
sys.stdin = StringIO("""${input}""")
sys.stdout = StringIO()
${code}
output = sys.stdout.getvalue()
`;
await pyodide.runPythonAsync(wrappedCode);
const output = pyodide.globals.get("output").trim();
document.getElementById("output").innerText = output;
} catch (err) {
document.getElementById("output").innerText = "Error: " + err;
}
}
async function submitCode() {
const code = document.getElementById("code").value;
let results = [];
for (let tc of currentProblem.test_cases) {
const wrappedCode = `
import sys
from io import StringIO
sys.stdin = StringIO("""${tc.input}""")
sys.stdout = StringIO()
${code}
output = sys.stdout.getvalue()
`;
try {
await pyodide.runPythonAsync(wrappedCode);
const output = pyodide.globals.get("output").trim();
const passed = output === tc.expected;
results.push(passed ? "✅ Passed" : `❌ Failed (Input: ${tc.input}, Expected: ${tc.expected}, Got: ${output})`);
} catch (err) {
results.push("❌ Runtime Error: " + err);
}
}
document.getElementById("output").innerText = results.join("\n");
}
async function loadProblem(index) {
const res = await fetch(`problems/problem${index}.json`);
currentProblem = await res.json();
document.getElementById("title").innerText = currentProblem.title;
document.getElementById("desc").innerText = currentProblem.description;
document.getElementById("sample-input").innerText = currentProblem.sample_input || "";
document.getElementById("sample-output").innerText = currentProblem.sample_output || "";
document.getElementById("code").value = currentProblem.starter_code;
document.getElementById("stdin").value = currentProblem.input || "";
}
function listProblems() {
for (let i = 1; i <= 2; i++) {
const li = document.createElement("li");
li.innerText = `Problem ${i}`;
li.onclick = () => loadProblem(i);
document.getElementById("problem-list").appendChild(li);
}
}
listProblems();