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

Commit cede051

Browse files
committed
Give each class its own file.
Much easier to navigate the parsons code with it split out. Signed-off-by: Brad Miller <bonelake@mac.com>
1 parent ec09ec2 commit cede051

4 files changed

Lines changed: 1018 additions & 1016 deletions

File tree

runestone/parsons/js/lineGrader.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
export default class LineBasedGrader {
2+
constructor(problem) {
3+
this.problem = problem;
4+
}
5+
// Use a LIS (Longest Increasing Subsequence) algorithm to return the indexes
6+
// that are not part of that subsequence.
7+
inverseLISIndices(arr) {
8+
// Get all subsequences
9+
var allSubsequences = [];
10+
for (var i = 0; i < arr.length; i++) {
11+
var subsequenceForCurrent = [arr[i]],
12+
current = arr[i],
13+
lastElementAdded = -1;
14+
for (var j = i; j < arr.length; j++) {
15+
var subsequent = arr[j];
16+
if (subsequent > current && lastElementAdded < subsequent) {
17+
subsequenceForCurrent.push(subsequent);
18+
lastElementAdded = subsequent;
19+
}
20+
}
21+
allSubsequences.push(subsequenceForCurrent);
22+
}
23+
// Figure out the longest one
24+
var longestSubsequenceLength = -1;
25+
var longestSubsequence;
26+
for (let i in allSubsequences) {
27+
var subs = allSubsequences[i];
28+
if (subs.length > longestSubsequenceLength) {
29+
longestSubsequenceLength = subs.length;
30+
longestSubsequence = subs;
31+
}
32+
}
33+
// Create the inverse indexes
34+
var indexes = [];
35+
var lIndex = 0;
36+
for (let i = 0; i < arr.length; i++) {
37+
if (lIndex > longestSubsequence.length) {
38+
indexes.push(i);
39+
} else {
40+
if (arr[i] == longestSubsequence[lIndex]) {
41+
lIndex += 1;
42+
} else {
43+
indexes.push(i);
44+
}
45+
}
46+
}
47+
return indexes;
48+
}
49+
// grade that element, returning the state
50+
grade() {
51+
var problem = this.problem;
52+
problem.clearFeedback();
53+
this.correctLines = 0;
54+
this.percentLines = 0;
55+
this.incorrectIndents = 0;
56+
var solutionLines = problem.solution;
57+
var answerLines = problem.answerLines();
58+
var i;
59+
var state;
60+
this.percentLines =
61+
Math.min(answerLines.length, solutionLines.length) /
62+
Math.max(answerLines.length, solutionLines.length);
63+
if (answerLines.length < solutionLines.length) {
64+
state = "incorrectTooShort";
65+
this.correctLength = false;
66+
} else if (answerLines.length == solutionLines.length) {
67+
this.correctLength = true;
68+
} else {
69+
this.correctLength = false;
70+
}
71+
72+
// Determine whether the code **that is there** is in the correct order
73+
// If there is too much or too little code this only matters for
74+
// calculating a percentage score.
75+
let isCorrectOrder = true;
76+
this.correctLines = 0;
77+
this.solutionLength = solutionLines.length;
78+
let loopLimit = Math.min(solutionLines.length, answerLines.length);
79+
for (i = 0; i < loopLimit; i++) {
80+
if (answerLines[i].text !== solutionLines[i].text) {
81+
isCorrectOrder = false;
82+
} else {
83+
this.correctLines += 1;
84+
}
85+
}
86+
87+
// Determine whether blocks are indented correctly
88+
this.indentLeft = [];
89+
this.indentRight = [];
90+
for (i = 0; i < loopLimit; i++) {
91+
if (answerLines[i].viewIndent() < solutionLines[i].indent) {
92+
this.indentRight.push(answerLines[i]);
93+
} else if (answerLines[i].viewIndent() > solutionLines[i].indent) {
94+
this.indentLeft.push(answerLines[i]);
95+
}
96+
}
97+
this.incorrectIndents =
98+
this.indentLeft.length + this.indentRight.length;
99+
if (
100+
this.incorrectIndents == 0 &&
101+
isCorrectOrder &&
102+
this.correctLength
103+
) {
104+
// Perfect
105+
state = "correct";
106+
} else if (this.correctLength && isCorrectOrder) {
107+
state = "incorrectIndent";
108+
} else if (!isCorrectOrder && state != "incorrectTooShort") {
109+
state = "incorrectMoveBlocks";
110+
}
111+
this.calculatePercent();
112+
this.graderState = state;
113+
return state;
114+
}
115+
116+
calculatePercent() {
117+
let numLines = this.percentLines * 0.2;
118+
let lines = this.problem.answerLines().length;
119+
let numCorrectBlocks = (this.correctLines / lines) * 0.4;
120+
let numCorrectIndents =
121+
((this.correctLines - this.incorrectIndents) / lines) * 0.4;
122+
123+
this.problem.percent = numLines + numCorrectBlocks + numCorrectIndents;
124+
}
125+
}

0 commit comments

Comments
 (0)