File I/O & Data Formats Assessment
Test your skills with CSV parsing, JSON handling, data format conversion, and string-based file processing in Python.
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.
This function should parse a JSON string and return the resulting Python object. It has a bug -- find and fix it.
Write a function to_json(data, pretty: bool = False) -> str that converts a Python object to a JSON string.
pretty is False, return compact JSON (no extra spaces).pretty is True, return indented JSON with 2-space indent.What does this code print? Enter the exact output.
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.
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.
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.
What does this code print? Enter the exact output.
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.
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).