Repository navigation
Expand file tree
/
Copy pathsession.ts
More file actions
198 lines (190 loc) · 6.53 KB
/
Copy pathsession.ts
File metadata and controls
198 lines (190 loc) · 6.53 KB
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
import { QUESTION_BANK } from "../data/questions.ts";
import { PRACTICE_TOPICS } from "./topics.ts";
import type {
Difficulty,
PracticeQuestion,
PracticeRecommendation,
PracticeResponse,
SessionSummary,
TopicId,
TopicSummary,
} from "./types.ts";
export interface SessionSelection {
topicId?: TopicId;
difficulty?: Difficulty;
count?: number;
random?: () => number;
}
/** Deterministic randomness for reproducible practice selections and tests. */
export function createSeededRandom(seed: number): () => number {
if (!Number.isInteger(seed)) throw new RangeError("seed must be an integer");
let state = seed >>> 0;
return () => {
state = (state + 0x6d2b79f5) | 0;
let value = Math.imul(state ^ (state >>> 15), 1 | state);
value ^= value + Math.imul(value ^ (value >>> 7), 61 | value);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
/** Filters and shuffles a copy; requesting more than the pool returns the whole pool. */
export function selectQuestions(
questions: readonly PracticeQuestion[],
selection: SessionSelection = {},
): PracticeQuestion[] {
const { topicId, difficulty, count = 6, random = Math.random } = selection;
if (!Number.isInteger(count) || count < 1)
throw new RangeError("count must be a positive integer");
const pool = questions.filter(
(question) =>
(!topicId || question.topicId === topicId) &&
(!difficulty || question.difficulty === difficulty),
);
for (let index = pool.length - 1; index > 0; index -= 1) {
const value = random();
if (!Number.isFinite(value) || value < 0 || value >= 1)
throw new RangeError("random must return a value in [0, 1)");
const target = Math.floor(value * (index + 1));
[pool[index], pool[target]] = [pool[target], pool[index]];
}
return pool.slice(0, count);
}
export function selectPracticeQuestions(
selection: SessionSelection = {},
): PracticeQuestion[] {
return selectQuestions(QUESTION_BANK, selection);
}
const percent = (numerator: number, denominator: number) =>
denominator ? Math.round((numerator * 100) / denominator) : 0;
/** Each question counts once. For duplicates, the latest valid response wins. */
export function gradeSession(
questions: readonly PracticeQuestion[],
responses: readonly PracticeResponse[],
): SessionSummary {
const questionById = new Map(
questions.map((question) => [question.id, question]),
);
if (questionById.size !== questions.length)
throw new Error("Session question IDs must be unique");
const chosen = new Map<string, string>();
for (const response of responses) {
const question = questionById.get(response.questionId);
if (question?.options.some((option) => option.id === response.optionId))
chosen.set(response.questionId, response.optionId);
}
const answers = questions.map((question) => {
const optionId = chosen.get(question.id);
return {
questionId: question.id,
topicId: question.topicId,
selectedOptionId:
question.options.find((option) => option.id === optionId)?.id ?? null,
correctOptionId: question.answerId,
isCorrect: optionId === question.answerId,
isAnswered: optionId !== undefined,
};
});
const answered = answers.filter((answer) => answer.isAnswered).length;
const correct = answers.filter((answer) => answer.isCorrect).length;
const topics = PRACTICE_TOPICS.map(({ id: topicId }) => {
const topicAnswers = answers.filter((answer) => answer.topicId === topicId);
const topicAnswered = topicAnswers.filter(
(answer) => answer.isAnswered,
).length;
const topicCorrect = topicAnswers.filter(
(answer) => answer.isCorrect,
).length;
return {
topicId,
totalQuestions: topicAnswers.length,
answered: topicAnswered,
correct: topicCorrect,
incorrect: topicAnswered - topicCorrect,
unanswered: topicAnswers.length - topicAnswered,
accuracyPercent: percent(topicCorrect, topicAnswered),
};
});
return {
totalQuestions: questions.length,
answered,
correct,
incorrect: answered - correct,
unanswered: questions.length - answered,
accuracyPercent: percent(correct, answered),
scorePercent: percent(correct, questions.length),
answers,
topics,
};
}
/** Combines topic totals from several completed sessions without averaging percentages. */
export function combineTopicSummaries(
summaries: readonly TopicSummary[],
): TopicSummary[] {
return PRACTICE_TOPICS.map(({ id: topicId }) => {
const matching = summaries.filter((summary) => summary.topicId === topicId);
const totalQuestions = matching.reduce(
(sum, summary) => sum + summary.totalQuestions,
0,
);
const answered = matching.reduce(
(sum, summary) => sum + summary.answered,
0,
);
const correct = matching.reduce((sum, summary) => sum + summary.correct, 0);
return {
topicId,
totalQuestions,
answered,
correct,
incorrect: answered - correct,
unanswered: totalQuestions - answered,
accuracyPercent: percent(correct, answered),
};
});
}
/** A transparent practice suggestion, never a placement-readiness prediction. */
export function recommendNextPractice(
summaries: readonly TopicSummary[],
): PracticeRecommendation {
const totals = combineTopicSummaries(summaries);
if (totals.every((topic) => topic.answered === 0)) {
return {
topicId: "quantitative",
reason: "start",
message:
"Start with a short quantitative aptitude session to see how the practice works.",
};
}
const untried = totals.find((topic) => topic.answered === 0);
if (untried) {
const title = PRACTICE_TOPICS.find(
(topic) => topic.id === untried.topicId,
)!.title.toLowerCase();
return {
topicId: untried.topicId,
reason: "coverage",
message: `Try ${title} next; you have not answered a question in this topic yet.`,
};
}
const weakest = [...totals].sort(
(left, right) =>
left.accuracyPercent - right.accuracyPercent ||
left.answered - right.answered,
)[0];
if (weakest.accuracyPercent < 80) {
return {
topicId: weakest.topicId,
reason: "review",
message:
"Practise this topic again and review the explanations; it has your lowest answer accuracy so far.",
};
}
const leastPractised = [...totals].sort(
(left, right) => left.answered - right.answered,
)[0];
return {
topicId: leastPractised.topicId,
reason: "continue",
message:
"Keep your practice balanced with the topic you have answered least often.",
};
}