#C14193. Categorize Items by Data Type
Categorize Items by Data Type
Categorize Items by Data Type
Given a list of items, each item can be of type integer, float, or string. Your task is to categorize these items into three groups based on their datatype. The program should output a JSON formatted dictionary with three keys: int (for integers), float (for floating‐point numbers), and str (for strings).
Note: The input is read from standard input (stdin) and the output must be printed to standard output (stdout) in JSON format. The classification is done as follows:
- If an item does not contain a decimal point and can be parsed as an integer, it should be considered an integer.
- If an item contains a decimal point and can be parsed as a float, it should be categorized as a float.
- Otherwise, it is treated as a string.
Make sure your solution handles empty lists and lists containing only one type of item.
inputFormat
The first line of input contains an integer n representing the number of items. The second line contains n tokens separated by spaces. Each token represents an item which can be an integer, a float (if it contains a decimal point), or a string.
outputFormat
Output a single line JSON string representing an object with three keys: int, float, and str. The corresponding values are lists containing items of the respective type. The order of items should be preserved.
For example, given the input:
6 1 hello 3.14 42 world 2.71
The output should be:
{"int": [1, 42], "float": [3.14, 2.71], "str": ["hello", "world"]}## sample
6
1 hello 3.14 42 world 2.71
{"int": [1, 42], "float": [3.14, 2.71], "str": ["hello", "world"]}