리펙토링: 코드 최적화
This commit is contained in:
@@ -25,27 +25,26 @@ function findCorrectChoiceIds(problem: VariableParenthesisProblem) {
|
||||
describe("variable parenthesis engine", () => {
|
||||
it("generates a problem with exactly one variable term inside parentheses", () => {
|
||||
const problem = generateVariableParenthesisProblem();
|
||||
const terms = [problem.firstTerm, problem.secondTerm];
|
||||
|
||||
expect(terms.filter((term) => term.kind === "variable")).toHaveLength(1);
|
||||
expect(terms.filter((term) => term.kind === "constant")).toHaveLength(1);
|
||||
expect(problem.terms.filter((term) => term.kind === "variable")).toHaveLength(1);
|
||||
expect(problem.terms.filter((term) => term.kind === "constant")).toHaveLength(1);
|
||||
expect(problem.multiplier).toBeLessThan(0);
|
||||
expect(Number.isInteger(problem.multiplier)).toBe(true);
|
||||
expect(problem.firstTerm.coefficient).toBeGreaterThanOrEqual(1);
|
||||
expect(problem.secondTerm.coefficient).toBeGreaterThanOrEqual(1);
|
||||
expect(
|
||||
problem.terms.every((term) => term.coefficient >= 1 && term.coefficient <= 9),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it("distributes signs correctly for addition and subtraction", () => {
|
||||
const problems = generateVariableParenthesisSession(50);
|
||||
|
||||
for (const problem of problems) {
|
||||
const secondSign = problem.operator === "-" ? -1 : 1;
|
||||
expect(problem.correctTerms.map((term) => term.coefficient)).toEqual(
|
||||
problem.terms.map((term, index) => {
|
||||
const sign = index === 0 || problem.operators[index - 1] === "+" ? 1 : -1;
|
||||
|
||||
expect(problem.correctTerms[0].coefficient).toBe(
|
||||
problem.multiplier * problem.firstTerm.coefficient,
|
||||
);
|
||||
expect(problem.correctTerms[1].coefficient).toBe(
|
||||
problem.multiplier * secondSign * problem.secondTerm.coefficient,
|
||||
return problem.multiplier * sign * term.coefficient;
|
||||
}),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user