-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraderV2.js
128 lines (116 loc) · 4.13 KB
/
graderV2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var fs = require('fs');
var debug = process.argv.some(value => value.includes('debug'));
fs.readFile('input.txt', 'utf8', function(err, data) {
if (err) throw err;
var readingStudents;
var readingAnswer = false;
var answers = [];
var students = new Map(); if (debug) {
console.log("Reading file.");
}
splitChar = '\n';
if (splitChar == '\r\n') {
console.log("ERROR SPLITTING BY WINDOWS LINE ENDINGS")
}
data.split(splitChar).forEach(line => {
if (debug) {
console.log(`Reading line: ${line}`);
}
if(line == '') {
return;
}
if (line.startsWith('Answer')) {
readingAnswer = true;
return;
}
if (line.startsWith('S1') && readingAnswer) {
readingAnswer = false;
readingStudents = true;
line.split(/\s+/).forEach((value, index) => {
students.set(index, '');
});
return;
}
if (readingAnswer) {
answer = line.split(/\s+/);
answers.push(answer);
return;
}
if (readingStudents) {
let row = line.split(/\s+/);
if (row.length != students.size) {
throw `Number of answers in the row does not match the number of students.`
}
line.split(/\s+/).forEach((value, index) => {
students.set(index, students.get(index) + value);
});
}
});
if (debug) {
console.log('Answers:');
console.log(answers);
console.log('Students:');
console.log(students);
}
students.forEach((value, key, map) => {
// var score = 0;
var subjects = new Map();
var questions = new Map();
var difficulties = new Map();
value.split('').forEach((answer, index) => {
var correctAnswer = answers[index][0];
var subjectKey = answers[index][1];
var questionKey = answers[index][2];
var difficultyKey = answers[index][3];
var correct = answer == correctAnswer ? 1 : 0;
// score += correct;
var subject = subjects.has(subjectKey) ? subjects.get(subjectKey) : { total : 0, correct : 0 };
subject.total++;
subject.correct += correct;
var question = questions.has(questionKey) ? questions.get(questionKey) : { total : 0, correct : 0 };
question.total++;
question.correct += correct;
var difficulty = difficulties.has(difficultyKey) ? difficulties.get(difficultyKey) : { total : 0, correct : 0 };
difficulty.total++;
difficulty.correct += correct;
subjects.set(subjectKey, subject);
questions.set(questionKey, question);
difficulties.set(difficultyKey, difficulty);
});
if (debug) {
console.log('subjects:');
console.log(subjects);
console.log('questions:');
console.log(questions);
console.log('difficulties:');
console.log(difficulties);
}
var contents = `Student ${key + 1}:\n`;
contents += `\n`;
subjects.forEach((value, key) => {
if (debug) {
contents += `${key} - `;
}
contents += `${value.correct}/${value.total}\n`;
})
contents += `\n\n`;
questions.forEach((value, key) => {
if (debug) {
contents += `${key} - `;
}
contents += `${value.correct}/${value.total}\n`;
})
contents += `\n\n`;
difficulties.forEach((value, key) => {
if (debug) {
contents += `${key} - `;
}
contents += `${value.correct}/${value.total}\n`;
})
contents += `\n`;
fs.appendFileSync('output1.txt', contents);
});
answers.forEach((value, number, array) => {
fs.appendFileSync('output1.txt', `${Array.from(students.values()).map(v => v.charAt(number)).filter(v => v == value[0]).length}/${students.size}\n`);
});
});