Skip to content

Commit 6a3aa91

Browse files
author
Philip Guo
committed
chugging along
1 parent c01cf7e commit 6a3aa91

3 files changed

Lines changed: 102 additions & 5 deletions

File tree

edu-python-questions.js

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ var tests = null;
2929
var expects = null;
3030
var curTestIndex = -1;
3131

32+
// the traces returned by executing the respective 'tests' and 'expects'
33+
// Python code. See resetResultTraces for invariants.
34+
var testsTraces = null;
35+
var expectsTraces = null;
36+
37+
// Pre: 'tests' and 'expects' are non-null
38+
function resetResultTraces() {
39+
testsTraces = [];
40+
expectsTraces = [];
41+
$.each(tests, function(i) {
42+
testsTraces.push(null);
43+
expectsTraces.push(null);
44+
});
45+
46+
assert(testsTraces.length > 0);
47+
assert(testsTraces.length == expectsTraces.length);
48+
assert(testsTraces.length == tests.length);
49+
assert(expectsTraces.length == expects.length);
50+
}
51+
3252

3353
$(document).ready(function() {
3454
eduPythonCommonInit(); // must call this first!
@@ -79,6 +99,9 @@ function enterVisualizeMode() {
7999

80100
$("#HintStatement").show();
81101
$("#SolutionStatement").show();
102+
103+
$('#submitBtn').html("Submit answer");
104+
$('#submitBtn').attr('disabled', false);
82105
}
83106

84107
function enterGradingMode() {
@@ -93,6 +116,42 @@ function enterGradingMode() {
93116
}
94117

95118

119+
// returns a curried function!
120+
function genTestResultHandler(idx) {
121+
function ret(traceData) {
122+
assert(testsTraces[idx] === null);
123+
testsTraces[idx] = traceData;
124+
125+
// if ALL results have been delivered, then call
126+
// readyToGradeSubmission() ...
127+
for (var i = 0; i < testsTraces.length; i++) {
128+
if (testsTraces[i] === null || expectsTraces[i] === null) {
129+
return;
130+
}
131+
}
132+
readyToGradeSubmission();
133+
}
134+
return ret;
135+
}
136+
137+
function genExpectResultHandler(idx) {
138+
function ret(traceData) {
139+
assert(expectsTraces[idx] === null);
140+
expectsTraces[idx] = traceData;
141+
142+
// if ALL results have been delivered, then call
143+
// readyToGradeSubmission() ...
144+
for (var i = 0; i < testsTraces.length; i++) {
145+
if (testsTraces[i] === null || expectsTraces[i] === null) {
146+
return;
147+
}
148+
}
149+
readyToGradeSubmission();
150+
}
151+
return ret;
152+
}
153+
154+
96155
function finishQuestionsInit(questionsDat) {
97156
$("#ProblemName").html(questionsDat.name);
98157
$("#ProblemStatement").html(questionsDat.question);
@@ -115,7 +174,9 @@ function finishQuestionsInit(questionsDat) {
115174
tests = questionsDat.tests;
116175
expects = questionsDat.expects;
117176
curTestIndex = 0;
118-
assert(tests.length == expects.length);
177+
178+
resetResultTraces();
179+
119180

120181
$("#testCodeInput").val(tests[curTestIndex]);
121182

@@ -152,9 +213,31 @@ function finishQuestionsInit(questionsDat) {
152213

153214

154215
$("#submitBtn").click(function() {
155-
enterGradingMode();
156-
console.log("SUBMIT!");
216+
$('#submitBtn').html("Please wait ... submitting ...");
217+
$('#submitBtn').attr('disabled', true);
218+
219+
// right now I make (2 * tests.length) HTTP POST calls, which might
220+
// be inefficient, so optimize later if necessary
221+
//
222+
// remember to code this up VERY CAREFULLY because the responses
223+
// will come in asynchronously and probably OUT-OF-ORDER
224+
for (var i = 0; i < tests.length; i++) {
225+
var submittedCode = concatSolnTestCode($("#actualCodeInput").val(), tests[i]);
226+
$.post("cgi-bin/web_exec.py",
227+
{user_script : submittedCode},
228+
genTestResultHandler(i),
229+
"json");
230+
231+
var expectCode = expects[i];
232+
$.post("cgi-bin/web_exec.py",
233+
{user_script : expectCode},
234+
genExpectResultHandler(i),
235+
"json");
236+
}
157237

238+
return;
239+
240+
enterGradingMode();
158241
$("#submittedCodeRO").val($("#actualCodeInput").val());
159242

160243
// iterate through all pairs of test and expect code:
@@ -177,3 +260,10 @@ function finishQuestionsInit(questionsDat) {
177260
});
178261

179262
}
263+
264+
// should be called after ALL elements in testsTraces and expectsTraces
265+
// have been populated by their respective AJAX POST calls
266+
function readyToGradeSubmission() {
267+
console.log('readyToGradeSubmission');
268+
}
269+

edu-python.css

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,20 @@ div#heapHeader {
563563
}
564564

565565
table#gradeMatrix {
566-
margin-top: 20px;
566+
margin-top: 12px;
567567
}
568568

569569
table#gradeMatrix thead {
570570
font-weight: bold;
571+
border: solid;
571572
}
572573

573574
table#gradeMatrix td {
574575
padding: 5px;
575576
vertical-align: middle;
576577
}
577-
578+
579+
#gradeSummary {
580+
margin-top: 12px;
581+
font-size: 12pt;
582+
}

question.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@
145145
<textarea id="submittedCodeRO" cols="60" rows="12" wrap="off" readonly="readonly">
146146
</textarea>
147147

148+
<div id="gradeSummary">Your solution passed 3/5 tests. Try to debug the incorrect outputs!</div>
149+
148150
<table id="gradeMatrix">
149151
<thead>
150152
<tr>

0 commit comments

Comments
 (0)