Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit fdf255b

Browse files
committed
Merge branch 'Parson_indentation_fix' of https://github.com/ascholerChemeketa/RunestoneComponents into ascholerChemeketa-Parson_indentation_fix
2 parents 9694e1c + 40e0ebe commit fdf255b

File tree

5 files changed

+240
-120
lines changed

5 files changed

+240
-120
lines changed

runestone/parsons/js/dagGrader.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,31 @@ export default class DAGGrader extends LineBasedGrader {
9292
return indices;
9393
}
9494

95+
checkCorrectIndentation(solutionLines, answerLines) {
96+
this.indentLeft = [];
97+
this.indentRight = [];
98+
99+
let indentationByTag = {};
100+
for (let i = 0; i < solutionLines.length; i++) {
101+
const line = solutionLines[i];
102+
indentationByTag[line.tag] = line.indent;
103+
}
104+
105+
let loopLimit = Math.min(solutionLines.length, answerLines.length);
106+
for (let i = 0; i < loopLimit; i++) {
107+
let solutionIndent = indentationByTag[answerLines[i].tag];
108+
if (answerLines[i].viewIndent() < solutionIndent) {
109+
this.indentRight.push(answerLines[i]);
110+
} else if (answerLines[i].viewIndent() > solutionIndent) {
111+
this.indentLeft.push(answerLines[i]);
112+
}
113+
}
114+
this.incorrectIndents =
115+
this.indentLeft.length + this.indentRight.length;
116+
117+
return this.incorrectIndents == 0;
118+
}
119+
95120
checkCorrectOrdering(solutionLines, answerLines) {
96121
if (!isDirectedAcyclicGraph(graphToNX(solutionLines))) {
97122
throw "Dependency between blocks does not form a Directed Acyclic Graph; Problem unsolvable.";

runestone/parsons/js/lineGrader.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,10 @@ export default class LineBasedGrader {
7575
let isCorrectOrder = this.checkCorrectOrdering(solutionLines, answerLines)
7676

7777
// Determine whether blocks are indented correctly
78-
this.indentLeft = [];
79-
this.indentRight = [];
80-
let loopLimit = Math.min(solutionLines.length, answerLines.length);
81-
for (i = 0; i < loopLimit; i++) {
82-
if (answerLines[i].viewIndent() < solutionLines[i].indent) {
83-
this.indentRight.push(answerLines[i]);
84-
} else if (answerLines[i].viewIndent() > solutionLines[i].indent) {
85-
this.indentLeft.push(answerLines[i]);
86-
}
87-
}
88-
this.incorrectIndents =
89-
this.indentLeft.length + this.indentRight.length;
78+
let isCorrectIndents = this.checkCorrectIndentation(solutionLines, answerLines);
79+
9080
if (
91-
this.incorrectIndents == 0 &&
81+
isCorrectIndents &&
9282
isCorrectOrder &&
9383
this.correctLength
9484
) {
@@ -105,6 +95,23 @@ export default class LineBasedGrader {
10595
return state;
10696
}
10797

98+
checkCorrectIndentation(solutionLines, answerLines) {
99+
this.indentLeft = [];
100+
this.indentRight = [];
101+
let loopLimit = Math.min(solutionLines.length, answerLines.length);
102+
for (let i = 0; i < loopLimit; i++) {
103+
if (answerLines[i].viewIndent() < answerLines[i].indent) {
104+
this.indentRight.push(answerLines[i]);
105+
} else if (answerLines[i].viewIndent() > solutionLines[i].indent) {
106+
this.indentLeft.push(answerLines[i]);
107+
}
108+
}
109+
this.incorrectIndents =
110+
this.indentLeft.length + this.indentRight.length;
111+
112+
return this.incorrectIndents == 0;
113+
}
114+
108115
checkCorrectOrdering(solutionLines, answerLines) {
109116
let isCorrectOrder = true;
110117
this.correctLines = 0;

runestone/parsons/js/parsons.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ export default class Parsons extends RunestoneBase {
309309
s.replace(/\s+/g, "")
310310
); // remove whitespace in tag and depends list
311311
tagIndex = textBlock.indexOf("#tag:");
312-
tag = textBlock.substring(tagIndex + 5, tagIndex + 6);
312+
tag = textBlock.substring(
313+
tagIndex + 5,
314+
textBlock.indexOf(";", tagIndex + 5)
315+
);
316+
if (tag == "")
317+
tag = "block-" + i;
313318
dependsIndex = textBlock.indexOf(";depends:");
314319
let dependsString = textBlock.substring(
315320
dependsIndex + 9,

runestone/parsons/test/_sources/index.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,34 @@ Proof Blocks
147147
$c'(u) \ne c'(v)$ #tag:7;depends:6,3;
148148
=====
149149
$c'$ is a 2-coloring of $H$, so $H$ is 2-colorable. (end of proof) #tag:8;depends:7;
150+
151+
============
152+
Code DAG
153+
============
154+
155+
.. parsonsprob:: test_parsons_dag_indent
156+
:grader: dag
157+
158+
Test that indentation works with dag
159+
160+
.. code-block:: python
161+
162+
def foo:
163+
if True:
164+
return 2
165+
def bar:
166+
return 40
167+
print(foo() + bar())
168+
169+
-----
170+
def foo(): #tag:foo;depends:;
171+
=====
172+
if True: #tag:foo_if; depends:foo;
173+
=====
174+
return 2 #tag:f1; depends:foo_if;
175+
=====
176+
def bar(): #tag:bar;depends:;
177+
=====
178+
return 40 #tag:b1; depends:bar;
179+
=====
180+
print(foo() + bar()) #tag:asdf; depends: f1,b1;

0 commit comments

Comments
 (0)