#C11903. Multi-Function Challenge
Multi-Function Challenge
Multi-Function Challenge
You are given a program that must support several functions. Your program will read multiple queries from standard input. Each query specifies a task to perform. The functions to implement are:
- TWO_SUM: Given a list of integers and a target value, find and output the indices of the two numbers that add up to the target. It is guaranteed that exactly one solution exists.
- PALINDROME: Given a string, check if it is a palindrome. Consider only alphanumeric characters and ignore cases.
- INORDER: Given a binary tree represented in level-order (with "null" representing missing nodes), output the inorder traversal of its nodes.
- SPIRAL: Given an integer n, generate an n x n spiral matrix filled with the integers from 1 to n^2 in spiral order.
The input begins with an integer Q representing the number of queries. Each query starts with a command indicating which function to execute. The commands and their input formats are described below.
inputFormat
The first line of input contains an integer Q (Q ≥ 1) – the number of queries. Each query is given in the following format depending on the type:
- TWO_SUM:
- A line with the string
TWO_SUM
. - A line with an integer n — the number of elements in the array.
- A line with n space-separated integers.
- A line with an integer target.
- A line with the string
- PALINDROME:
- A line with the string
PALINDROME
. - A line with the string to check.
- A line with the string
- INORDER:
- A line with the string
INORDER
. - A line with an integer m – the number of tokens in the level-order representation of the tree.
- A line with m space-separated tokens. Each token is either an integer or the string
null
(without quotes) representing a missing node.
- A line with the string
- SPIRAL:
- A line with the string
SPIRAL
. - A line with a positive integer n, the dimension of the matrix.
- A line with the string
outputFormat
For each query, output the result on standard output as described below:
- TWO_SUM: Output two space-separated integer indices.
- PALINDROME: Output either
True
orFalse
. - INORDER: Output the inorder traversal of the tree as space-separated integers on one line. If the tree is empty, output an empty line.
- SPIRAL: Output the spiral matrix: print n lines, each containing n space-separated integers.
The outputs for different queries should appear in the order of the queries.
## sample4
TWO_SUM
4
2 7 11 15
9
PALINDROME
A man, a plan, a canal: Panama
INORDER
4
1 null 2 3
SPIRAL
3
0 1
True
1 3 2
1 2 3
8 9 4
7 6 5
</p>