|
| 1 | +// This file is adapted from lineGrader in parsons directive. |
| 2 | +// We could have fit our data structure to use the original LineBasedGrader directly, |
| 3 | +// but that would result in changes in parsons directive affecting this, so we created a copy |
| 4 | +// instead. |
| 5 | + |
| 6 | +export default class BlockBasedGrader { |
| 7 | + constructor(problem) { |
| 8 | + this.problem = problem; |
| 9 | + } |
| 10 | + // Use a LIS (Longest Increasing Subsequence) algorithm to return the indexes |
| 11 | + // that are not part of that subsequence. |
| 12 | + inverseLISIndices(arr) { |
| 13 | + // Get all subsequences |
| 14 | + var allSubsequences = []; |
| 15 | + for (var i = 0; i < arr.length; i++) { |
| 16 | + var subsequenceForCurrent = [arr[i]], |
| 17 | + current = arr[i], |
| 18 | + lastElementAdded = -1; |
| 19 | + for (var j = i; j < arr.length; j++) { |
| 20 | + var subsequent = arr[j]; |
| 21 | + if (subsequent > current && lastElementAdded < subsequent) { |
| 22 | + subsequenceForCurrent.push(subsequent); |
| 23 | + lastElementAdded = subsequent; |
| 24 | + } |
| 25 | + } |
| 26 | + allSubsequences.push(subsequenceForCurrent); |
| 27 | + } |
| 28 | + // Figure out the longest one |
| 29 | + var longestSubsequenceLength = -1; |
| 30 | + var longestSubsequence; |
| 31 | + for (let i in allSubsequences) { |
| 32 | + var subs = allSubsequences[i]; |
| 33 | + if (subs.length > longestSubsequenceLength) { |
| 34 | + longestSubsequenceLength = subs.length; |
| 35 | + longestSubsequence = subs; |
| 36 | + } |
| 37 | + } |
| 38 | + // Create the inverse indexes |
| 39 | + var indexes = []; |
| 40 | + var lIndex = 0; |
| 41 | + for (let i = 0; i < arr.length; i++) { |
| 42 | + if (lIndex > longestSubsequence.length) { |
| 43 | + indexes.push(i); |
| 44 | + } else { |
| 45 | + if (arr[i] == longestSubsequence[lIndex]) { |
| 46 | + lIndex += 1; |
| 47 | + } else { |
| 48 | + indexes.push(i); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + return indexes; |
| 53 | + } |
| 54 | + // grade that element, returning the state |
| 55 | + grade() { |
| 56 | + this.correctLines = 0; |
| 57 | + this.percentLines = 0; |
| 58 | + var solutionLines = this.solution; |
| 59 | + var answerLines = this.answer; |
| 60 | + var i; |
| 61 | + var state; |
| 62 | + this.percentLines = |
| 63 | + Math.min(answerLines.length, solutionLines.length) / |
| 64 | + Math.max(answerLines.length, solutionLines.length); |
| 65 | + if (answerLines.length < solutionLines.length) { |
| 66 | + state = "incorrectTooShort"; |
| 67 | + this.correctLength = false; |
| 68 | + } else if (answerLines.length == solutionLines.length) { |
| 69 | + this.correctLength = true; |
| 70 | + } else { |
| 71 | + this.correctLength = false; |
| 72 | + } |
| 73 | + |
| 74 | + // Determine whether the code **that is there** is in the correct order |
| 75 | + // If there is too much or too little code this only matters for |
| 76 | + // calculating a percentage score. |
| 77 | + let isCorrectOrder = true; |
| 78 | + this.correctLines = 0; |
| 79 | + this.solutionLength = solutionLines.length; |
| 80 | + let loopLimit = Math.min(solutionLines.length, answerLines.length); |
| 81 | + for (i = 0; i < loopLimit; i++) { |
| 82 | + if (answerLines[i] !== solutionLines[i]) { |
| 83 | + isCorrectOrder = false; |
| 84 | + } else { |
| 85 | + this.correctLines += 1; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if ( |
| 90 | + isCorrectOrder && |
| 91 | + this.correctLength |
| 92 | + ) { |
| 93 | + // Perfect |
| 94 | + state = "correct"; |
| 95 | + } else if (!isCorrectOrder && state != "incorrectTooShort") { |
| 96 | + state = "incorrectMoveBlocks"; |
| 97 | + } |
| 98 | + this.calculatePercent(); |
| 99 | + this.graderState = state; |
| 100 | + return state; |
| 101 | + } |
| 102 | + |
| 103 | + calculatePercent() { |
| 104 | + let numLines = this.percentLines * 0.2; |
| 105 | + let lines = this.answer.length; |
| 106 | + let numCorrectBlocks = (this.correctLines / lines) * 0.8; |
| 107 | + |
| 108 | + this.percent = numLines + numCorrectBlocks; |
| 109 | + } |
| 110 | +} |
0 commit comments