← 返回 nvidia 的题目列表Integrate a Public GET API and Transform Results
类型:online_judge
Problem: Call a Public GET API and Transform the Result
Given a public HTTP GET endpoint:
GET https://example.com/api/items?userId={userId}
It returns a JSON array, each element like:
{
"id": 123,
"type": "A",
"score": 10,
"timestamp": "2024-01-02T03:04:05Z"
}
Implement a program that:
Reads an integer userId from stdin.
Calls the API to fetch the data (assume it is available).
Transforms the items:
Group by type
For each type, output count and scoreSum
Output a JSON object sorted by type in lexicographical order.
Input (stdin)
One line integer: userId
Output (stdout)
Print a JSON object whose keys are type and values like:
{
"A": {"count": 2, "scoreSum": 30},
"B": {"count": 1, "scoreSum": 7}
}
Constraints / Notes
0 <= items.length <= 1e5
score fits in signed 32-bit integer
Only handle HTTP 200 success path
Basic error handling: if network/parse fails, print {}
Example
(Input/output format example; real output depends on API response.)
Input: 42
Output: {"A":{"count":2,"scoreSum":30},"B":{"count":1,"scoreSum":7}}
Example
Input
42
Output
{}