The absolute value of a number \$x\$ is normally written as \$|x|\$. The left and right side of the absolute value uses the same symbol, so it is not immediately obvious how to parse nested absolute values e.g. \$||1-2|+|3-|4-5|||\$
Your goal is to parse such an expression containing nested absolute values:
The expression will be given as a string of characters.
For simplicity the expression will only contain single digit numbers (or letters if that is easier in your language),
the operators +
and -
(you can use any two distinct characters to represent these operations) and the symbol |
for the left and right side of a absolute value.
You do not need to handle the case where a number is directly adjacent to a absolute value (e.g. 2|3|
or |2|3
, this generalization will be handled in the companion challenge)
Your output should be the same expression in a form that allows to determine how the absolute values are bracketed.
The output has to satisfy the following rules:
- The expression within a absolute value must not end with an operator (
+
or-
) - The expression within a absolute value cannot be empty
- Each
|
has to be part of exactly one absolute value
You may assume there is a valid way to parse the given input.
Examples:
|2| -> (2)
|2|+|3| -> (2)+(3)
||2|| -> ((2))
||2|-|3|| -> ((2)-(3))
|-|-2+3|| -> (-(-2+3))
|-|-2+3|+|4|-5| -> (-(-2+3)+(4)-5)
|-|-2+|-3|+4|-5| -> (-(-2+(-3)+4)-5)
||1-2|+|3-|4-5||| -> ((1-2)+(3-(4-5)))
This is code-golf the shortest solution wins.
Parse nested absolute values 2
The absolute value of a number \$x\$ is normally written as \$|x|\$. The left and right side of the absolute value uses the same symbol, so it is not immediately obvious how to parse nested absolute values e.g. \$||1-2|+|3-|4-5|||\$
Your goal is to parse such an expression containing nested absolute values:
Follow up to my previous challenge about parsing nested absolute values
The expression will be given as a string of characters.
For simplicity the expression will only contain single digit numbers (or letters if that is easier in your language),
the operators +
and -
(you can use any two distinct characters to represent these operations) and the symbol |
for the left and right side of a absolute value.
To make the challenger not too easy, it is allowed for numbers to be directly adjacent to absolute values so (2|3|
and |2|3
both are valid expressions)
Your output should be the same expression in a form that allows to determine how the absolute values are bracketed.
The output has to satisfy the following rules:
- The expression within a absolute value must not end with an operator (
+
or-
) - The expression within a absolute value cannot be empty
- Each
|
has to be part of exactly one absolute value
You may assume there is a valid way to parse the given input. If there is more than one way to parse the expression you can choose any valid solution.
Examples (all possible outputs):
|2| -> (2)
|2|+|3| -> (2)+(3)
|2|3|-4| -> (2)3(-4)
|2|3|-4| -> (2(3)-4)
|2|3-|4| -> (2)3-(4)
||2|| -> ((2))
||2|-|3|| -> ((2)-(3))
|-|-2+3|| -> (-(-2+3))
|-|-2+3|+|4|-5| -> (-(-2+3)+(4)-5)
|-|-2+|-3|+4|-5| -> (-(-2+(-3)+4)-5)
||1-2|+|3-|4-5||| -> ((1-2)+(3-(4-5)))
This is code-golf the shortest solution wins.Part1
Meta
Is this a duplicate?
Is the formulation clear?
Should I post both versions, or only one of them (The firsts one is relatively easy, the second one is probably more difficult)?