Skip to content

Commit b4d2001

Browse files
committed
✨ Challenge #4: Reverse the Parentheses
1 parent 4fd1857 commit b4d2001

File tree

4 files changed

+74
-41
lines changed

4 files changed

+74
-41
lines changed

2023/challenge-04/README.md

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,29 @@
1-
# Box inside a box and another...
1+
# Challenge #4: 😵‍💫 Reverse the Parentheses
22

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.
44

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.
66

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.
88

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:
1210

1311
```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
1914

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!
2117

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
2820

29-
fitsInOneBox(boxes); // false
21+
// Step by step:
22+
// 1. Reverse the nested -> sa(ualcatn)s
23+
// 2. Reverse the remaining -> santaclaus
3024
```
25+
## Notes:
3126

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.

2023/challenge-04/solution.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function decode(message) {
2+
let i;
3+
while ((i = message.lastIndexOf("(")) + 1) {
4+
const j = message.indexOf(")", i);
5+
if (j + 1)
6+
message =
7+
message.slice(0, i) +
8+
message
9+
.slice(i + 1, j)
10+
.split("")
11+
.reverse()
12+
.join("") +
13+
message.slice(j + 1);
14+
}
15+
return message;
16+
}

2023/challenge-04/solution.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { decode } from "./solution";
2+
3+
describe("Challenge #4: 😵‍💫 Reverse the Parentheses", () => {
4+
const testCases = [
5+
createTestCase(
6+
["hola (odnum)"],
7+
"hola mundo",
8+
"should reverse characters inside parentheses"
9+
),
10+
createTestCase(
11+
["(olleh) (dlrow)!"],
12+
"hello world!",
13+
"should reverse characters inside parentheses, multiple occurrences"
14+
),
15+
createTestCase(
16+
["sa(u(cla)atn)s"],
17+
"santaclaus",
18+
"should handle nested parentheses"
19+
),
20+
createTestCase(
21+
[""],
22+
"",
23+
"should return an empty string when the input is an empty string"
24+
),
25+
createTestCase(
26+
["abcde (12345)"],
27+
"edcba 54321",
28+
"should reverse characters inside parentheses, alphanumeric content"
29+
),
30+
];
31+
32+
it.each(testCases)("#$# $description", ({ input, expected, description }) => {
33+
expect(decode(...input)).toEqual(expected);
34+
});
35+
});
36+
37+
function createTestCase(input, expected, description) {
38+
return { input, expected, description };
39+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
100100
| 01 | [First repeat gift!](2023/challenge-01) | 🟢 | [Show](2023/challenge-01/solution.js) |
101101
| 02 | [Factory in Action!](2023/challenge-02) | 🟢 | [Show](2023/challenge-02/solution.js) |
102102
| 03 | [The naughty elf](2023/challenge-03) | 🟢 | [Show](2023/challenge-03/solution.js) |
103+
| 04 | [Reverse the Parentheses](2023/challenge-04) | 🟠 | [Show](2023/challenge-04/solution.js) |
103104

104105
[^1]: **Difficulty**: 🟢 Easy 🟠 Medium 🔴 Hard 🟣 Very Hard
105106

0 commit comments

Comments
 (0)