Skip to content

Commit 61cb407

Browse files
feat(curriculum): daily challenges 264-278 (freeCodeCamp#67117)
Co-authored-by: majestic-owl448 <26656284+majestic-owl448@users.noreply.github.com>
1 parent df2dd02 commit 61cb407

33 files changed

Lines changed: 2722 additions & 1 deletion
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
id: 69cfca90e8a0a6d4d6871c4e
3+
title: "Challenge 264: Anagram Groups"
4+
challengeType: 28
5+
dashedName: challenge-264
6+
---
7+
8+
# --description--
9+
10+
Given an array of words, return a 2d array of the words grouped into anagrams.
11+
12+
- Words are anagrams if they contain the same letters in any order.
13+
- Each word belongs to exactly one group.
14+
- Return order doesn't matter.
15+
16+
For example, given `["listen", "silent", "hello", "enlist", "world"]`, return `[["listen", "silent", "enlist"], ["hello"], ["world"]]`.
17+
18+
# --hints--
19+
20+
`groupAnagrams(["listen", "silent", "hello", "enlist", "world"])` should return `[["listen", "silent", "enlist"], ["hello"], ["world"]]`.
21+
22+
```js
23+
const groups = groupAnagrams(["listen", "silent", "hello", "enlist", "world"]);
24+
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
25+
assert.deepEqual(sorted, [["enlist", "listen", "silent"], ["hello"], ["world"]]);
26+
```
27+
28+
`groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])` should return `[["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]`.
29+
30+
```js
31+
const groups = groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]);
32+
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
33+
assert.deepEqual(sorted, [["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]);
34+
```
35+
36+
`groupAnagrams(["care", "race", "acre", "pots", "stop", "tops", "opts", "post", "spot", "evil", "vile", "live", "veil"])` should return `[["acre", "care", "race"], ["evil", "live", "veil", "vile"], ["opts", "post", "pots", "spot", "stop", "tops"]]`.
37+
38+
```js
39+
const groups = groupAnagrams(["care", "race", "acre", "pots", "stop", "tops", "opts", "post", "spot", "evil", "vile", "live", "veil"]);
40+
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
41+
assert.deepEqual(sorted, [["acre", "care", "race"], ["evil", "live", "veil", "vile"], ["opts", "post", "pots", "spot", "stop", "tops"]]);
42+
43+
```
44+
45+
`groupAnagrams(["algorithms", "logarithms", "education", "cautioned", "auctioned", "triangle", "integral", "alerting", "relating"])` should return `[["alerting", "integral", "relating", "triangle"], ["algorithms", "logarithms"], ["auctioned", "cautioned", "education"]]`.
46+
47+
```js
48+
const groups = groupAnagrams(["algorithms", "logarithms", "education", "cautioned", "auctioned", "triangle", "integral", "alerting", "relating"]);
49+
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
50+
assert.deepEqual(sorted, [["alerting", "integral", "relating", "triangle"], ["algorithms", "logarithms"], ["auctioned", "cautioned", "education"]]);
51+
```
52+
53+
# --seed--
54+
55+
## --seed-contents--
56+
57+
```js
58+
function groupAnagrams(words) {
59+
60+
return words;
61+
}
62+
```
63+
64+
# --solutions--
65+
66+
```js
67+
function groupAnagrams(words) {
68+
const map = {};
69+
for (const word of words) {
70+
const key = word.split('').sort().join('');
71+
if (!map[key]) map[key] = [];
72+
map[key].push(word);
73+
}
74+
return Object.values(map);
75+
}
76+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: 69cfca90e8a0a6d4d6871c4f
3+
title: "Challenge 265: Deepest Brackets"
4+
challengeType: 28
5+
dashedName: challenge-265
6+
---
7+
8+
# --description--
9+
10+
Given a string containing balanced brackets, return the content of the deepest nested brackets.
11+
12+
- Brackets can be any of the three types: `()`, `[]`, and `{}`.
13+
- The input will always have a single deepest group.
14+
15+
For example, given `"(hello (world))"`, return `"world"`.
16+
17+
# --hints--
18+
19+
`getDeepestBrackets("(hello (world))")` should return `"world"`.
20+
21+
```js
22+
assert.equal(getDeepestBrackets("(hello (world))"), "world");
23+
```
24+
25+
`getDeepestBrackets("[outer [inner] outer]")` should return `"inner"`.
26+
27+
```js
28+
assert.equal(getDeepestBrackets("[outer [inner] outer]"), "inner");
29+
```
30+
31+
`getDeepestBrackets("{a{b}c{d{e}f}g}")` should return `"e"`.
32+
33+
```js
34+
assert.equal(getDeepestBrackets("{a{b}c{d{e}f}g}"), "e");
35+
```
36+
37+
`getDeepestBrackets("[the {quick (brown [fox] jumped) over (the) lazy} dog]")` should return `"fox"`.
38+
39+
```js
40+
assert.equal(getDeepestBrackets("[the {quick (brown [fox] jumped) over (the) lazy} dog]"), "fox");
41+
```
42+
43+
`getDeepestBrackets("f[(r)e{e}C{o[(d){e(C)}a]m}]p")` should return `"C"`.
44+
45+
```js
46+
assert.equal(getDeepestBrackets("f[(r)e{e}C{o[(d){e(C)}a]m}]p"), "C");
47+
```
48+
49+
# --seed--
50+
51+
## --seed-contents--
52+
53+
```js
54+
function getDeepestBrackets(str) {
55+
56+
return str;
57+
}
58+
```
59+
60+
# --solutions--
61+
62+
```js
63+
function getDeepestBrackets(str) {
64+
let maxDepth = 0, depth = 0, content = '';
65+
let start = 0;
66+
67+
for (let i = 0; i < str.length; i++) {
68+
if ('([{'.includes(str[i])) {
69+
depth++;
70+
if (depth > maxDepth) {
71+
maxDepth = depth;
72+
start = i + 1;
73+
}
74+
} else if (')]}'.includes(str[i])) {
75+
if (depth === maxDepth) {
76+
content = str.slice(start, i);
77+
}
78+
depth--;
79+
}
80+
}
81+
return content;
82+
}
83+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
id: 69cfca90e8a0a6d4d6871c50
3+
title: "Challenge 266: Good Day"
4+
challengeType: 28
5+
dashedName: challenge-266
6+
---
7+
8+
# --description--
9+
10+
Given a time string in `"HH:MM"` format (24-hour clock), return:
11+
12+
- `"Good morning"` for times `05:00` to `11:59`
13+
- `"Good afternoon"` for times `12:00` to `17:59`
14+
- `"Good evening"` for times `18:00` to `21:59`
15+
- `"Good night"` for times `22:00` to `04:59`
16+
17+
# --hints--
18+
19+
`getGreeting("06:30")` should return `"Good morning"`.
20+
21+
```js
22+
assert.equal(getGreeting("06:30"), "Good morning");
23+
```
24+
25+
`getGreeting("12:00")` should return `"Good afternoon"`.
26+
27+
```js
28+
assert.equal(getGreeting("12:00"), "Good afternoon");
29+
```
30+
31+
`getGreeting("21:59")` should return `"Good evening"`.
32+
33+
```js
34+
assert.equal(getGreeting("21:59"), "Good evening");
35+
```
36+
37+
`getGreeting("00:01")` should return `"Good night"`.
38+
39+
```js
40+
assert.equal(getGreeting("00:01"), "Good night");
41+
```
42+
43+
`getGreeting("11:30")` should return `"Good morning"`.
44+
45+
```js
46+
assert.equal(getGreeting("11:30"), "Good morning");
47+
```
48+
49+
# --seed--
50+
51+
## --seed-contents--
52+
53+
```js
54+
function getGreeting(time) {
55+
56+
return time;
57+
}
58+
```
59+
60+
# --solutions--
61+
62+
```js
63+
function getGreeting(time) {
64+
const [hours, minutes] = time.split(':').map(Number);
65+
const total = hours * 60 + minutes;
66+
67+
if (total >= 300 && total < 720) return 'Good morning';
68+
if (total >= 720 && total < 1080) return 'Good afternoon';
69+
if (total >= 1080 && total < 1320) return 'Good evening';
70+
return 'Good night';
71+
}
72+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
id: 69cfca90e8a0a6d4d6871c51
3+
title: "Challenge 267: Parsec Converter"
4+
challengeType: 28
5+
dashedName: challenge-267
6+
---
7+
8+
# --description--
9+
10+
In a distant galaxy, parsecs are used to measure both time and distance. Given an integer number of parsecs, return its equivalent in time or distance.
11+
12+
- If the given integer is odd, it represents time. If it's even, it represents distance.
13+
14+
Use these conversion rates:
15+
16+
| Parsecs | Time/Distance |
17+
| - | - |
18+
| 1 | 2 hours |
19+
| 2 | 6 light years |
20+
21+
Return the converted value as an integer.
22+
23+
# --hints--
24+
25+
`convertParsecs(1)` should return `2`.
26+
27+
```js
28+
assert.equal(convertParsecs(1), 2);
29+
```
30+
31+
`convertParsecs(2)` should return `6`.
32+
33+
```js
34+
assert.equal(convertParsecs(2), 6);
35+
```
36+
37+
`convertParsecs(31)` should return `62`.
38+
39+
```js
40+
assert.equal(convertParsecs(31), 62);
41+
```
42+
43+
`convertParsecs(88)` should return `264`.
44+
45+
```js
46+
assert.equal(convertParsecs(88), 264);
47+
```
48+
49+
`convertParsecs(17)` should return `34`.
50+
51+
```js
52+
assert.equal(convertParsecs(17), 34);
53+
```
54+
55+
`convertParsecs(14)` should return `42`.
56+
57+
```js
58+
assert.equal(convertParsecs(14), 42);
59+
```
60+
61+
# --seed--
62+
63+
## --seed-contents--
64+
65+
```js
66+
function convertParsecs(parsecs) {
67+
68+
return parsecs;
69+
}
70+
```
71+
72+
# --solutions--
73+
74+
```js
75+
function convertParsecs(parsecs) {
76+
if (parsecs % 2 !== 0) {
77+
return parsecs * 2;
78+
} else {
79+
return parsecs * 3;
80+
}
81+
}
82+
```

0 commit comments

Comments
 (0)