#C5976. Extract Element from Nested List

    ID: 49684 Type: Default 1000ms 256MiB

Extract Element from Nested List

Extract Element from Nested List

You are given a query string and a nested list in string format. The query is a dot separated list of indices (e.g. 1.0 means the 0th element of the 1st sublist). Your task is to extract the element from the nested list corresponding to the index path.

If at any step the index is out of range or the current element is not a list when an index is expected, output \(\texttt{no such element}\). If the retrieved element is itself a list, output \(\texttt{list}\). Otherwise, output the element (an integer).

Note: The nested list is provided in JSON-like format. The query indices satisfy the formula \(q = i_0\.i_1\.\dots\.i_k\), where each \(i_j\) is an integer.

inputFormat

The input is given via stdin as two lines:

  • The first line contains the query string, which is a dot separated list of indices (for example, 1.0).
  • The second line contains the nested list in JSON format (for example, [[1, 2, 3], [4, [5, 6]], [7, 8, 9]]).

outputFormat

Output the extracted element to stdout:

  • If the extraction is successful and the element is not a list, print the element (e.g., 4).
  • If the element is a list, print list.
  • If no such element exists, print no such element.
## sample
1.0
[[1, 2, 3], [4, [5, 6]], [7, 8, 9]]
4