Skip to main content
ZhimaYuandi

F04 · PREDICT · DEBUG · VERIFY LOCALLY

F04 Validate Inputs: Why Does Another Input Break It?

A blank title, missing tags or normalized duplicate causes an error, yet leaves a partial record in the collection. An error does not mean there were no side effects.

Back to the practice route →

The bug and the local contract

def add_document(documents, candidate):
    documents.append(candidate)
    validate_document(candidate)
    return candidate
  • Implement validate_document, add_document and update_document under the shared types and ID rules: 500 records, 120 title code points, 40 category code points and up to 12 tags of 32 code points each.
  • Deduplicate titles with strip().casefold(). Updates preserve ID, allow title, category and tags only, and exclude self from duplicate checks. Valid renames succeed; collisions, invalid fields and unknown IDs fail explicitly.
  • Validate an independent candidate and commit only after every check passes. Failures preserve the complete collection. The page uses Python-generated normalization fixtures rather than claiming arbitrary Unicode support.

Use fictional document metadata. The web model does not execute arbitrary code.

01 · CONTROLLED MODEL

Predict, then inspect each step

This models only the selected preset. It does not execute your Python draft or read/write computer files. One matching case does not establish correctness for all inputs.

Success must produce the complete candidate. Failure must report an error and preserve every field. Normalization values were generated by Python; free input is not casefolded here.

Unchecked or invalidated

    Before

    {
      "records": [
        {
          "id": "doc_00000000000000000000000000000001",
          "title": "Straße",
          "category": "course",
          "tags": [
            "study"
          ]
        },
        {
          "id": "doc_00000000000000000000000000000002",
          "title": "Notes",
          "category": "course",
          "tags": [
            "study"
          ]
        }
      ],
      "operation": "add",
      "input": {
        "id": "doc_00000000000000000000000000000003",
        "title": "New Notes",
        "category": "course",
        "tags": [
          "study"
        ]
      }
    }

    Model result

    Step through the trace to reveal the result

    Progressive learning hints

    Hints are fixed teaching content. Their use is recorded.

    Hint 1

    Write the expected collection after failure. Is a failure result enough? Compare every field before and after.

    Hint 2

    Check types, lengths and normalized titles on a candidate before committing. Exclude the current ID during update duplicate checks.

    Hint 3

    Cover blanks, duplicates, length boundaries, wrong types and colliding renames. Failures must leave no new records or overwritten fields. toLowerCase() is not casefold().

    My Python draft

    Save and export your draft here. Code is not executed or synced to the starter. Draft, input or strategy edits invalidate earlier model results and self-reports.

    Local learning records

    Saved only in this browser; export is available. No cross-device sync or proof of mastery. Earlier F01 and six-task records are preserved through migration.

    • F04 opened: —
    • Model check: Unchecked or invalidated
    • Local tests (self-reported): —
    • Highest hint level: 0/3
    • Reference viewed: —
    • Unaided variant (self-reported): —

    02 · Repair your local project

    Start any task independently: its package contains that task’s bug and a runnable baseline for the other business functions. Python 3.11+, standard library only. See the package’s verification notes for tested environments.

    Download this task’s starter

    1. Extract into a new directory. Open a terminal and identify the file being run. Run public tests to reproduce the bug; consult the package’s intentional-failure list.
    2. Write a hypothesis and choose a value to inspect. Repair the specified function, retain public tests and record the diff and a minimal counterexample.
    3. Run this task’s tests, then the full regression suite. Web-model results do not replace local execution; self-reports are not remotely verified by this site.
    python check_environment.py
    python -m unittest tests.test_f04 -v
    python -m unittest discover -s tests -v

    03 · Independent variant

    Implement register_attendee(attendees, name) in challenges.py: require a nonblank string of at most 50 code points and reject duplicates after strip().casefold(). Explain at least two rules. Write a test that defeats append-before-validation, then implement addition. Explain errors, preserve input and add your own boundary counterexample.

    python -m unittest tests.test_variants -v

    Implement from the requirement, replace the skipped placeholder in tests/test_student_counterexamples.py and add an assertion that exposes the original bug. Explain the failure. Hint or reference use excludes an unaided completion record for this attempt.

    Reference explanation and answer

    Try your own repair first. Opening or downloading the reference records answer use. Passing reference tests establishes only that the materials run.

    View reference explanation (records answer use)

    Build a candidate, validate every rule and commit once to avoid partial records. Normalization defines this project’s rules: Straße and STRASSE share a casefold result, not a universal reason to merge real documents.

    Download this task’s reference (answer) · Version and source digest

    Course references and review