11
Today your challenge is to produce all possible full parenthesizations of an expression.
Your input is a single line of printable ASCII containing one or more terms separated by operators. The input might also contains spaces - you must ignore these. A term is [a-zA-Z0-9]
, an operator is [^ ()a-zA-Z0-9]
. You may assume that the input is always valid.
Output all possible ways to fully parenthesize the given expression, separated by newlines with an optional trailing newline.
Do not:
- Parenthesize terms - only parenthesize around operators.
- Reorder the terms.
- Output any spaces.
Example input/output:
N
N
a * b
(a*b)
x_x_0
(x_(x_0))
((x_x)_0)
a * b|c|d
(a*(b|(c|d)))
(a*((b|c)|d))
((a*b)|(c|d))
((a*(b|c))|d)
(((a*b)|c)|d)
Smallest code in bytes wins.
You have to list down the exact operators we have to consider. Is
!
an operator? What about↑
? – Optimizer – 2015-11-29T10:31:32.343@Optimizer I listed the exact regular expression of what is considered an operator.
!
fits the regex, so does↑
, however↑
can not be part of the input because it is not printable ASCII. – orlp – 2015-11-29T10:34:56.040Ah okay. So anything except a term is an operator... – Optimizer – 2015-11-29T10:37:29.177
So both terms and operators are always one character long? – user81655 – 2015-11-29T11:05:56.190
@user81655 Correct. – orlp – 2015-11-29T11:19:56.477
Can we optionally assume a trailing newline in the input? – Martin Ender – 2015-11-29T11:21:36.970
@Optimizer and except spaces and parentheses. – Martin Ender – 2015-11-29T11:23:46.330
@MartinBüttner Sure, you may assume a trailing newline in the input. – orlp – 2015-11-29T11:38:57.267
This means that you can't write a function? – LegionMammal978 – 2015-11-29T12:17:53.830
@LegionMammal978 The default rules apply.
– orlp – 2015-11-29T12:22:50.5201insert obligatory LISP-related pun here – cat – 2015-11-29T12:27:42.093