/** * llmindex.io — code item templates (program-trace prediction, perturbed constants) * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * License: Proprietary — © Simon-Pierre Boucher, all rights reserved */ import { ANSWER_FORMAT_INSTRUCTIONS } from '../answer'; import type { ItemTemplate } from '../types'; export const codeTracePython: ItemTemplate = { id: 'code.trace.python-v1', domain: 'code', description: 'Predict the printed output of a short Python loop with perturbed constants.', paramSpace: 15 * 9 * 20 * 30, render(rng, perturbSeed) { const start = rng.int(1, 15); const step = rng.int(2, 9); const limit = rng.int(30, 120); const divisor = rng.int(3, 7); const program = [ `total = 0`, `v = ${start}`, `while total + v <= ${limit}:`, ` if v % ${divisor} != 0:`, ` total += v`, ` v += ${step}`, `print(total)`, ].join('\n'); // Recompute exactly as the program executes (single source of truth). let t = 0; let x = start; while (t + x <= limit) { if (x % divisor !== 0) t += x; x += step; } const leads = [ 'What does this Python program print?', 'Trace the following Python code and give its exact output.', 'Execute this Python snippet mentally. What is printed?', ]; return { templateId: this.id, domain: this.domain, prompt: `${rng.pick(leads)}\n\n\`\`\`python\n${program}\n\`\`\`\n\n${ANSWER_FORMAT_INSTRUCTIONS}`, answerKey: String(t), grading: 'numeric', perturbSeed, }; }, }; export const codeTraceNested: ItemTemplate = { id: 'code.trace.nested-v1', domain: 'code', description: 'Trace nested loops with break/continue and an accumulator over generated constants — state tracking under control-flow interruptions.', paramSpace: 9 ** 5 * 4 ** 3, render(rng, perturbSeed) { const outer = rng.int(4, 7); const inner = rng.int(4, 7); const skipMod = rng.int(2, 4); const breakAt = rng.int(3, Math.max(3, inner - 1)); const mult = rng.int(2, 5); // Simulate exactly the printed program. let total = 0; for (let i = 1; i <= outer; i++) { for (let j = 1; j <= inner; j++) { if (j === breakAt && i % 2 === 0) break; if ((i + j) % skipMod === 0) continue; total += i * mult + j; } } const program = [ `total = 0`, `for i in range(1, ${outer + 1}):`, ` for j in range(1, ${inner + 1}):`, ` if j == ${breakAt} and i % 2 == 0:`, ` break`, ` if (i + j) % ${skipMod} == 0:`, ` continue`, ` total += i * ${mult} + j`, `print(total)`, ].join('\n'); return { templateId: this.id, domain: this.domain, prompt: `Trace this Python program exactly. What does it print?\n\n\`\`\`python\n${program}\n\`\`\`\n\n${ANSWER_FORMAT_INSTRUCTIONS}`, answerKey: String(total), grading: 'numeric', perturbSeed, }; }, }; export const codeTraceJs: ItemTemplate = { id: 'code.trace.js-v1', domain: 'code', description: 'Predict the result of a JavaScript array pipeline with perturbed constants.', paramSpace: 8 * 12 * 10 * 6, render(rng, perturbSeed) { const len = rng.int(6, 12); const mult = rng.int(2, 7); const offset = rng.int(1, 10); const mod = rng.int(2, 5); const arr = Array.from({ length: len }, (_, i) => i + offset); const result = arr .map((n) => n * mult) .filter((n) => n % mod === 0) .reduce((a, b) => a + b, 0); const program = [ `const arr = [${arr.join(', ')}];`, `const out = arr`, ` .map(n => n * ${mult})`, ` .filter(n => n % ${mod} === 0)`, ` .reduce((a, b) => a + b, 0);`, `console.log(out);`, ].join('\n'); const leads = [ 'What does this JavaScript program log?', 'Evaluate the following JavaScript. What number is logged to the console?', ]; return { templateId: this.id, domain: this.domain, prompt: `${rng.pick(leads)}\n\n\`\`\`js\n${program}\n\`\`\`\n\n${ANSWER_FORMAT_INSTRUCTIONS}`, answerKey: String(result), grading: 'numeric', perturbSeed, }; }, };