mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-27 02:01:02 -04:00
feat(curriculum): daily challenges 80-89 (#62910)
Co-authored-by: Ilenia <26656284+ilenia-magoni@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd62
|
||||
title: "Challenge 80: Email Sorter"
|
||||
challengeType: 28
|
||||
dashedName: challenge-80
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On October 29, 1971, the first email ever was sent, introducing the `username@domain` format we still use. Now, there are billions of email addresses.
|
||||
|
||||
In this challenge, you are given a list of email addresses and need to sort them alphabetically by domain name first (the part after the `@`), and username second (the part before the `@`).
|
||||
|
||||
- Sorting should be case-insensitive.
|
||||
- If more than one email has the same domain, sort them by their username.
|
||||
- Return an array of the sorted addresses.
|
||||
- Returned addresses should retain their original case.
|
||||
|
||||
For example, given `["jill@mail.com", "john@example.com", "jane@example.com"]`, return `["jane@example.com", "john@example.com", "jill@mail.com"]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sort(["jill@mail.com", "john@example.com", "jane@example.com"])` should return `["jane@example.com", "john@example.com", "jill@mail.com"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sort(["jill@mail.com", "john@example.com", "jane@example.com"]), ["jane@example.com", "john@example.com", "jill@mail.com"]);
|
||||
```
|
||||
|
||||
`sort(["bob@mail.com", "alice@zoo.com", "carol@mail.com"])` should return `["bob@mail.com", "carol@mail.com", "alice@zoo.com"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sort(["bob@mail.com", "alice@zoo.com", "carol@mail.com"]), ["bob@mail.com", "carol@mail.com", "alice@zoo.com"]);
|
||||
```
|
||||
|
||||
`sort(["user@z.com", "user@y.com", "user@x.com"])` should return `["user@x.com", "user@y.com", "user@z.com"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sort(["user@z.com", "user@y.com", "user@x.com"]), ["user@x.com", "user@y.com", "user@z.com"]);
|
||||
```
|
||||
|
||||
`sort(["sam@MAIL.com", "amy@mail.COM", "bob@Mail.com"])` should return `["amy@mail.COM", "bob@Mail.com", "sam@MAIL.com"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sort(["sam@MAIL.com", "amy@mail.COM", "bob@Mail.com"]), ["amy@mail.COM", "bob@Mail.com", "sam@MAIL.com"]);
|
||||
```
|
||||
|
||||
`sort(["simon@beta.com", "sammy@alpha.com", "Sarah@Alpha.com", "SAM@ALPHA.com", "Simone@Beta.com", "sara@alpha.com"])` should return `["SAM@ALPHA.com", "sammy@alpha.com", "sara@alpha.com", "Sarah@Alpha.com", "simon@beta.com", "Simone@Beta.com"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sort(["simon@beta.com", "sammy@alpha.com", "Sarah@Alpha.com", "SAM@ALPHA.com", "Simone@Beta.com", "sara@alpha.com"]), ["SAM@ALPHA.com", "sammy@alpha.com", "sara@alpha.com", "Sarah@Alpha.com", "simon@beta.com", "Simone@Beta.com"]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sort(emails) {
|
||||
|
||||
return emails;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sort(emails) {
|
||||
return emails.slice().sort((a, b) => {
|
||||
const [userA, domainA] = a.split('@');
|
||||
const [userB, domainB] = b.split('@');
|
||||
|
||||
const domainCompare = domainA.toLowerCase().localeCompare(domainB.toLowerCase());
|
||||
if (domainCompare !== 0) return domainCompare;
|
||||
|
||||
return userA.toLowerCase().localeCompare(userB.toLowerCase());
|
||||
});
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd63
|
||||
title: "Challenge 81: Nth Prime"
|
||||
challengeType: 28
|
||||
dashedName: challenge-81
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A prime number is a positive integer greater than 1 that is divisible only by 1 and itself. The first five prime numbers are `2`, `3`, `5`, `7`, and `11`.
|
||||
|
||||
Given a positive integer `n`, return the `n`th prime number. For example, given `5` return the 5th prime number: `11`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`nthPrime(5)` should return `11`.
|
||||
|
||||
```js
|
||||
assert.equal(nthPrime(5), 11);
|
||||
```
|
||||
|
||||
`nthPrime(10)` should return `29`.
|
||||
|
||||
```js
|
||||
assert.equal(nthPrime(10), 29);
|
||||
```
|
||||
|
||||
`nthPrime(16)` should return `53`.
|
||||
|
||||
```js
|
||||
assert.equal(nthPrime(16), 53);
|
||||
```
|
||||
|
||||
`nthPrime(99)` should return `523`.
|
||||
|
||||
```js
|
||||
assert.equal(nthPrime(99), 523);
|
||||
```
|
||||
|
||||
`nthPrime(1000)` should return `7919`.
|
||||
|
||||
```js
|
||||
assert.equal(nthPrime(1000), 7919);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function nthPrime(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function nthPrime(n) {
|
||||
const primes = [];
|
||||
let num = 2;
|
||||
|
||||
while (primes.length < n) {
|
||||
let isPrime = true;
|
||||
for (let i = 2; i * i <= num; i++) {
|
||||
if (num % i === 0) {
|
||||
isPrime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isPrime) primes.push(num);
|
||||
num++;
|
||||
}
|
||||
|
||||
return primes[n - 1];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd64
|
||||
title: "Challenge 82: SpOoKy~CaSe"
|
||||
challengeType: 28
|
||||
dashedName: challenge-82
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a variable name, convert it to "spooky case" using the following constraints:
|
||||
|
||||
- Replace all underscores (`_`), and hyphens (`-`) with a tilde (`~`).
|
||||
- Capitalize the first letter of the string, and every other letter after that, ignore the tilde character when counting.
|
||||
|
||||
For example, given `hello_world`, return `HeLlO~wOrLd`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`spookify("hello_world")` should return `"HeLlO~wOrLd"`.
|
||||
|
||||
```js
|
||||
assert.equal(spookify("hello_world"), "HeLlO~wOrLd");
|
||||
```
|
||||
|
||||
`spookify("Spooky_Case")` should return `"SpOoKy~CaSe"`.
|
||||
|
||||
```js
|
||||
assert.equal(spookify("Spooky_Case"), "SpOoKy~CaSe");
|
||||
```
|
||||
|
||||
`spookify("TRICK-or-TREAT")` should return `"TrIcK~oR~tReAt"`.
|
||||
|
||||
```js
|
||||
assert.equal(spookify("TRICK-or-TREAT"), "TrIcK~oR~tReAt");
|
||||
```
|
||||
|
||||
`spookify("c_a-n_d-y_-b-o_w_l")` should return `"C~a~N~d~Y~~b~O~w~L"`.
|
||||
|
||||
```js
|
||||
assert.equal(spookify("c_a-n_d-y_-b-o_w_l"), "C~a~N~d~Y~~b~O~w~L");
|
||||
```
|
||||
|
||||
`spookify("thE_hAUntEd-hOUsE-Is-fUll_Of_ghOsts")` should return `"ThE~hAuNtEd~HoUsE~iS~fUlL~oF~gHoStS"`.
|
||||
|
||||
```js
|
||||
assert.equal(spookify("thE_hAUntEd-hOUsE-Is-fUll_Of_ghOsts"), "ThE~hAuNtEd~HoUsE~iS~fUlL~oF~gHoStS");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function spookify(boo) {
|
||||
|
||||
return boo;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function spookify(boo) {
|
||||
const replaced = boo.replace(/[_-]/g, "~");
|
||||
|
||||
let result = "";
|
||||
let capitalize = true;
|
||||
|
||||
for (let char of replaced) {
|
||||
if (char === "~") {
|
||||
result += char;
|
||||
} else {
|
||||
result += capitalize ? char.toUpperCase() : char.toLowerCase();
|
||||
capitalize = !capitalize;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,98 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd65
|
||||
title: "Challenge 83: Signature Validation"
|
||||
challengeType: 28
|
||||
dashedName: challenge-83
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a message string, a secret key string, and a signature number, determine if the signature is valid using this encoding method:
|
||||
|
||||
- Letters in the message and secret key have these values:
|
||||
- `a` to `z` have values `1` to `26` respectively.
|
||||
- `A` to `Z` have values `27` to `52` respectively.
|
||||
- All other characters have no value.
|
||||
- Compute the signature by taking the sum of the message plus the sum of the secret key.
|
||||
|
||||
For example, given the message `"foo"` and the secret key `"bar"`, the signature would be `57`:
|
||||
|
||||
```md
|
||||
f (6) + o (15) + o (15) = 36
|
||||
b (2) + a (1) + r (18) = 21
|
||||
36 + 21 = 57
|
||||
```
|
||||
|
||||
Check if the computed signature matches the provided signature.
|
||||
|
||||
# --hints--
|
||||
|
||||
`verify("foo", "bar", 57)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(verify("foo", "bar", 57));
|
||||
```
|
||||
|
||||
`verify("foo", "bar", 54)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.isFalse(verify("foo", "bar", 54));
|
||||
```
|
||||
|
||||
`verify("freeCodeCamp", "Rocks", 238)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(verify("freeCodeCamp", "Rocks", 238));
|
||||
```
|
||||
|
||||
`verify("Is this valid?", "No", 210)` should return `false`.
|
||||
|
||||
```js
|
||||
assert.isFalse(verify("Is this valid?", "No", 210));
|
||||
```
|
||||
|
||||
`verify("Is this valid?", "Yes", 233)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(verify("Is this valid?", "Yes", 233));
|
||||
```
|
||||
|
||||
`verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514)` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function verify(message, key, signature) {
|
||||
|
||||
return message;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function verify(message, key, signature) {
|
||||
function charValue(ch) {
|
||||
if (ch >= 'a' && ch <= 'z') return ch.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
|
||||
if (ch >= 'A' && ch <= 'Z') return ch.charCodeAt(0) - 'A'.charCodeAt(0) + 27;
|
||||
return 0;
|
||||
}
|
||||
|
||||
function computeSum(str) {
|
||||
let sum = 0;
|
||||
for (let ch of str) {
|
||||
sum += charValue(ch);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
const total = computeSum(message) + computeSum(key);
|
||||
return total === signature;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,87 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd66
|
||||
title: "Challenge 84: Infected"
|
||||
challengeType: 28
|
||||
dashedName: challenge-84
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On November 2nd, 1988, the first major internet worm was released, infecting about 10% of computers connected to the internet after only a day.
|
||||
|
||||
In this challenge, you are given a number of days that have passed since an internet worm was released, and you need to determine how many computers are infected using the following rules:
|
||||
|
||||
- On day 0, the first computer is infected.
|
||||
- Each subsequent day, the number of infected computers doubles.
|
||||
- Every 3rd day, a patch is applied after the virus spreads and reduces the number of infected computers by 20%. Round the number of patched computers up to the nearest whole number.
|
||||
|
||||
For example, on:
|
||||
|
||||
- Day 0: 1 total computer is infected.
|
||||
- Day 1: 2 total computers are infected.
|
||||
- Day 2: 4 total computers are infected.
|
||||
- Day 3: 8 total computers are infected. Then, apply the patch: 8 infected * 20% = 1.6 patched. Round 1.6 up to 2. 8 computers infected - 2 patched = 6 total computers infected after day 3.
|
||||
|
||||
Return the number of total infected computers after the given amount of days have passed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`infected(1)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(infected(1), 2);
|
||||
```
|
||||
|
||||
`infected(3)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.equal(infected(3), 6);
|
||||
```
|
||||
|
||||
`infected(8)` should return `152`.
|
||||
|
||||
```js
|
||||
assert.equal(infected(8), 152);
|
||||
```
|
||||
|
||||
`infected(17)` should return `39808`.
|
||||
|
||||
```js
|
||||
assert.equal(infected(17), 39808);
|
||||
```
|
||||
|
||||
`infected(25)` should return `5217638`.
|
||||
|
||||
```js
|
||||
assert.equal(infected(25), 5217638);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function infected(days) {
|
||||
|
||||
return days;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function infected(days) {
|
||||
let infected = 1;
|
||||
|
||||
for (let day = 1; day <= days; day++) {
|
||||
infected *= 2;
|
||||
|
||||
if (day % 3 === 0) {
|
||||
let patched = Math.ceil(infected * 0.2);
|
||||
infected -= patched;
|
||||
}
|
||||
}
|
||||
|
||||
return infected;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a4
|
||||
title: "Challenge 85: Word Counter"
|
||||
challengeType: 28
|
||||
dashedName: challenge-85
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a sentence string, return the number of words that are in the sentence.
|
||||
|
||||
- Words are any sequence of non-space characters and are separated by a single space.
|
||||
|
||||
# --hints--
|
||||
|
||||
`countWords("Hello world")` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(countWords("Hello world"), 2);
|
||||
```
|
||||
|
||||
`countWords("The quick brown fox jumps over the lazy dog.")` should return `9`.
|
||||
|
||||
```js
|
||||
assert.equal(countWords("The quick brown fox jumps over the lazy dog."), 9);
|
||||
```
|
||||
|
||||
`countWords("I like coding challenges!")` should return `4`.
|
||||
|
||||
```js
|
||||
assert.equal(countWords("I like coding challenges!"), 4);
|
||||
```
|
||||
|
||||
`countWords("Complete the challenge in JavaScript and Python.")` should return `7`.
|
||||
|
||||
```js
|
||||
assert.equal(countWords("Complete the challenge in JavaScript and Python."), 7);
|
||||
```
|
||||
|
||||
`countWords("The missing semi-colon crashed the entire internet.")` should return `7`.
|
||||
|
||||
```js
|
||||
assert.equal(countWords("The missing semi-colon crashed the entire internet."), 7);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function countWords(sentence) {
|
||||
|
||||
return sentence;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function countWords(sentence) {
|
||||
|
||||
return sentence.split(' ').length;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a5
|
||||
title: "Challenge 86: Image Search"
|
||||
challengeType: 28
|
||||
dashedName: challenge-86
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On November 4th, 2001, Google launched its image search, allowing people to find images using search terms. In this challenge, you will imitate the image search.
|
||||
|
||||
Given an array of image names and a search term, return an array of image names containing the search term.
|
||||
|
||||
- Ignore the case when matching the search terms.
|
||||
- Return the images in the same order they appear in the input array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`imageSearch(["dog.png", "cat.jpg", "parrot.jpeg"], "dog")` should return `["dog.png"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(imageSearch(["dog.png", "cat.jpg", "parrot.jpeg"], "dog"), ["dog.png"]);
|
||||
```
|
||||
|
||||
`imageSearch(["Sunset.jpg", "Beach.png", "sunflower.jpeg"], "sun")` should return `["Sunset.jpg", "sunflower.jpeg"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(imageSearch(["Sunset.jpg", "Beach.png", "sunflower.jpeg"], "sun"), ["Sunset.jpg", "sunflower.jpeg"]);
|
||||
```
|
||||
|
||||
`imageSearch(["Moon.png", "sun.jpeg", "stars.png"], "PNG")` should return `["Moon.png", "stars.png"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(imageSearch(["Moon.png", "sun.jpeg", "stars.png"], "PNG"), ["Moon.png", "stars.png"]);
|
||||
```
|
||||
|
||||
`imageSearch(["cat.jpg", "dogToy.jpeg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"], "Cat")` should return `["cat.jpg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(imageSearch(["cat.jpg", "dogToy.jpeg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"], "Cat"), ["cat.jpg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function imageSearch(images, term) {
|
||||
|
||||
return images;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function imageSearch(images, term) {
|
||||
const lowerTerm = term.toLowerCase();
|
||||
return images.filter(img => img.toLowerCase().includes(lowerTerm));
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a6
|
||||
title: "Challenge 87: Matrix Builder"
|
||||
challengeType: 28
|
||||
dashedName: challenge-87
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two integers (a number of rows and a number of columns), return a matrix (an array of arrays) filled with zeros (`0`) of the given size.
|
||||
|
||||
For example, given `2` and `3`, return:
|
||||
|
||||
```json
|
||||
[
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]
|
||||
]
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
`buildMatrix(2, 3)` should return `[[0, 0, 0], [0, 0, 0]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(buildMatrix(2, 3), [[0, 0, 0], [0, 0, 0]]);
|
||||
```
|
||||
|
||||
`buildMatrix(3, 2)` should return `[[0, 0], [0, 0], [0, 0]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(buildMatrix(3, 2), [[0, 0], [0, 0], [0, 0]]);
|
||||
```
|
||||
|
||||
`buildMatrix(4, 3)` should return `[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(buildMatrix(4, 3), [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]);
|
||||
```
|
||||
|
||||
`buildMatrix(9, 1)` should return `[[0], [0], [0], [0], [0], [0], [0], [0], [0]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(buildMatrix(9, 1), [[0], [0], [0], [0], [0], [0], [0], [0], [0]]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function buildMatrix(rows, cols) {
|
||||
|
||||
return rows;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function buildMatrix(rows, cols) {
|
||||
const matrix = [];
|
||||
for (let i = 0; i < rows; i++) {
|
||||
const row = new Array(cols).fill(0);
|
||||
matrix.push(row);
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a7
|
||||
title: "Challenge 88: Weekday Finder"
|
||||
challengeType: 28
|
||||
dashedName: challenge-88
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string date in the format `YYYY-MM-DD`, return the day of the week.
|
||||
|
||||
Valid return days are:
|
||||
|
||||
- `"Sunday"`
|
||||
- `"Monday"`
|
||||
- `"Tuesday"`
|
||||
- `"Wednesday"`
|
||||
- `"Thursday"`
|
||||
- `"Friday"`
|
||||
- `"Saturday"`
|
||||
|
||||
Be sure to ignore time zones.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getWeekday("2025-11-06")` should return `Thursday`.
|
||||
|
||||
```js
|
||||
assert.equal(getWeekday("2025-11-06"), "Thursday");
|
||||
```
|
||||
|
||||
`getWeekday("1999-12-31")` should return `Friday`.
|
||||
|
||||
```js
|
||||
assert.equal(getWeekday("1999-12-31"), "Friday");
|
||||
```
|
||||
|
||||
`getWeekday("1111-11-11")` should return `Saturday`.
|
||||
|
||||
```js
|
||||
assert.equal(getWeekday("1111-11-11"), "Saturday");
|
||||
```
|
||||
|
||||
`getWeekday("2112-12-21")` should return `Wednesday`.
|
||||
|
||||
```js
|
||||
assert.equal(getWeekday("2112-12-21"), "Wednesday");
|
||||
```
|
||||
|
||||
`getWeekday("2345-10-01")` should return `Monday`.
|
||||
|
||||
```js
|
||||
assert.equal(getWeekday("2345-10-01"), "Monday");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getWeekday(dateString) {
|
||||
|
||||
return dateString;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getWeekday(dateString) {
|
||||
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
||||
const [year, month, day] = dateString.split("-").map(Number);
|
||||
const date = new Date(Date.UTC(year, month - 1, day));
|
||||
|
||||
return days[date.getUTCDay()];
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a8
|
||||
title: "Challenge 89: Counting Cards"
|
||||
challengeType: 28
|
||||
dashedName: challenge-89
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A standard deck of playing cards has 13 unique cards in each suit. Given an integer representing the number of cards to pick from the deck, return the number of unique combinations of cards you can pick.
|
||||
|
||||
- Order does not matter. Picking card A then card B is the same as picking card B then card A.
|
||||
|
||||
For example, given `52`, return `1`. There's only one combination of 52 cards to pick from a 52 card deck. And given `2`, return `1326`, There's 1326 card combinations you can end up with when picking 2 cards from the deck.
|
||||
|
||||
# --hints--
|
||||
|
||||
`combinations(52)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(combinations(52), 1);
|
||||
```
|
||||
|
||||
`combinations(1)` should return `52`.
|
||||
|
||||
```js
|
||||
assert.equal(combinations(1), 52);
|
||||
```
|
||||
|
||||
`combinations(2)` should return `1326`.
|
||||
|
||||
```js
|
||||
assert.equal(combinations(2), 1326);
|
||||
```
|
||||
|
||||
`combinations(5)` should return `2598960`.
|
||||
|
||||
```js
|
||||
assert.equal(combinations(5), 2598960);
|
||||
```
|
||||
|
||||
`combinations(10)` should return `15820024220`.
|
||||
|
||||
```js
|
||||
assert.equal(combinations(10), 15820024220);
|
||||
```
|
||||
|
||||
`combinations(50)` should return `1326`.
|
||||
|
||||
```js
|
||||
assert.equal(combinations(50), 1326);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function combinations(cards) {
|
||||
|
||||
return cards;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function combinations(cards) {
|
||||
const n = 52;
|
||||
|
||||
function factorial(x) {
|
||||
let result = 1;
|
||||
for (let i = 2; i <= x; i++) {
|
||||
result *= i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return factorial(n) / (factorial(cards) * factorial(n - cards));
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,86 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd62
|
||||
title: "Challenge 80: Email Sorter"
|
||||
challengeType: 29
|
||||
dashedName: challenge-80
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On October 29, 1971, the first email ever was sent, introducing the `username@domain` format we still use. Now, there are billions of email addresses.
|
||||
|
||||
In this challenge, you are given a list of email addresses and need to sort them alphabetically by domain name first (the part after the `@`), and username second (the part before the `@`).
|
||||
|
||||
- Sorting should be case-insensitive.
|
||||
- If more than one email has the same domain, sort them by their username.
|
||||
- Return an array of the sorted addresses.
|
||||
- Returned addresses should retain their original case.
|
||||
|
||||
For example, given `["jill@mail.com", "john@example.com", "jane@example.com"]`, return `["jane@example.com", "john@example.com", "jill@mail.com"]`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sort(["jill@mail.com", "john@example.com", "jane@example.com"])` should return `["jane@example.com", "john@example.com", "jill@mail.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["jill@mail.com", "john@example.com", "jane@example.com"]), ["jane@example.com", "john@example.com", "jill@mail.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["bob@mail.com", "alice@zoo.com", "carol@mail.com"])` should return `["bob@mail.com", "carol@mail.com", "alice@zoo.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["bob@mail.com", "alice@zoo.com", "carol@mail.com"]), ["bob@mail.com", "carol@mail.com", "alice@zoo.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["user@z.com", "user@y.com", "user@x.com"])` should return `["user@x.com", "user@y.com", "user@z.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["user@z.com", "user@y.com", "user@x.com"]), ["user@x.com", "user@y.com", "user@z.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["sam@MAIL.com", "amy@mail.COM", "bob@Mail.com"])` should return `["amy@mail.COM", "bob@Mail.com", "sam@MAIL.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["sam@MAIL.com", "amy@mail.COM", "bob@Mail.com"]), ["amy@mail.COM", "bob@Mail.com", "sam@MAIL.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort(["simon@beta.com", "sammy@alpha.com", "Sarah@Alpha.com", "SAM@ALPHA.com", "Simone@Beta.com", "sara@alpha.com"])` should return `["SAM@ALPHA.com", "sammy@alpha.com", "sara@alpha.com", "Sarah@Alpha.com", "simon@beta.com", "Simone@Beta.com"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort(["simon@beta.com", "sammy@alpha.com", "Sarah@Alpha.com", "SAM@ALPHA.com", "Simone@Beta.com", "sara@alpha.com"]), ["SAM@ALPHA.com", "sammy@alpha.com", "sara@alpha.com", "Sarah@Alpha.com", "simon@beta.com", "Simone@Beta.com"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def sort(emails):
|
||||
|
||||
return emails
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def sort(emails):
|
||||
return sorted(
|
||||
emails,
|
||||
key=lambda email: (email.split('@')[1].lower(), email.split('@')[0].lower())
|
||||
)
|
||||
```
|
||||
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd63
|
||||
title: "Challenge 81: Nth Prime"
|
||||
challengeType: 29
|
||||
dashedName: challenge-81
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A prime number is a positive integer greater than 1 that is divisible only by 1 and itself. The first five prime numbers are `2`, `3`, `5`, `7`, and `11`.
|
||||
|
||||
Given a positive integer `n`, return the `n`th prime number. For example, given `5` return the 5th prime number: `11`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`nth_prime(5)` should return `11`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(5), 11)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(10)` should return `29`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(10), 29)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(16)` should return `53`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(16), 53)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(99)` should return `523`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(99), 523)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`nth_prime(1000)` should return `7919`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(nth_prime(1000), 7919)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def nth_prime(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def nth_prime(n):
|
||||
primes = []
|
||||
num = 2
|
||||
|
||||
while len(primes) < n:
|
||||
is_prime = True
|
||||
for i in range(2, int(num**0.5) + 1):
|
||||
if num % i == 0:
|
||||
is_prime = False
|
||||
break
|
||||
if is_prime:
|
||||
primes.append(num)
|
||||
num += 1
|
||||
|
||||
return primes[n - 1]
|
||||
```
|
||||
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd64
|
||||
title: "Challenge 82: SpOoKy~CaSe"
|
||||
challengeType: 29
|
||||
dashedName: challenge-82
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing a variable name, convert it to "spooky case" using the following constraints:
|
||||
|
||||
- Replace all underscores (`_`), and hyphens (`-`) with a tilde (`~`).
|
||||
- Capitalize the first letter of the string, and every other letter after that, ignore the tilde character when counting.
|
||||
|
||||
For example, given `hello_world`, return `HeLlO~wOrLd`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`spookify("hello_world")` should return `"HeLlO~wOrLd"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("hello_world"), "HeLlO~wOrLd")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("Spooky_Case")` should return `"SpOoKy~CaSe"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("Spooky_Case"), "SpOoKy~CaSe")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("TRICK-or-TREAT")` should return `"TrIcK~oR~tReAt"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("TRICK-or-TREAT"), "TrIcK~oR~tReAt")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("c_a-n_d-y_-b-o_w_l")` should return `"C~a~N~d~Y~~b~O~w~L"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("c_a-n_d-y_-b-o_w_l"), "C~a~N~d~Y~~b~O~w~L")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`spookify("thE_hAUntEd-hOUsE-Is-fUll_Of_ghOsts")` should return `"ThE~hAuNtEd~HoUsE~iS~fUlL~oF~gHoStS"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(spookify("thE_hAUntEd-hOUsE-Is-fUll_Of_ghOsts"), "ThE~hAuNtEd~HoUsE~iS~fUlL~oF~gHoStS")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def spookify(boo):
|
||||
|
||||
return boo
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def spookify(boo):
|
||||
replaced = boo.replace("_", "~").replace("-", "~")
|
||||
|
||||
result = []
|
||||
capitalize = True
|
||||
|
||||
for char in replaced:
|
||||
if char == "~":
|
||||
result.append(char)
|
||||
else:
|
||||
result.append(char.upper() if capitalize else char.lower())
|
||||
capitalize = not capitalize
|
||||
|
||||
return "".join(result)
|
||||
```
|
||||
@@ -0,0 +1,111 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd65
|
||||
title: "Challenge 83: Signature Validation"
|
||||
challengeType: 29
|
||||
dashedName: challenge-83
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a message string, a secret key string, and a signature number, determine if the signature is valid using this encoding method:
|
||||
|
||||
- Letters in the message and secret key have these values:
|
||||
- `a` to `z` have values `1` to `26` respectively.
|
||||
- `A` to `Z` have values `27` to `52` respectively.
|
||||
- All other characters have no value.
|
||||
- Compute the signature by taking the sum of the message plus the sum of the secret key.
|
||||
|
||||
For example, given the message `"foo"` and the secret key `"bar"`, the signature would be `57`:
|
||||
|
||||
```md
|
||||
f (6) + o (15) + o (15) = 36
|
||||
b (2) + a (1) + r (18) = 21
|
||||
36 + 21 = 57
|
||||
```
|
||||
|
||||
Check if the computed signature matches the provided signature.
|
||||
|
||||
# --hints--
|
||||
|
||||
`verify("foo", "bar", 57)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("foo", "bar", 57), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("foo", "bar", 54)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("foo", "bar", 54), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("freeCodeCamp", "Rocks", 238)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("freeCodeCamp", "Rocks", 238), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("Is this valid?", "No", 210)` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("Is this valid?", "No", 210), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("Is this valid?", "Yes", 233)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("Is this valid?", "Yes", 233), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514)` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(verify("Check out the freeCodeCamp podcast,", "in the mobile app", 514), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def verify(message, key, signature):
|
||||
|
||||
return message
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def verify(message, key, signature):
|
||||
def charValue(ch) -> int:
|
||||
if 'a' <= ch <= 'z':
|
||||
return ord(ch) - ord('a') + 1
|
||||
elif 'A' <= ch <= 'Z':
|
||||
return ord(ch) - ord('A') + 27
|
||||
else:
|
||||
return 0
|
||||
|
||||
def compute_sum(s):
|
||||
return sum(charValue(ch) for ch in s)
|
||||
|
||||
total = compute_sum(message) + compute_sum(key)
|
||||
return total == signature
|
||||
```
|
||||
@@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 68e39ed6106dac2f0a98fd66
|
||||
title: "Challenge 84: Infected"
|
||||
challengeType: 29
|
||||
dashedName: challenge-84
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On November 2nd, 1988, the first major internet worm was released, infecting about 10% of computers connected to the internet after only a day.
|
||||
|
||||
In this challenge, you are given a number of days that have passed since an internet worm was released, and you need to determine how many computers are infected using the following rules:
|
||||
|
||||
- On day 0, the first computer is infected.
|
||||
- Each subsequent day, the number of infected computers doubles.
|
||||
- Every 3rd day, a patch is applied after the virus spreads and reduces the number of infected computers by 20%. Round the number of patched computers up to the nearest whole number.
|
||||
|
||||
For example, on:
|
||||
|
||||
- Day 0: 1 total computer is infected.
|
||||
- Day 1: 2 total computers are infected.
|
||||
- Day 2: 4 total computers are infected.
|
||||
- Day 3: 8 total computers are infected. Then, apply the patch: 8 infected * 20% = 1.6 patched. Round 1.6 up to 2. 8 computers infected - 2 patched = 6 total computers infected after day 3.
|
||||
|
||||
Return the number of total infected computers after the given amount of days have passed.
|
||||
|
||||
# --hints--
|
||||
|
||||
`infected(1)` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(1), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(3)` should return `6`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(3), 6)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(8)` should return `152`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(8), 152)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(17)` should return `39808`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(17), 39808)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`infected(25)` should return `5217638`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(infected(25), 5217638)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def infected(days):
|
||||
|
||||
return days
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import math
|
||||
def infected(days):
|
||||
infected = 1
|
||||
for day in range(1, days + 1):
|
||||
infected *= 2
|
||||
if day % 3 == 0:
|
||||
patched = math.ceil(infected * 0.2)
|
||||
infected -= patched
|
||||
|
||||
return infected
|
||||
```
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a4
|
||||
title: "Challenge 85: Word Counter"
|
||||
challengeType: 29
|
||||
dashedName: challenge-85
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a sentence string, return the number of words that are in the sentence.
|
||||
|
||||
- Words are any sequence of non-space characters and are separated by a single space.
|
||||
|
||||
# --hints--
|
||||
|
||||
`count_words("Hello world")` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("Hello world"), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("The quick brown fox jumps over the lazy dog.")` should return `9`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("The quick brown fox jumps over the lazy dog."), 9)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("I like coding challenges!")` should return `4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("I like coding challenges!"), 4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("Complete the challenge in JavaScript and Python.")` should return `7`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("Complete the challenge in JavaScript and Python."), 7)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`count_words("The missing semi-colon crashed the entire internet.")` should return `7`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(count_words("The missing semi-colon crashed the entire internet."), 7)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def count_words(sentence):
|
||||
|
||||
return sentence
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def count_words(sentence):
|
||||
|
||||
return len(sentence.split(' '))
|
||||
```
|
||||
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a5
|
||||
title: "Challenge 86: Image Search"
|
||||
challengeType: 29
|
||||
dashedName: challenge-86
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
On November 4th, 2001, Google launched its image search, allowing people to find images using search terms. In this challenge, you will imitate the image search.
|
||||
|
||||
Given an array of image names and a search term, return an array of image names containing the search term.
|
||||
|
||||
- Ignore the case when matching the search terms.
|
||||
- Return the images in the same order they appear in the input array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`image_search(["dog.png", "cat.jpg", "parrot.jpeg"], "dog")` should return `["dog.png"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["dog.png", "cat.jpg", "parrot.jpeg"], "dog"), ["dog.png"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`image_search(["Sunset.jpg", "Beach.png", "sunflower.jpeg"], "sun")` should return `["Sunset.jpg", "sunflower.jpeg"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["Sunset.jpg", "Beach.png", "sunflower.jpeg"], "sun"), ["Sunset.jpg", "sunflower.jpeg"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`image_search(["Moon.png", "sun.jpeg", "stars.png"], "PNG")` should return `["Moon.png", "stars.png"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["Moon.png", "sun.jpeg", "stars.png"], "PNG"), ["Moon.png", "stars.png"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`image_search(["cat.jpg", "dogToy.jpeg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"], "Cat")` should return `["cat.jpg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(image_search(["cat.jpg", "dogToy.jpeg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"], "Cat"), ["cat.jpg", "kitty-cat.png", "catNip.jpeg", "franken_cat.gif"])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def image_search(images, term):
|
||||
|
||||
return images
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def image_search(images, term):
|
||||
lower_term = term.lower()
|
||||
return [img for img in images if lower_term in img.lower()]
|
||||
```
|
||||
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a6
|
||||
title: "Challenge 87: Matrix Builder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-87
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two integers (a number of rows and a number of columns), return a matrix (an array of arrays) filled with zeros (`0`) of the given size.
|
||||
|
||||
For example, given `2` and `3`, return:
|
||||
|
||||
```json
|
||||
[
|
||||
[0, 0, 0],
|
||||
[0, 0, 0]
|
||||
]
|
||||
```
|
||||
|
||||
# --hints--
|
||||
|
||||
`build_matrix(2, 3)` should return `[[0, 0, 0], [0, 0, 0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(2, 3), [[0, 0, 0], [0, 0, 0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_matrix(3, 2)` should return `[[0, 0], [0, 0], [0, 0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(3, 2), [[0, 0], [0, 0], [0, 0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_matrix(4, 3)` should return `[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(4, 3), [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`build_matrix(9, 1)` should return `[[0], [0], [0], [0], [0], [0], [0], [0], [0]]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(build_matrix(9, 1), [[0], [0], [0], [0], [0], [0], [0], [0], [0]])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def build_matrix(rows, cols):
|
||||
|
||||
return rows
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def build_matrix(rows, cols):
|
||||
|
||||
return [[0 for _ in range(cols)] for _ in range(rows)]
|
||||
```
|
||||
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a7
|
||||
title: "Challenge 88: Weekday Finder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-88
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string date in the format `YYYY-MM-DD`, return the day of the week.
|
||||
|
||||
Valid return days are:
|
||||
|
||||
- `"Sunday"`
|
||||
- `"Monday"`
|
||||
- `"Tuesday"`
|
||||
- `"Wednesday"`
|
||||
- `"Thursday"`
|
||||
- `"Friday"`
|
||||
- `"Saturday"`
|
||||
|
||||
Be sure to ignore time zones.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_weekday("2025-11-06")` should return `Thursday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("2025-11-06"), "Thursday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("1999-12-31")` should return `Friday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("1999-12-31"), "Friday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("1111-11-11")` should return `Saturday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("1111-11-11"), "Saturday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("2112-12-21")` should return `Wednesday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("2112-12-21"), "Wednesday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_weekday("2345-10-01")` should return `Monday`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_weekday("2345-10-01"), "Monday")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_weekday(date_string):
|
||||
|
||||
return date_string
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import datetime
|
||||
def get_weekday(date_string):
|
||||
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
||||
year, month, day = map(int, date_string.split("-"))
|
||||
date = datetime.date(year, month, day)
|
||||
|
||||
return days[date.weekday() % 7 + 1 if date.weekday() != 6 else 0]
|
||||
```
|
||||
@@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 68ee9e3066cfd4eb2328e8a8
|
||||
title: "Challenge 89: Counting Cards"
|
||||
challengeType: 29
|
||||
dashedName: challenge-89
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
A standard deck of playing cards has 13 unique cards in each suit. Given an integer representing the number of cards to pick from the deck, return the number of unique combinations of cards you can pick.
|
||||
|
||||
- Order does not matter. Picking card A then card B is the same as picking card B then card A.
|
||||
|
||||
For example, given `52`, return `1`. There's only one combination of 52 cards to pick from a 52 card deck. And given `2`, return `1326`, There's 1326 card combinations you can end up with when picking 2 cards from the deck.
|
||||
|
||||
# --hints--
|
||||
|
||||
`combinations(52)` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(52), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(1)` should return `52`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(1), 52)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(2)` should return `1326`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(2), 1326)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(5)` should return `2598960`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(5), 2598960)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(10)` should return `15820024220`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(10), 15820024220)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`combinations(50)` should return `1326`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(combinations(50), 1326)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def combinations(cards):
|
||||
|
||||
return cards
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
from math import factorial
|
||||
def combinations(cards):
|
||||
n = 52
|
||||
|
||||
return factorial(n) // (factorial(cards) * factorial(n - cards))
|
||||
```
|
||||
@@ -322,6 +322,46 @@
|
||||
{
|
||||
"id": "68d30fc57588d97fd3027b30",
|
||||
"title": "Challenge 79: Navigator"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd62",
|
||||
"title": "Challenge 80: Email Sorter"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd63",
|
||||
"title": "Challenge 81: Nth Prime"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd64",
|
||||
"title": "Challenge 82: SpOoKy~CaSe"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd65",
|
||||
"title": "Challenge 83: Signature Validation"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd66",
|
||||
"title": "Challenge 84: Infected"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a4",
|
||||
"title": "Challenge 85: Word Counter"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a5",
|
||||
"title": "Challenge 86: Image Search"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a6",
|
||||
"title": "Challenge 87: Matrix Builder"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a7",
|
||||
"title": "Challenge 88: Weekday Finder"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a8",
|
||||
"title": "Challenge 89: Counting Cards"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -321,6 +321,46 @@
|
||||
{
|
||||
"id": "68d30fc57588d97fd3027b30",
|
||||
"title": "Challenge 79: Navigator"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd62",
|
||||
"title": "Challenge 80: Email Sorter"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd63",
|
||||
"title": "Challenge 81: Nth Prime"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd64",
|
||||
"title": "Challenge 82: SpOoKy~CaSe"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd65",
|
||||
"title": "Challenge 83: Signature Validation"
|
||||
},
|
||||
{
|
||||
"id": "68e39ed6106dac2f0a98fd66",
|
||||
"title": "Challenge 84: Infected"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a4",
|
||||
"title": "Challenge 85: Word Counter"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a5",
|
||||
"title": "Challenge 86: Image Search"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a6",
|
||||
"title": "Challenge 87: Matrix Builder"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a7",
|
||||
"title": "Challenge 88: Weekday Finder"
|
||||
},
|
||||
{
|
||||
"id": "68ee9e3066cfd4eb2328e8a8",
|
||||
"title": "Challenge 89: Counting Cards"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const { MONGOHQ_URL } = process.env;
|
||||
|
||||
// Number challenges in the dev-playground blocks
|
||||
// Update this if the number of challenges changes
|
||||
const EXPECTED_CHALLENGE_COUNT = 79;
|
||||
const EXPECTED_CHALLENGE_COUNT = 89;
|
||||
|
||||
// Date to set for the first challenge, second challenge will be one day later, etc...
|
||||
// **DO NOT CHANGE THIS AFTER RELEASE (if seeding production - okay for local dev)**
|
||||
|
||||
Reference in New Issue
Block a user