Skip to main content
ThePythonBook/Assessment

File I/O & Data Formats Assessment

Test your skills with CSV parsing, JSON handling, data format conversion, and string-based file processing in Python.

Progress
830 XP0/10
#1Parse CSV String
Write Code

Write a function parse_csv(text: str) -> list[dict] that takes a CSV-formatted string (with a header row) and returns a list of dictionaries. Each dictionary maps column headers to row values.

Use the csv module with io.StringIO.

Loading editor...
#2Fix the JSON Parser
Fix the Bug

This function should parse a JSON string and return the resulting Python object. It has a bug -- find and fix it.

Loading editor...
#3Build JSON String
Write Code

Write a function to_json(data, pretty: bool = False) -> str that converts a Python object to a JSON string.

  • When pretty is False, return compact JSON (no extra spaces).
  • When pretty is True, return indented JSON with 2-space indent.
  • Loading editor...
    #4Predict the CSV Output
    Predict Output

    What does this code print? Enter the exact output.

    Loading editor...
    #5Fix the CSV Filter
    Fix the Bug

    This function should filter CSV rows where the score column is at least min_score and return the matching names. It has bugs -- find and fix them.

    Loading editor...
    #6Refactor the JSON Transformer
    Refactor

    Refactor this function to be more concise. It converts a JSON string containing a list of objects into a dictionary keyed by the id field. Keep the same behavior but use a dict comprehension.

    Loading editor...
    #7CSV to JSON Converter
    Write Code

    Write a function csv_to_json(csv_text: str) -> str that converts a CSV string (with header row) into a JSON string representing a list of objects.

    The JSON output should be compact (no extra whitespace). Use the default json.dumps separators.

    Loading editor...
    #8Predict the JSON Round-Trip
    Predict Output

    What does this code print? Enter the exact output.

    Loading editor...
    #9Fix the Path Builder
    Fix the Bug

    This function should use PurePosixPath to build a file path from parts and return it as a string. It has bugs -- find and fix them.

    Loading editor...
    #10Refactor the Format Detector
    Refactor

    Refactor this function. It detects whether a string is JSON or CSV and parses it accordingly. Simplify the JSON detection logic and remove redundant code while keeping the same behavior.

    A string is considered JSON if it starts with { or [ (after stripping whitespace).

    Loading editor...