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

Commit df14b13

Browse files
committed
Run black and prettier for formatting
1 parent 8ed94a2 commit df14b13

3 files changed

Lines changed: 166 additions & 162 deletions

File tree

runestone/parsons/js/dagGrader.js

Lines changed: 92 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,121 @@
11
import LineBasedGrader from "./lineGrader";
2-
// import { DiGraph } from "jsnetworkx/node/classes";
3-
// import { hasPath } from "jsnetworkx/node/algorithms/shortestPaths/generic";
4-
// import { isDirectedAcyclicGraph } from "jsnetworkx/node/algorithms/dag";
5-
6-
import { DiGraph, hasPath, isDirectedAcyclicGraph } from "./dagHelpers"
2+
import { DiGraph, hasPath, isDirectedAcyclicGraph } from "./dagHelpers";
73

84
function graphToNX(answerLines) {
9-
var graph = new DiGraph();
10-
for (let line1 of answerLines) {
11-
graph.addNode(line1.tag);
12-
for (let line2tag of line1.depends) {
13-
// the depends graph lists the *incoming* edges of a node
14-
graph.addEdge(line2tag, line1.tag);
15-
}
5+
var graph = new DiGraph();
6+
for (let line1 of answerLines) {
7+
graph.addNode(line1.tag);
8+
for (let line2tag of line1.depends) {
9+
// the depends graph lists the *incoming* edges of a node
10+
graph.addEdge(line2tag, line1.tag);
1611
}
17-
return graph;
12+
}
13+
return graph;
1814
}
1915

2016
function isVertexCover(graph, vertexCover) {
21-
for (let edge of graph.edges()) {
22-
if (!(vertexCover.has(edge[0]) || vertexCover.has(edge[1]))) {
23-
return false;
24-
}
17+
for (let edge of graph.edges()) {
18+
if (!(vertexCover.has(edge[0]) || vertexCover.has(edge[1]))) {
19+
return false;
2520
}
26-
return true;
21+
}
22+
return true;
2723
}
2824

2925
function allSubsets(arr) {
30-
let subsets = {};
31-
for (let i = 0; i <= arr.length; i++) {
32-
subsets[i] = [];
26+
let subsets = {};
27+
for (let i = 0; i <= arr.length; i++) {
28+
subsets[i] = [];
29+
}
30+
for (let i = 0; i < Math.pow(2, arr.length); i++) {
31+
let bin = i.toString(2);
32+
while (bin.length < arr.length) {
33+
bin = "0" + bin;
3334
}
34-
for (let i = 0; i < Math.pow(2, arr.length); i++) {
35-
let bin = i.toString(2);
36-
while (bin.length < arr.length) {
37-
bin = "0" + bin;
38-
}
39-
let subset = new Set();
40-
for (let j = 0; j < bin.length; j++) {
41-
if (bin[j] == '1') {
42-
subset.add(arr[j]);
43-
}
44-
}
45-
subsets[subset.size].push(subset);
35+
let subset = new Set();
36+
for (let j = 0; j < bin.length; j++) {
37+
if (bin[j] == "1") {
38+
subset.add(arr[j]);
39+
}
4640
}
47-
return subsets;
41+
subsets[subset.size].push(subset);
42+
}
43+
return subsets;
4844
}
4945

5046
export default class DAGGrader extends LineBasedGrader {
47+
inverseLISIndices(arr, inSolution) {
48+
// For more details and a proof of the correctness of the algorithm, see the paper: https://arxiv.org/abs/2204.04196
5149

52-
inverseLISIndices(arr, inSolution) {
53-
// For more details and a proof of the correctness of the algorithm, see the paper: https://arxiv.org/abs/2204.04196
50+
var solution = this.problem.solution;
51+
var answerLines = inSolution.map((block) => block.lines[0]); // assume NOT adaptive for DAG grading (for now)
5452

55-
var solution = this.problem.solution;
56-
var answerLines = inSolution.map(block => block.lines[0]); // assume NOT adaptive for DAG grading (for now)
53+
let graph = graphToNX(solution);
5754

58-
let graph = graphToNX(solution);
55+
let seen = new Set();
56+
let problematicSubgraph = new DiGraph();
57+
for (let line1 of answerLines) {
58+
for (let line2 of seen) {
59+
let problematic = hasPath(graph, {
60+
source: line1.tag,
61+
target: line2.tag,
62+
});
63+
if (problematic) {
64+
problematicSubgraph.addEdge(line1.tag, line2.tag);
65+
}
66+
}
5967

60-
let seen = new Set();
61-
let problematicSubgraph = new DiGraph();
62-
for (let line1 of answerLines) {
63-
for (let line2 of seen) {
64-
let problematic = hasPath(graph, {source: line1.tag, target: line2.tag});
65-
if (problematic) {
66-
problematicSubgraph.addEdge(line1.tag, line2.tag);
67-
}
68-
}
68+
seen.add(line1);
69+
}
6970

70-
seen.add(line1);
71+
let mvc = null;
72+
let subsets = allSubsets(problematicSubgraph.nodes());
73+
for (let i = 0; i <= problematicSubgraph.numberOfNodes(); i++) {
74+
for (let subset of subsets[i]) {
75+
if (isVertexCover(problematicSubgraph, subset)) {
76+
mvc = subset;
77+
break;
7178
}
79+
}
80+
if (mvc != null) {
81+
break;
82+
}
83+
}
7284

73-
let mvc = null;
74-
let subsets = allSubsets(problematicSubgraph.nodes());
75-
for (let i = 0; i <= problematicSubgraph.numberOfNodes(); i++) {
76-
for (let subset of subsets[i]) {
77-
if (isVertexCover(problematicSubgraph, subset)) {
78-
mvc = subset;
79-
break;
80-
}
81-
}
82-
if (mvc != null) {
83-
break;
84-
}
85-
}
85+
let indices = [...mvc].map((tag) => {
86+
for (let i = 0; i < answerLines.length; i++) {
87+
if (answerLines[i].tag === tag) return i;
88+
}
89+
});
90+
return indices;
91+
}
8692

87-
let indices = ([...mvc].map(tag => {
88-
for (let i = 0; i < answerLines.length; i++) {
89-
if (answerLines[i].tag === tag) return i;
90-
}
91-
}));
92-
return indices;
93+
checkCorrectOrdering(solutionLines, answerLines) {
94+
if (!isDirectedAcyclicGraph(graphToNX(solutionLines))) {
95+
throw "Dependency between blocks does not form a Directed Acyclic Graph; Problem unsolvable.";
9396
}
9497

95-
checkCorrectOrdering(solutionLines, answerLines) {
96-
if (!(isDirectedAcyclicGraph(graphToNX(solutionLines)))) {
97-
throw "Dependency between blocks does not form a Directed Acyclic Graph; Problem unsolvable."
98+
let seen = new Set();
99+
let isCorrectOrder = true;
100+
this.correctLines = 0;
101+
this.solutionLength = solutionLines.length;
102+
let loopLimit = Math.min(solutionLines.length, answerLines.length);
103+
for (let i = 0; i < loopLimit; i++) {
104+
let line = answerLines[i];
105+
if (line.distractor) {
106+
isCorrectOrder = false;
107+
} else {
108+
for (let j = 0; j < line.depends.length; j++) {
109+
if (!seen.has(line.depends[j])) {
110+
isCorrectOrder = false;
111+
}
98112
}
99-
100-
let seen = new Set();
101-
let isCorrectOrder = true;
102-
this.correctLines = 0;
103-
this.solutionLength = solutionLines.length;
104-
let loopLimit = Math.min(solutionLines.length, answerLines.length);
105-
for (let i = 0; i < loopLimit; i++) {
106-
let line = answerLines[i];
107-
if (line.distractor) {
108-
isCorrectOrder = false;
109-
} else {
110-
for (let j = 0; j < line.depends.length; j++) {
111-
if (!seen.has(line.depends[j])) {
112-
isCorrectOrder = false;
113-
}
114-
}
115-
}
116-
if (isCorrectOrder) {
117-
this.correctLines += 1;
118-
}
119-
seen.add(line.tag)
120-
}
121-
return isCorrectOrder
113+
}
114+
if (isCorrectOrder) {
115+
this.correctLines += 1;
116+
}
117+
seen.add(line.tag);
122118
}
123-
119+
return isCorrectOrder;
120+
}
124121
}

0 commit comments

Comments
 (0)