-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission.js
308 lines (249 loc) · 10.2 KB
/
submission.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
'use strict';
const { parseLine } = require('./utils');
const submission = ({ inputDataSet, submittedDataSet, outputToFile }) => {
function createError(message, lineNumber) {
return new Error(`Invalid submission: ${message} at line ${lineNumber}`);
}
function parseInputDataSet(data) {
const dataset = {
numContributors: 0,
numProjects: 0,
contributors: {},
projects: {},
numTimesContributorsGotMentored: 0,
numTimesContributorsIncreasedSkillLevel: 0,
numContributorsWorkedOnProjects: 0,
};
const lines = data.split('\n');
const [numContributors, numProjects] = parseLine(lines[0], [0, 1]);
let currentLine = 1;
dataset.numContributors = numContributors;
dataset.numProjects = numProjects;
for (let i = 0; i < numContributors; i++) {
const [name, numSkills] = parseLine(lines[currentLine++], [1]);
dataset.contributors[name] = {
availableFrom: 0, waitingDays: 0, skills: {},
};
for (let j = 0; j < numSkills; j++) {
const [skillName, level] = parseLine(lines[currentLine + j], [1]);
dataset.contributors[name].skills[skillName] = level;
}
currentLine += numSkills;
}
for (let i = 0; i < numProjects; i++) {
const [
name, numDays, score, bestBeforeDay, numRoles,
] = parseLine(lines[currentLine++], [1, 2, 3, 4]);
dataset.projects[name] = {
numDays, score, bestBeforeDay, numRoles, skills: [],
};
for (let j = 0; j < numRoles; j++) {
const [skillName, level] = parseLine(lines[currentLine + j], [1]);
dataset.projects[name].skills.push({ name: skillName, level });
}
currentLine += numRoles;
}
return dataset;
}
function parseSubmittedDataSet(dataset, submittedData) {
if (submittedData === '') {
throw new Error('File is empty');
}
const lines = submittedData.split('\n');
let currentLine = 0;
let line = lines[currentLine++].trim();
if (!line.length) {
throw new Error(`Invalid submission: Line ${currentLine} is empty`);
}
if (isNaN(line)) {
throw createError(`Can't parse integer value (${line})`, currentLine);
}
const numExecutedProjects = +line;
if (numExecutedProjects > dataset.numProjects) {
throw createError([
`The number of completed projects is ${numExecutedProjects}`,
`instead of being between 0 and ${dataset.numProjects}`,
].join(' '), currentLine);
}
for (let i = 0; i < numExecutedProjects; i++) {
line = lines[currentLine++];
if (typeof line === 'undefined') {
throw createError([
'Submission file has fewer lines than expected.',
'Unexpected EOF (end of file)',
].join(' '), currentLine);
}
const projectName = line.trim();
if (!projectName.length) {
throw new Error(`Invalid submission: Line ${currentLine} is empty`);
}
const project = dataset.projects[projectName];
if (!project) {
throw createError(`Project "${projectName}" does not exist`, currentLine);
}
if (typeof project.receivedScore !== 'undefined') {
throw createError(`Project ${projectName} is listed twice`, currentLine);
}
line = lines[currentLine++];
if (typeof line === 'undefined') {
throw createError([
'Submission file has fewer lines than expected.',
'Unexpected EOF (end of file)',
].join(' '), currentLine);
}
const contributorNames = parseLine(line);
const numContributors = contributorNames.length;
if (!numContributors) {
throw new Error(`Invalid submission: Line ${currentLine} is empty`);
}
if (numContributors !== project.numRoles) {
throw createError([
`The number of listed contributors for project ${projectName}`,
`is ${numContributors} instead of ${project.numRoles}`,
].join(' '), currentLine);
}
const duplicatedNames = contributorNames.filter((name, idx, array) =>
array.indexOf(name) !== idx
);
if (duplicatedNames.length) {
throw createError([
`Contributor ${duplicatedNames[0]} is listed for more than one role`,
`in project ${projectName}`,
].join(' '), currentLine);
}
const nonExistentContributor = contributorNames.find(name =>
!dataset.contributors[name]
);
if (nonExistentContributor) {
throw createError(
`Contributor "${nonExistentContributor}" does not exist`, currentLine
);
}
dataset.numContributorsWorkedOnProjects += numContributors;
const contributorsAreAvailableFrom = Math.max(...contributorNames.map(
name => dataset.contributors[name].availableFrom
));
const endedOn = contributorsAreAvailableFrom + project.numDays;
for (let j = 0; j < numContributors; j++) {
const name = contributorNames[j];
const contributor = dataset.contributors[name];
const { name: skill, level } = project.skills[j];
const mentored = (contributor.skills[skill] || 0) === level - 1 &&
contributorNames.some(contributorName =>
dataset.contributors[contributorName].skills[skill] >= level
);
if (contributor.skills[skill] >= level || mentored) {
if ((contributor.skills[skill] || 0) <= level) {
contributor.skills[skill] ??= 0;
contributor.skills[skill]++;
dataset.numTimesContributorsIncreasedSkillLevel++;
}
if (mentored) {
dataset.numTimesContributorsGotMentored++;
}
contributor.waitingDays += contributorsAreAvailableFrom - contributor.availableFrom;
contributor.availableFrom = endedOn;
continue;
}
const highestSkillLevel = Math.max(...contributorNames.map(
contributorName => dataset.contributors[contributorName].skills[skill] || 0
));
throw createError([
`Project ${projectName} cannot be completed because contributor`,
`${name} has the ${skill} skill at level ${contributor.skills[skill] || 0},`,
`while the ${skill} skill at level ${level} is required.`,
`The highest ${skill} skill level among contributors on this project is`,
`${highestSkillLevel}`,
].join(' '), currentLine);
}
const lateBy = Math.max(0, endedOn - project.bestBeforeDay);
project.receivedScore = Math.max(0, project.score - lateBy);
}
if (typeof lines[currentLine] !== 'undefined' && lines[currentLine].trim().length) {
throw createError(
`Expected end of file, got: ${lines[currentLine]}`, currentLine + 1
);
}
return dataset;
}
function createInsights(dataset) {
const noColor = +outputToFile === 1;
const yellow = value => noColor ? value : `\u001B[33m${value}\u001B[0m`;
const formatNumber = value => yellow(value.toLocaleString('en-US'));
const toPercentage = value => yellow(
`${(value * 100).toString().match(/^-?([\d][^.]*)/)[0]}%`
);
const {
numContributors, numProjects, contributors, projects,
numTimesContributorsGotMentored, numTimesContributorsIncreasedSkillLevel,
numContributorsWorkedOnProjects,
} = dataset;
const projectNames = Object.keys(projects);
const contributorNames = Object.keys(contributors);
const completedProjects = projectNames.filter(name =>
typeof projects[name].receivedScore !== 'undefined'
);
const numProjectsCompleted = completedProjects.length;
const {
score, numProjectsBestBeforeDay, numProjectsCompletedTooLong,
} = completedProjects.reduce((sum, name) => {
const project = projects[name];
sum.score += project.receivedScore;
sum.numProjectsBestBeforeDay += project.receivedScore === project.score;
sum.numProjectsCompletedTooLong += project.receivedScore === 0;
return sum;
}, { score: 0, numProjectsBestBeforeDay: 0, numProjectsCompletedTooLong: 0 });
const averageWaitingDays = (contributorNames.reduce(
(sum, name) => sum + contributors[name].waitingDays, 0
) / numContributorsWorkedOnProjects || 0).toFixed(2);
const numContributorsWorked = contributorNames.filter(name =>
contributors[name].availableFrom > 0
).length;
return {
score,
toString: () => [
`Score: ${formatNumber(score)}`,
'Insights about this submission:',
[
[
`- projects completed: ${formatNumber(numProjectsCompleted)}`,
`(${toPercentage(numProjectsCompleted / numProjects)} of all projects)`,
].join(' '),
[
'- projects completed before their "best before" time in days',
`(scoring full points): ${formatNumber(numProjectsBestBeforeDay)}`,
`(${toPercentage(numProjectsBestBeforeDay / numProjects)} of all projects)`,
].join(' '),
[
'- projects completed but scoring 0 points (because they were',
'completed too long after their "best before" time):',
formatNumber(numProjectsCompletedTooLong),
`(${toPercentage(numProjectsCompletedTooLong / numProjects)} of all projects)`,
].join(' '),
[
'- number of times a contributor got mentored on a project:',
formatNumber(numTimesContributorsGotMentored),
].join(' '),
[
'- number of times a contributor increased their skill level thanks to',
`completing a project: ${formatNumber(numTimesContributorsIncreasedSkillLevel)}`,
].join(' '),
[
'- contributors were waiting for their next project to start',
`${formatNumber(averageWaitingDays)} days on average`,
].join(' '),
[
`- ${formatNumber(numContributorsWorked)} contributors out of`,
`${formatNumber(numContributors)} worked on at least one project`,
].join(' '),
].join('\n'),
].join('\n\n'),
};
}
function evaluate() {
const dataset = parseInputDataSet(inputDataSet);
return createInsights(parseSubmittedDataSet(dataset, submittedDataSet));
}
return { evaluate };
};
module.exports = submission;