Given a string that is potentially boxed in, toggle the box. This becomes clearer in the test cases and explanation.
Input / Output
Input Format
The input will be either a single string separated by CR, LF, or CRLF, or a list of strings. The input format is up to you to decide.
Output Format
The output format must be in the same format as the input.
Explanation + Example
Let's take a boxed string as an example:
+-------------+
|Hello, World!|
+-------------+
To toggle the box, we remove the first and last row and the first and last column. You may assume that there will be no trailing whitespace after the last line (with an optional trailing newline at the end of the last line), and no trailing whitespace on any line, not counting the newline of course.
This would become:
Hello, World!
The Other Way
If the string is not in a box, add a box to it. This is fairly simple; prepend +---...---+
on its own line with k
dashes where k
is the length of the longest line, and then for each line, pad it with trailing whitespace to match the length of the longest line and then prepend and append a pipe character ("|"
). Finally, append a line containing the same +---...---+
setup as the first line.
For example:
Hello, World!
becomes:
+-------------+
|Hello, World!|
+-------------+
You may assume that none of the lines in the input will have trailing whitespace.
Another example of a string that should have a box put around it:
Hello,
World!
becomes
+----------+
|Hello, |
| World!|
+----------+
An example of something that should still be boxed:
+--------+
| |
--------+
becomes:
+----------+
|+--------+|
|| ||
| --------+|
+----------+
Rules + Specifications
- Standard Loopholes Apply
- No line of input will have any leading or trailing whitespace both overall and in each line, regardless of whether or not it has been boxed in
- Boxes will only have
+
as their corners and|
for the vertical edges and-
for the horizontal sides. - A box must have its pluses in place to be considered a box; if it has height or width 2 (that is, no content), it should still be unboxed, which would result in a bunch of newlines, depending on the height of the box.
- If the input has a box but text outside of the box, the whole thing should be boxed.
- Your program must check the entire perimeter of the string. If a single character along the outside is not correct (either missing or a different character than what it should be), then it should be boxed, not unboxed.
- The unboxed string itself may contain
+ | -
. If the unboxed string itself has a box around it, return the string with the box; it should only be unboxed once.
Edge Cases
1: Small boxes
Input:
++
++
Output is a empty or a newline
Input:
+----+
+----+
Output is empty or a newline
Input:
++
||
||
++
Output is 2 newlines or 3 newlines
2: Partial Box
Input:
+-------+
| Hello |
+ ------+
Output:
+---------+
|+-------+|
|| Hello ||
|+ ------+|
+---------+
3: Text outside of box
Input:
+-------+
a| Hello |
+-------+
Output:
+----------+
| +-------+|
|a| Hello ||
| +-------+|
+----------+