Skip to content

Commit ca8aeb7

Browse files
committed
simplify test output
1 parent 3663d57 commit ca8aeb7

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

public/index.html

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
integrity="sha512-yh2RE0wZCVZeysGiqTwDTO/dKelCbS9bP2L94UvOFtl/FKXcNAje3Y2oBg/ZMZ3LS1sicYk4dYVGtDex75fvvA=="
2424
crossorigin="anonymous"
2525
referrerpolicy="no-referrer"
26-
/>
26+
>
2727

2828
<!-- Custom styles -->
2929
<link rel="stylesheet" href="style.css">
3030
</head>
3131

3232
<body>
33-
<h1 class="title">Python Coding Challenge</h1>
33+
<h1 class="title">Python Coding Challenge</h1>
3434
<div class="section">
3535
<div class="columns">
3636
<div class="column">
@@ -41,18 +41,24 @@ <h3>Instructions</h3>
4141
</p>
4242
</div>
4343
<div class="column">
44-
<div class="box image is-5by4" id="solution"></div>
45-
<div class="controls">
46-
<button class="button is-primary" id="run">Run</button>
44+
<div class="card">
45+
<header class="card-header"><p class="card-header-title">Your Code</p></header>
46+
<div class="mx-0" id="solution" style="aspect-ratio:4/3">
47+
</div>
48+
<div class="card-footer">
49+
<button class="card-footer-item" id="run">Run</button>
50+
</div>
51+
</div>
52+
<div class="card">
53+
<header class="card-header">
54+
<p class="card-header-title">Output</p>
55+
</header>
56+
<div class="card-content" id="output"></div>
4757
</div>
48-
<div id="output"></div>
4958
</div>
5059
</div>
5160
</div>
52-
<div class="right-half">
53-
</div>
5461
<script type="mpy" src="main.py"></script>
55-
</div>
5662
</body>
5763
</html>
5864

public/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ async def run(event):
2626
global worker
2727

2828
code = editor.getValue()
29-
document.getElementById("output").innerText = "⏳ Running..."
29+
output = document.getElementById("output")
30+
output.innerText = "⏳ Running..."
3031

3132
# ---- TIMEOUT HANDLING ----
3233
timeout_ms = 1500 # 1.5 second limit
@@ -44,19 +45,19 @@ async def run(event):
4445
# Race the worker call vs. timeout
4546
result = await window.Promise.race([eval_promise, timeout_promise])
4647

47-
document.getElementById("output").innerText = f"{result}"
48+
output.innerText = f"{result}"
4849

4950
except Exception as e:
5051
if str(e) == "Error: timeout":
5152
# ---- Worker hung. Kill it. ----
5253
worker.terminate()
5354
worker = make_worker() # start fresh
5455
await worker.ready
55-
document.getElementById("output").innerText = (
56+
output.innerText = (
5657
"💥 Your code took too long (possible infinite loop). Worker reset."
5758
)
5859
else:
59-
document.getElementById("output").innerText = f"💥 Error: {e}"
60+
output.innerText = f"💥 Error: {e}"
6061

6162
document.getElementById("run").addEventListener("click", run)
6263

public/worker.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,8 @@ def evaluate(code):
3030
if check_for_multiplication(code):
3131
return "💥 Your solution cannot use the * operator!"
3232

33-
tests = [
34-
(1,1),
35-
(1,2),
36-
(2,1),
37-
(2,3),
38-
(0,3),
39-
(3,0),
40-
(-2,3),
41-
(2,-3),
42-
]
33+
tests = [(x, y) for x in range(-3,4) for y in range(-3,4)]
34+
4335
results = []
4436
for test in tests:
4537
try:
@@ -52,15 +44,25 @@ def evaluate(code):
5244
results.append( f"error: {e}")
5345

5446
formatted = ""
47+
correct = 0
48+
all_correct = False
5549
for test, result in zip(tests, results):
5650
if str(result).startswith("error:"):
57-
formatted += f"💥 multiply{test} raised an exception: {result[7:]}\n"
58-
elif result == test[0] * test[1]:
59-
formatted += f"✅ multiply{test} = {result} (correct)\n"
51+
if not all_correct:
52+
formatted += f"💥 multiply{test} raised an exception: {result[7:]}\n"
53+
all_correct = True
54+
elif result != test[0] * test[1]:
55+
if not all_correct:
56+
formatted += f"❌ inputs {test}. Expected {test[0] * test[1]}, got {result})\n"
57+
all_correct = True
6058
else:
61-
formatted += f"❌ multiply{test} = {result} (incorrect, expected {test[0] * test[1]})\n"
59+
correct += 1
60+
output = f"{correct} / {len(tests)} correct\n" + formatted
6261

63-
return formatted
62+
if correct == len(tests):
63+
return "✅ All tests passed! Great job!"
64+
else:
65+
return f"{correct} / {len(tests)} correct\n" + formatted
6466

6567

6668

0 commit comments

Comments
 (0)