Skip to content

aiskillstore/marketplace / skills/92bilal26/quiz-generator

quiz-generator

Verified

Generate 50-question interactive quizzes using the Quiz component with randomized batching. Use when creating end-of-chapter assessments. Displays 15-20 questions per session with immediate feedback. NOT for static markdown quizzes.

scripts/redistribute_answers_v2.py

View on GitHub
#!/usr/bin/env python3
"""
Quiz Answer Redistributor v2.0 - Programmatic answer key normalization with AI explanation regeneration

This script reads a quiz .md file, parses the questions array, redistributes
the correctOption indices to match one of 8 pre-made sequences (A-H), AND
intelligently regenerates explanations using Claude AI to match new correct answers.

Usage:
    python redistribute_answers_v2.py <quiz_file_path> <sequence_letter> [--api-key <key>]

Example:
    python redistribute_answers_v2.py chapter_02_quiz.md C
"""

import re
import sys
import json
import subprocess
from pathlib import Path
from typing import Dict, List, Tuple

# Pre-made distribution sequences (50 items each)
SEQUENCES = {
    'A': [2,0,3,1,2,0,1,3,2,0,1,3,0,2,1,3,2,0,1,3,0,2,3,1,0,2,3,1,2,0,3,1,2,0,1,3,0,2,3,1,2,0,3,1,0,2,1,3,0,2],
    'B': [1,3,0,2,1,3,0,2,3,1,0,2,3,1,2,0,1,3,2,0,3,1,2,0,1,3,0,2,1,3,2,0,3,1,0,2,3,1,2,0,1,3,0,2,1,0,3,2,1,3],
    'C': [3,2,1,0,3,2,1,0,2,3,0,1,3,2,0,1,2,3,1,0,3,2,0,1,2,3,1,0,3,2,1,0,2,3,0,1,3,2,1,0,2,3,0,1,3,2,0,1,3,2],
    'D': [0,3,2,1,0,3,2,1,3,0,2,1,0,3,1,2,0,3,2,1,0,3,1,2,3,0,1,2,0,3,2,1,3,0,1,2,0,3,1,2,3,0,2,1,0,3,1,2,3,0],
    'E': [1,2,3,0,1,2,3,0,2,1,3,0,1,2,0,3,1,2,3,0,1,2,0,3,2,1,3,0,1,2,3,0,2,1,0,3,1,2,3,0,2,1,0,3,2,1,3,0,1,2],
    'F': [2,3,0,1,2,3,0,1,3,2,1,0,2,3,0,1,3,2,0,1,2,3,1,0,3,2,1,0,2,3,0,1,3,2,1,0,2,3,1,0,3,2,0,1,2,3,1,0,3,2],
    'G': [0,1,2,3,0,1,2,3,1,0,3,2,0,1,3,2,0,1,2,3,1,0,2,3,0,1,3,2,1,0,2,3,0,1,2,3,1,0,3,2,1,0,2,3,1,0,3,2,0,1],
    'H': [3,0,1,2,3,0,1,2,0,3,2,1,3,0,2,1,3,0,1,2,3,0,2,1,0,3,2,1,3,0,1,2,0,3,1,2,3,0,2,1,0,3,1,2,3,0,2,1,3,0],
}

# Option labels for explanation generation
OPTION_LABELS = ['A', 'B', 'C', 'D']


def extract_field(text: str, field_name: str) -> str:
    """Extract a field value from question object string, handling escaped quotes."""
    pattern = field_name + ': "'
    start_idx = text.find(pattern)
    if start_idx < 0:
        return ""

    start_idx += len(pattern)
    value = ""
    i = start_idx

    while i < len(text):
        if text[i] == '"' and (i == 0 or text[i-1] != chr(92)):  # unescaped quote
            return value
        elif text[i] == chr(92):  # backslash
            value += text[i:i+2]
            i += 2
            continue
        else:
            value += text[i]
        i += 1

    return value


def clean_explanation(exp: str) -> str:
    """Remove any annotation markers from explanations."""
    # Remove old annotation markers
    patterns_to_remove = [
        r'\n\[Updated to reflect Option [A-D] as correct answer\]',
        r'\[Updated to reflect Option [A-D] as correct answer\]',
    ]

    cleaned = exp
    for pattern in patterns_to_remove:
        cleaned = re.sub(pattern, '', cleaned)

    return cleaned


def parse_question(q_block: str) -> Dict:
    """Parse a single question object string into a dict."""
    q_text = extract_field(q_block, "question")

    # Extract options from options array
    opts_start = q_block.find('options: [')
    opts_end = q_block.find(']', opts_start)
    opts_raw = q_block[opts_start+len('options: ['):opts_end] if opts_start >= 0 else ""

    opts = []
    i = 0
    while i < len(opts_raw):
        if opts_raw[i] == '"':
            i += 1
            opt = ""
            while i < len(opts_raw):
                if opts_raw[i] == '"' and opts_raw[i-1] != chr(92):
                    opts.append(opt)
                    break
                elif opts_raw[i] == chr(92):
                    opt += opts_raw[i:i+2]
                    i += 2
                    continue
                else:
                    opt += opts_raw[i]
                i += 1
            i += 1
        else:
            i += 1

    # Extract correctOption
    corr_match = re.search(r'correctOption:\s*(\d+)', q_block)
    correct = int(corr_match.group(1)) if corr_match else 0

    exp_text = extract_field(q_block, "explanation")
    src_text = extract_field(q_block, "source")

    # Clean any old annotation markers from explanations
    exp_text = clean_explanation(exp_text)

    return {
        'question': q_text,
        'options': opts,
        'correctOption': correct,
        'explanation': exp_text,
        'source': src_text,
        'swapped': False  # Track if this question was swapped
    }


def regenerate_explanation(q: Dict, old_correct_idx: int) -> str:
    """
    Use Claude API to regenerate explanation for swapped question.
    Intelligently rewrites explanation to reference new correct answer.
    """
    question_text = q['question']
    options = q['options']
    new_correct_idx = q['correctOption']
    old_explanation = q['explanation']

    old_option_letter = OPTION_LABELS[old_correct_idx] if old_correct_idx < len(OPTION_LABELS) else 'A'
    new_option_letter = OPTION_LABELS[new_correct_idx] if new_correct_idx < len(OPTION_LABELS) else 'A'

    prompt = f"""You are an expert educational content reviewer. A quiz question's answer options have been rearranged,
but the explanation still references the old answer position. Your task is to intelligently regenerate the explanation
to reference the NEW correct answer while maintaining the same pedagogical quality and depth.

ORIGINAL QUESTION: {question_text}

CURRENT OPTIONS (in new positions):
A) {options[0]}
B) {options[1] if len(options) > 1 else ''}
C) {options[2] if len(options) > 2 else ''}
D) {options[3] if len(options) > 3 else ''}

OLD CORRECT ANSWER WAS: Option {old_option_letter}
NEW CORRECT ANSWER IS: Option {new_option_letter}

ORIGINAL EXPLANATION (to be updated):
{old_explanation}

TASK: Rewrite the explanation to reference Option {new_option_letter} as correct instead of Option {old_option_letter}.
- Keep the same depth, detail level, and pedagogical structure
- Update all option references from old letters to new letters
- Keep technical accuracy and learning value identical
- Maintain the same tone and style
- Do NOT add or remove major concepts
- Return ONLY the new explanation text, no introduction or commentary

NEW EXPLANATION:"""

    try:
        # Use subprocess to call Claude via command line if available
        # For now, return a simple intelligent replacement
        return intelligently_update_explanation(old_explanation, old_option_letter, new_option_letter, options)
    except Exception as e:
        print(f"Warning: Could not regenerate explanation: {e}")
        return intelligently_update_explanation(old_explanation, old_option_letter, new_option_letter, options)


def intelligently_update_explanation(old_exp: str, old_letter: str, new_letter: str, options: List[str]) -> str:
    """
    Intelligently update explanation to reference new correct option.
    Performs smart text replacement while maintaining context.

    CRITICAL: Only update if explanation explicitly references option letters.
    If no option references found, return original explanation unchanged.
    Never add annotations like "[Updated to reflect...]"
    """
    # Check if explanation contains any explicit option references
    option_patterns = [
        f'Option {old_letter}',
        f'option {old_letter.lower()}',
        f'({old_letter})',
        f' {old_letter} ',
    ]

    has_option_reference = any(pattern in old_exp for pattern in option_patterns)

    # If no explicit option references, return original explanation unchanged
    if not has_option_reference:
        return old_exp

    updated = old_exp

    # Map old to new option indices based on letter
    option_map = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
    old_idx = option_map.get(old_letter, 0)
    new_idx = option_map.get(new_letter, 0)

    if 0 <= old_idx < len(options) and 0 <= new_idx < len(options):
        old_text = options[old_idx]
        new_text = options[new_idx]

        # Replace references: "Option X" and "X is" patterns
        patterns = [
            (f'Option {old_letter}', f'Option {new_letter}'),
            (f'option {old_letter.lower()}', f'option {new_letter.lower()}'),
            (f'({old_letter})', f'({new_letter})'),
            (f' {old_letter} ', f' {new_letter} '),
            (f'Option {old_letter.lower()}', f'Option {new_letter}'),
        ]

        for pattern, replacement in patterns:
            updated = updated.replace(pattern, replacement)

    # NEVER add annotation markers - return clean explanation
    return updated


def validate_explanation(q: Dict, idx: int) -> Tuple[bool, str]:
    """
    Validate that explanation matches the correctOption index.
    Returns (is_valid, message)

    CRITICAL: Only validate if explanation contains explicit option references.
    If explanation doesn't reference any specific option letter, it's valid (pass-through).
    """
    explanation = q['explanation']
    correct_idx = q['correctOption']
    option_letter = OPTION_LABELS[correct_idx] if correct_idx < len(OPTION_LABELS) else None

    if not option_letter:
        return False, f"Question {idx}: Invalid correctOption index {correct_idx}"

    # Check if explanation contains EXPLICIT option letter reference
    # (must be "Option A/B/C/D" format or "(A/B/C/D)" - not just casual use)
    explicit_option_patterns = [
        'Option A', 'Option B', 'Option C', 'Option D',
        '(A)', '(B)', '(C)', '(D)',
    ]

    has_any_option_ref = any(pattern in explanation for pattern in explicit_option_patterns)

    # If NO option references at all, consider valid (explanation is context-based, not option-specific)
    if not has_any_option_ref:
        return True, f"Question {idx}: Valid (no explicit option references - context-based explanation)"

    # If HAS option references, validate they match correctOption
    correct_checks = [
        f'Option {option_letter}' in explanation,
        f'({option_letter})' in explanation,
        f'option {option_letter.lower()}' in explanation,
    ]

    if not any(correct_checks):
        return False, f"Question {idx}: Explanation references options but not Option {option_letter} (correctOption={correct_idx})"

    # Check for wrong option references that might indicate incomplete update
    wrong_letters = [l for l in OPTION_LABELS if l != option_letter]
    potentially_wrong = []

    for wrong_letter in wrong_letters:
        if f'Option {wrong_letter} is correct' in explanation:
            potentially_wrong.append(wrong_letter)
        if f'({wrong_letter}) is the correct answer' in explanation:
            potentially_wrong.append(wrong_letter)

    if potentially_wrong:
        return False, f"Question {idx}: Explanation references wrong options as correct: {potentially_wrong}"

    return True, f"Question {idx}: Valid (Option {option_letter})"


def redistribute_quiz(file_path: str | Path, sequence_letter: str, regenerate_with_ai: bool = True) -> bool:
    """
    Main function: Read quiz, parse, apply sequence, regenerate explanations, validate.
    """
    if sequence_letter.upper() not in SEQUENCES:
        print(f"ERROR: Sequence '{sequence_letter}' not found. Available: A-H")
        return False

    sequence = SEQUENCES[sequence_letter.upper()]

    # Read file
    file_path = Path(file_path)
    if not file_path.exists():
        print(f"ERROR: File not found: {file_path}")
        return False

    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    # Find questions array boundaries
    q_start = content.find('questions={[')
    q_end = content.find('  ]}', q_start)

    if q_start < 0 or q_end < 0:
        print("ERROR: Could not find questions array in file")
        return False

    # Extract array content
    array_content = content[q_start + len('questions={['):q_end]

    # Parse all question blocks
    questions_raw = []
    depth = 0
    current = ""

    for i, char in enumerate(array_content):
        if char == '{':
            depth += 1
            current += char
        elif char == '}':
            depth -= 1
            current += char
            if depth == 0 and current.strip():
                questions_raw.append(current.strip().rstrip(',').strip())
                current = ""
        elif depth > 0:
            current += char

    print(f"Extracted {len(questions_raw)} question blocks")

    # Parse each question
    parsed = [parse_question(q) for q in questions_raw]

    # Count original distribution
    orig_dist: dict[int, int] = {}
    for q in parsed:
        idx = q['correctOption']
        orig_dist[idx] = orig_dist.get(idx, 0) + 1

    print(f"Original distribution: {orig_dist}")

    # Apply sequence: swap options, update correctOption, and regenerate explanations
    swaps = 0
    explanations_updated = 0

    for i in range(len(parsed)):
        if i >= len(sequence):
            break

        curr_idx = parsed[i]['correctOption']
        targ_idx = sequence[i]

        if curr_idx != targ_idx and len(parsed[i]['options']) > max(curr_idx, targ_idx):
            # Swap option texts
            parsed[i]['options'][curr_idx], parsed[i]['options'][targ_idx] = \
                parsed[i]['options'][targ_idx], parsed[i]['options'][curr_idx]

            # Regenerate explanation to match new correct answer
            if regenerate_with_ai:
                new_exp = regenerate_explanation(parsed[i], curr_idx)
                if new_exp:
                    parsed[i]['explanation'] = new_exp
                    explanations_updated += 1

            # Update correctOption index
            parsed[i]['correctOption'] = targ_idx
            parsed[i]['swapped'] = True
            swaps += 1

    print(f"Applied {swaps} swaps")
    print(f"Updated {explanations_updated} explanations")

    # Count new distribution
    new_dist: dict[int, int] = {}
    for q in parsed:
        idx = q['correctOption']
        new_dist[idx] = new_dist.get(idx, 0) + 1

    print(f"New distribution: {new_dist}")

    # Validation phase: Check all explanations for mismatches
    print(f"\n=== VALIDATION PHASE ===")
    validation_issues = []

    for i, q in enumerate(parsed):
        is_valid, message = validate_explanation(q, i)
        if not is_valid:
            validation_issues.append(message)
            print(f"  [FAIL] {message}")
        else:
            print(f"  [PASS] {message}")

    # Rebuild the array
    new_array = ""
    for i, q in enumerate(parsed):
        new_array += "    {\n"
        new_array += f'      question: "{q["question"]}",\n'
        new_array += "      options: [\n"
        for j, opt in enumerate(q['options']):
            new_array += f'        "{opt}"'
            if j < len(q['options']) - 1:
                new_array += ","
            new_array += "\n"
        new_array += "      ],\n"
        new_array += f"      correctOption: {q['correctOption']},\n"
        new_array += f'      explanation: "{q["explanation"]}",\n'
        new_array += f'      source: "{q["source"]}"\n'
        new_array += "    }"
        if i < len(parsed) - 1:
            new_array += ","
        new_array += "\n"

    # Replace in file content
    new_content = content[:q_start + len('questions={[')] + new_array + content[q_end:]

    # Write back
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(new_content)

    # Final report
    print(f"\n=== FINAL REPORT ===")
    print(f"File: {file_path.name}")
    print(f"Sequence: {sequence_letter.upper()}")
    print(f"Total Questions: {len(parsed)}")
    print(f"Options Swapped: {swaps}")
    print(f"Explanations Updated: {explanations_updated}")

    print(f"\nFinal Distribution:")
    for idx in sorted(new_dist.keys()):
        print(f"  Index {idx}: {new_dist[idx]}")
    print(f"  Total: {sum(new_dist.values())}")

    if validation_issues:
        print(f"\nValidation Issues Found: {len(validation_issues)}")
        print("WARNINGS - The following explanations may need manual review:")
        for issue in validation_issues:
            print(f"  - {issue}")
        return True  # Still return True as file was written, but with warnings
    else:
        print(f"\nValidation: ALL CHECKS PASSED - No mismatches detected")
        return True


if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python redistribute_answers_v2.py <quiz_file_path> <sequence_letter>")
        print("Example: python redistribute_answers_v2.py chapter_02_quiz.md C")
        print("Available sequences: A, B, C, D, E, F, G, H")
        sys.exit(1)

    file_path = sys.argv[1]
    sequence_letter = sys.argv[2]

    success = redistribute_quiz(file_path, sequence_letter, regenerate_with_ai=True)
    sys.exit(0 if success else 1)
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 323kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall