|
1 |
| -# Box inside a box and another... |
| 1 | +# Challenge #4: 😵💫 Reverse the Parentheses |
2 | 2 |
|
3 |
| -## 🔢 Instructions |
| 3 | +In Santa's workshop 🎅, some Christmas messages have been written in a peculiar way: the letters inside the parentheses should be read backward. |
4 | 4 |
|
5 |
| -Santa Claus needs to review his gift boxes to make sure he can pack them all in his sleigh. He has a series of **boxes of different sizes, characterized by their length, width, and height**. |
| 5 | +Santa needs these messages to be correctly formatted. Your task is to write a function that takes a text string and reverses the characters inside each pair of parentheses, removing the parentheses in the final message. |
6 | 6 |
|
7 |
| -Your task is to **write a function** that, given a list of boxes with their sizes, determines whether it is possible to pack **all the boxes in one so that each box contains another** (which in turn contains another, and so on). |
| 7 | +However, keep in mind that there may be nested parentheses, so you must reverse the characters in the correct order. |
8 | 8 |
|
9 |
| -Each box represents its measures with an object. For example: `{l: 2, w: 3, h: 2}`. This means that the box has a length of 2, a width of 3 and a height of 2. |
10 |
| - |
11 |
| -A box fits into another box if all the sides of the first are smaller than the sides of the second. **The elves have told us that the boxes cannot be rotated**, so you cannot put a box of 2x3x2 in a box of 3x2x2. Let's see some examples: |
| 9 | +## Example: |
12 | 10 |
|
13 | 11 | ```javascript
|
14 |
| -fitsInOneBox([ |
15 |
| - { l: 1, w: 1, h: 1 }, |
16 |
| - { l: 2, w: 2, h: 2 } |
17 |
| -]); // true |
18 |
| -``` |
| 12 | +const a = decode('hola (odnum)') |
| 13 | +console.log(a) // hola mundo |
19 | 14 |
|
20 |
| -In the previous example, the smallest box fits into the largest box. Therefore, it is possible to pack all the boxes in one. Now let's see a case that does not: |
| 15 | +const b = decode('(olleh) (dlrow)!') |
| 16 | +console.log(b) // hello world! |
21 | 17 |
|
22 |
| -```javascript |
23 |
| -const boxes = [ |
24 |
| - { l: 1, w: 1, h: 1 }, |
25 |
| - { l: 2, w: 2, h: 2 }, |
26 |
| - { l: 3, w: 1, h: 3 } |
27 |
| -]; |
| 18 | +const c = decode('sa(u(cla)atn)s') |
| 19 | +console.log(c) // santaclaus |
28 | 20 |
|
29 |
| -fitsInOneBox(boxes); // false |
| 21 | +// Step by step: |
| 22 | +// 1. Reverse the nested -> sa(ualcatn)s |
| 23 | +// 2. Reverse the remaining -> santaclaus |
30 | 24 | ```
|
| 25 | +## Notes: |
31 | 26 |
|
32 |
| -In the previous example, the smallest box fits into the middle box, but the middle box does not fit into the largest box. Therefore, it is not possible to pack all the boxes in one. |
33 |
| - |
34 |
| -Note that the boxes may not come in order: |
35 |
| - |
36 |
| -```javascript |
37 |
| -const boxes = [ |
38 |
| - { l: 1, w: 1, h: 1 }, |
39 |
| - { l: 3, w: 3, h: 3 }, |
40 |
| - { l: 2, w: 2, h: 2 } |
41 |
| -]; |
42 |
| - |
43 |
| -fitsInOneBox(boxes); // true |
44 |
| -``` |
45 |
| - |
46 |
| -In the previous example, the first box fits into the third, and the third into the second. Therefore, it is possible to pack all the boxes in one. |
47 |
| - |
48 |
| -**Things to keep in mind**: |
49 |
| - |
50 |
| -- The boxes cannot be rotated because the elves have told us that the machine is not ready. |
51 |
| -- The boxes may come in any order. |
52 |
| -- The boxes are not always squares, they could be rectangles. |
| 27 | +- The input strings will always be well-formed with correctly matching parentheses; no need to validate them. |
| 28 | +- There should be no parentheses left in the final message. |
| 29 | +- The maximum nesting level is 2. |
0 commit comments