At AcmeCLI, you need to format an API response into a deterministic, aligned text table.
Implement format_api_output(response_json, query, sort_by, limit).
response_json: str — JSON string expected to be an array of objects.query: str — case-insensitive substring filter applied to name.sort_by: str — either 'name' or 'score'.limit: int — maximum number of output rows.id and name are strings.score handling:
null, treat as 0.int (but not bool); otherwise ignore the record.sort_by == 'name': sort by name.lower() ascending, then id ascending.sort_by == 'score': sort by score descending, then name.lower() ascending, then id ascending.ID NAME SCORE.ID and NAME are left-aligned; SCORE is right-aligned.response_json='[{"id":"1","name":"A","score":"9"},{"id":"2","name":"A","score":9}]', query='', sort_by='score', limit=10
Output:
ID NAME SCORE 2 A 9
Explanation: the first record is ignored because score is a string.
response_json='[{"id":"x","name":"Echo"},{"id":"y","name":"echelon","score":0},{"id":"z","name":"Other","score":5}]', query='ech', sort_by='name', limit=10
Output:
ID NAME SCORE x Echo 0 y echelon 0
Explanation: filter keeps Echo/echelon; sort is case-insensitive by name.
0 <= len(response_json) <= 2 * 10^60 <= n <= 2 * 10^5 (records in parsed array)0 <= limit <= nsort_by in {'name','score'}response_json = '[{"id":"p1","name":"Alpha","score":10},{"id":"p2","name":"beta","score":7}]', query = 'a', sort_by = 'score', limit = 10OutputID NAME SCORE
p1 Alpha 10
p2 beta 7WhyBoth names contain 'a' (case-insensitive). Sorted by score descending; SCORE is right-aligned.response_json = '[{"id":"x","name":"Echo"},{"id":"y","name":"echelon","score":0},{"id":"z","name":"Other","score":5}]', query = 'ech', sort_by = 'name', limit = 10OutputID NAME SCORE
x Echo 0
y echelon 0WhyFilter keeps Echo and echelon. Sort by name case-insensitively; widths are computed from emitted rows.0 <= len(response_json) <= 2 * 10^60 <= n <= 2 * 10^5 (number of records in the parsed array)0 <= limit <= nsort_by is either 'name' or 'score'Records missing 'id' or 'name' are ignoredIf 'score' is present and not an int (or is bool), the record is ignored; missing/null score is treated as 0def format_api_output(response_json: str, query: str, sort_by: str, limit: int) -> str: