Skip to content

Commit 5c887f7

Browse files
committed
✨ Challenge #11: The Studious Elves
1 parent b03bc69 commit 5c887f7

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

Diff for: 2023/challenge-11/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## Challenge #11: 📖 The Studious Elves
2+
3+
In Santa's workshop, the elves are known for their love of puzzles and brain teasers. This year, they've come up with a special challenge: to create a Christmas palindrome.
4+
5+
A palindrome is a word that reads the same forwards and backward. The task is to determine if it's possible to transform a given string into a palindrome with, at most, a single exchange of letters.
6+
7+
```javascript
8+
/**
9+
* Determines if it's possible to form a palindrome with at most one letter exchange.
10+
* @param {string} word - The input string.
11+
* @returns {(number[]|null)} - Returns an array of two indices if a palindrome can be formed with one change,
12+
* an empty array if the input is already a palindrome, or null if it's not possible.
13+
*/
14+
function getIndexsForPalindrome(word) {
15+
// Implementation goes here
16+
}
17+
```
18+
19+
### Examples
20+
21+
```javascript
22+
getIndexsForPalindrome('anna'); // Returns: []
23+
getIndexsForPalindrome('abab'); // Returns: [0, 1]
24+
getIndexsForPalindrome('abac'); // Returns: null
25+
getIndexsForPalindrome('aaaaaaaa'); // Returns: []
26+
getIndexsForPalindrome('aaababa'); // Returns: [1, 3]
27+
getIndexsForPalindrome('caababa'); // Returns: null
28+
```
29+
30+
### Explanation
31+
32+
- If the input string is already a palindrome, the function returns an empty array.
33+
- If it's not possible to form a palindrome, the function returns null.
34+
- If a palindrome can be formed with one change, the function returns an array with the two positions (indexes) that must be swapped to create it.
35+
36+
**Note:**
37+
38+
- The function should always return the first valid pair of indices if multiple swaps are possible.
39+
- Ensure the provided examples match the expected output.
40+
- Feel free to add more examples or edge cases to thoroughly test the function.

Diff for: 2023/challenge-11/solution.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function getIndexsForPalindrome(word) {
2+
const isPalindrome = (str) => str === str.split("").reverse().join("");
3+
4+
if (isPalindrome(word)) {
5+
return [];
6+
}
7+
8+
for (let i = 0; i < word.length; i++) {
9+
for (let j = i + 1; j < word.length; j++) {
10+
const swapped = [...word];
11+
[swapped[i], swapped[j]] = [swapped[j], swapped[i]];
12+
if (isPalindrome(swapped.join(""))) {
13+
return [i, j];
14+
}
15+
}
16+
}
17+
18+
return null;
19+
}

Diff for: 2023/challenge-11/solution.spec.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { getIndexsForPalindrome } from "./solution";
2+
3+
describe("Challenge #11: 📖 The Studious Elves", () => {
4+
const testCases = [
5+
createTestCase(
6+
["anna"],
7+
[],
8+
"should return an empty array for an input that is already a palindrome"
9+
),
10+
createTestCase(
11+
["abab"],
12+
[0, 1],
13+
"should return the indices for a single swap palindrome"
14+
),
15+
createTestCase(
16+
["abac"],
17+
null,
18+
"should return null for an input where no palindrome can be formed"
19+
),
20+
createTestCase(
21+
["aaaaaaaa"],
22+
[],
23+
"should return an empty array for a string of the same characters"
24+
),
25+
createTestCase(
26+
["aaababa"],
27+
[1, 3],
28+
"should return the indices for a single swap palindrome"
29+
),
30+
createTestCase(
31+
["caababa"],
32+
null,
33+
"should return null for an input where no palindrome can be formed"
34+
),
35+
];
36+
37+
it.each(testCases)("#$# $description", ({ input, expected, description }) => {
38+
expect(getIndexsForPalindrome(...input)).toEqual(expected);
39+
});
40+
});
41+
42+
function createTestCase(input, expected, description) {
43+
return { input, expected, description };
44+
}

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ This repository contains the solutions to the challenges proposed by [@midudev](
107107
| 08 | [Sorting the warehouse](2023/challenge-08) | 🟠 | [Show](2023/challenge-08/solution.js) |
108108
| 09 | [Switch the lights](2023/challenge-09) | 🟢 | [Show](2023/challenge-09/solution.js) |
109109
| 10 | [Create your own Christmas tree](2023/challenge-10) | 🟢 | [Show](2023/challenge-10/solution.js) |
110+
| 11 | [Challenge #11: 📖 The studious elves](2023/challenge-11) | 🟠 | [Show](2023/challenge-11/solution.js) |
110111

111112
[^1]: **Difficulty**: 🟢 Easy 🟠 Medium 🔴 Hard 🟣 Very Hard
112113

0 commit comments

Comments
 (0)