#C13714. Expression Formatter and Validator

    ID: 43283 Type: Default 1000ms 256MiB

Expression Formatter and Validator

Expression Formatter and Validator

You are given a string representing a mathematical expression that may include digits, operators (+,,,/,=+, -, *, /, =) and brackets. Your task is two‐fold:

  1. Format the expression by ensuring exactly one space before and after each operator and one space after every opening bracket and before every closing bracket. For example, convert 3+5*(2-9)/2 to 3 + 5 * ( 2 - 9 ) / 2.

  2. Validate the formatted expression. An expression is considered valid if it: ( \bullet ) contains only digits, the operators +,,,/,=+, -, *, /, = and brackets ( ( ) ( ) ), and spaces; ( \bullet ) has properly paired and nested brackets; ( \bullet ) does not contain consecutive operators.

Your program should read the expression from standard input, output the properly formatted expression on the first line, and on the second line output either True or False indicating whether the expression is valid.

inputFormat

A single line containing the mathematical expression. The expression may include extra spaces, digits, operators (+,,,/,=+, -, *, /, =) and brackets. Input is read from standard input (stdin).

outputFormat

The output consists of two lines:

  1. The first line is the formatted mathematical expression, where each operator and bracket is surrounded by a single space.
  2. The second line is either True or False indicating whether the formatted expression is valid. The output is written to standard output (stdout).## sample
3+5*(2-9)/2
3 + 5 * ( 2 - 9 ) / 2

True

</p>