Skip to main content
added 7 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses a non-recursive approach, based on the facts that

  • each operator must have a ( immediately before, and
  • the corresponding ) must be placed immediately after the first number for which the count of numbers minus operators to the right of the ( equals 2.

The procedure is as follows:

Repeat these steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if itthe group is an operator.
  5. When the counter reaches 2, insert ) right after that group.

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses a non-recursive approach, based on the facts that

  • each operator must have a ( immediately before, and
  • the corresponding ) must be placed immediately after the first number for which the count of numbers minus operators to the right of the ( equals 2.

The procedure is as follows:

Repeat these steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if it is an operator.
  5. When the counter reaches 2, insert ) right after that group.

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses a non-recursive approach, based on the facts that

  • each operator must have a ( immediately before, and
  • the corresponding ) must be placed immediately after the first number for which the count of numbers minus operators to the right of the ( equals 2.

The procedure is as follows:

Repeat these steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if the group is an operator.
  5. When the counter reaches 2, insert ) right after that group.
added 9 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses a non-recursive approach, based on the facts that (i) each operator must have a ( immediately before, and (ii) the corresponding ) must be placed wherever the cumulative count of numbers minus operators, starting at that position, equals 2.

  • each operator must have a ( immediately before, and
  • the corresponding ) must be placed immediately after the first number for which the count of numbers minus operators to the right of the ( equals 2.

The procedure is as follows:

Repeat these steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if it is an operator.
  5. When the counter reaches 2, insert ) right after that group.

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses a non-recursive approach, based on the facts that (i) each operator must have a ( immediately before, and (ii) the corresponding ) must be placed wherever the cumulative count of numbers minus operators, starting at that position, equals 2.

The procedure is as follows:

Repeat these steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if it is an operator.
  5. When the counter reaches 2, insert ) right after that group.

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses a non-recursive approach, based on the facts that

  • each operator must have a ( immediately before, and
  • the corresponding ) must be placed immediately after the first number for which the count of numbers minus operators to the right of the ( equals 2.

The procedure is as follows:

Repeat these steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if it is an operator.
  5. When the counter reaches 2, insert ) right after that group.
added 727 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses thisa non-recursive approach, based on the facts that (i) each operator must have a ( immediately before, and (ii) the corresponding ) must be placed wherever the cumulative count of numbers minus operators, starting at that position, equals 2.

The procedure is as follows:

Repeat the followingthese steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if it is an operator.
  5. When the counter reaches 2, insert ) right after that group.

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses this non-recursive approach:

Repeat the following as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if it is an operator.
  5. When the counter reaches 2, insert ) right after that group.

MATL, 93 92 90 89 86 85 84 bytes

t"t'(?<!\()[*-/]'1&XXX<t?:&)w0XH&)40hwb'\d+|[\(-/ ]'XX"@gt1)t47>Ew41>-H+XH2=?41XH]&h

Try it online! Or verify all test cases.

The code uses a non-recursive approach, based on the facts that (i) each operator must have a ( immediately before, and (ii) the corresponding ) must be placed wherever the cumulative count of numbers minus operators, starting at that position, equals 2.

The procedure is as follows:

Repeat these steps as many times as the input length (it would suffice to repeat as many times as the number of operators in the input string):

  1. Find the first operator (+-/*) that is not predeced by (, if any.
  2. Insert ( right before that.
  3. Take the substring after that position until the end and split it into groups, where each group is either a number (one or more digits), an operator, an opening or closing parenthesis, or whitespace (one or more spaces).
  4. Start a counter at 0. For each group from the previous step, increment the counter if the group is a number, and decrement it if it is an operator.
  5. When the counter reaches 2, insert ) right after that group.
added 727 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372
Loading
added 11 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372
Loading
added 3 characters in body
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372
Loading
Source Link
Luis Mendo
  • 105.3k
  • 9
  • 135
  • 372
Loading