mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): daily challenges 246-263 (#66715)
This commit is contained in:
+1
-1
@@ -11,7 +11,7 @@ Given an integer `n`, return the `n`th row of Pascal's triangle as an array.
|
||||
|
||||
In Pascal's Triangle, each row begins and ends with 1, and each interior value is the sum of the two values directly above it.
|
||||
|
||||
Here's the first 5 rows of the triangle:
|
||||
Here are the first 5 rows of the triangle:
|
||||
|
||||
```js
|
||||
1
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a03
|
||||
title: "Challenge 246: Name Initials"
|
||||
challengeType: 28
|
||||
dashedName: challenge-246
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a full name as a string, return their initials.
|
||||
|
||||
- Names to initialize are separated by a space.
|
||||
- Initials should be made uppercase.
|
||||
- Initials should be separated by dots.
|
||||
|
||||
For example, `"Tommy Millwood"` returns `"T.M."`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getInitials("Tommy Millwood")` should return `"T.M."`.
|
||||
|
||||
```js
|
||||
assert.equal(getInitials("Tommy Millwood"), "T.M.");
|
||||
```
|
||||
|
||||
`getInitials("Savanna Puddlesplash")` should return `"S.P."`.
|
||||
|
||||
```js
|
||||
assert.equal(getInitials("Savanna Puddlesplash"), "S.P.");
|
||||
```
|
||||
|
||||
`getInitials("Frances Cowell Conrad")` should return `"F.C.C."`.
|
||||
|
||||
```js
|
||||
assert.equal(getInitials("Frances Cowell Conrad"), "F.C.C.");
|
||||
```
|
||||
|
||||
`getInitials("Dragon")` should return `"D."`.
|
||||
|
||||
```js
|
||||
assert.equal(getInitials("Dragon"), "D.");
|
||||
```
|
||||
|
||||
`getInitials("Dorothy Vera Clump Haverstock Norris")` should return `"D.V.C.H.N."`.
|
||||
|
||||
```js
|
||||
assert.equal(getInitials("Dorothy Vera Clump Haverstock Norris"), "D.V.C.H.N.");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getInitials(name) {
|
||||
|
||||
return name;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getInitials(name) {
|
||||
return name.split(' ').map(word => word[0].toUpperCase() + '.').join('');
|
||||
}
|
||||
```
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a04
|
||||
title: "Challenge 247: Last Letter"
|
||||
challengeType: 28
|
||||
dashedName: challenge-247
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return the letter from the string that appears last in the alphabet.
|
||||
|
||||
- If two or more letters tie for the last in the alphabet, return the first one.
|
||||
- Ignore all non-letter characters.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getLastLetter("world")` should return `"w"`.
|
||||
|
||||
```js
|
||||
assert.equal(getLastLetter("world"), "w");
|
||||
```
|
||||
|
||||
`getLastLetter("Hello World")` should return `"W"`.
|
||||
|
||||
```js
|
||||
assert.equal(getLastLetter("Hello World"), "W");
|
||||
```
|
||||
|
||||
`getLastLetter("The quick brown fox jumped over the lazy dog.")` should return `"z"`.
|
||||
|
||||
```js
|
||||
assert.equal(getLastLetter("The quick brown fox jumped over the lazy dog."), "z");
|
||||
```
|
||||
|
||||
`getLastLetter("HeLl0")` should return `"L"`.
|
||||
|
||||
```js
|
||||
assert.equal(getLastLetter("HeLl0"), "L");
|
||||
```
|
||||
|
||||
`getLastLetter("!#$ er@R asd fT.,> 2t0e9")` should return `"T"`.
|
||||
|
||||
```js
|
||||
assert.equal(getLastLetter("!#$ er@R asd fT.,> 2t0e9"), "T");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getLastLetter(str) {
|
||||
|
||||
return str;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getLastLetter(str) {
|
||||
const letters = str.split('').filter(char => /[a-zA-Z]/.test(char));
|
||||
return letters.reduce((last, char) => char.toLowerCase() > last.toLowerCase() ? char : last);
|
||||
}
|
||||
```
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a05
|
||||
title: "Challenge 248: Sorted Array Swap"
|
||||
challengeType: 28
|
||||
dashedName: challenge-248
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of integers, return a new array using the following rules:
|
||||
|
||||
1. Sort the integers in ascending order
|
||||
2. Then swap all values whose index is a multiple of 3 with the value before it.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sortAndSwap([3, 1, 2, 4, 6, 5])` should return `[1, 2, 4, 3, 5, 6]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sortAndSwap([3, 1, 2, 4, 6, 5]), [1, 2, 4, 3, 5, 6]);
|
||||
```
|
||||
|
||||
`sortAndSwap([9, 7, 5, 3, 1, 2, 4, 6, 8])` should return `[1, 2, 4, 3, 5, 7, 6, 8, 9]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sortAndSwap([9, 7, 5, 3, 1, 2, 4, 6, 8]), [1, 2, 4, 3, 5, 7, 6, 8, 9]);
|
||||
```
|
||||
|
||||
`sortAndSwap([1, 2, 3, 4, 5, 6, 7, 8, 9])` should return `[1, 2, 4, 3, 5, 7, 6, 8, 9]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sortAndSwap([1, 2, 3, 4, 5, 6, 7, 8, 9]), [1, 2, 4, 3, 5, 7, 6, 8, 9]);
|
||||
```
|
||||
|
||||
`sortAndSwap([12, 5, 8, 1, 3, 10, 2, 7, 6, 4, 9, 11])` should return `[1, 2, 4, 3, 5, 7, 6, 8, 10, 9, 11, 12]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sortAndSwap([12, 5, 8, 1, 3, 10, 2, 7, 6, 4, 9, 11]), [1, 2, 4, 3, 5, 7, 6, 8, 10, 9, 11, 12]);
|
||||
```
|
||||
|
||||
`sortAndSwap([100, -50, 0, 75, -25, 50, -75, 25])` should return `[-75, -50, 0, -25, 25, 75, 50, 100]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sortAndSwap([100, -50, 0, 75, -25, 50, -75, 25]), [-75, -50, 0, -25, 25, 75, 50, 100]);
|
||||
```
|
||||
|
||||
`sortAndSwap([5, 9, 13, 77, 88, 313, -10, -65, 0, 8, 99, 101, -4, 2])` should return `[-65, -10, 0, -4, 2, 8, 5, 9, 77, 13, 88, 101, 99, 313]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(sortAndSwap([5, 9, 13, 77, 88, 313, -10, -65, 0, 8, 99, 101, -4, 2]), [-65, -10, 0, -4, 2, 8, 5, 9, 77, 13, 88, 101, 99, 313]);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sortAndSwap(arr) {
|
||||
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sortAndSwap(arr) {
|
||||
const result = [...arr].sort((a, b) => a - b);
|
||||
|
||||
for (let i = 3; i < result.length; i += 3) {
|
||||
[result[i], result[i - 1]] = [result[i - 1], result[i]];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a06
|
||||
title: "Challenge 249: String Math"
|
||||
challengeType: 28
|
||||
dashedName: challenge-249
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string with numbers and other characters, perform math on the numbers based on the count of non-digit characters between the numbers.
|
||||
|
||||
- If the count of characters separating two numbers is even, use addition.
|
||||
- If it's odd, use subtraction.
|
||||
- Consecutive digits form a single number.
|
||||
- Operations are applied left to right.
|
||||
- Ignore leading and trailing characters that aren't digits.
|
||||
|
||||
For example, given `"3ab10c8"`, return `5`. Add 3 and 10 to get 13 because there's an even number of characters between them. Then subtract 8 from 13 because there's an odd number of characters between the result and 8.
|
||||
|
||||
# --hints--
|
||||
|
||||
`doMath("3ab10c8")` should return `5`.
|
||||
|
||||
```js
|
||||
assert.equal(doMath("3ab10c8"), 5);
|
||||
```
|
||||
|
||||
`doMath("6MINUS4")` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(doMath("6MINUS4"), 2);
|
||||
```
|
||||
|
||||
`doMath("9plus3")` should return `12`.
|
||||
|
||||
```js
|
||||
assert.equal(doMath("9plus3"), 12);
|
||||
```
|
||||
|
||||
`doMath("5fkwo#10i#%.<>15P=@20!#B/25")` should return `15`.
|
||||
|
||||
```js
|
||||
assert.equal(doMath("5fkwo#10i#%.<>15P=@20!#B/25"), 15);
|
||||
```
|
||||
|
||||
`doMath("a.67,1$lk6ldf34@#LD@]2d32d2'2l3,@l3L#@2gh35s09if=df#$t9sm49t0df3$^%[vc;:0:4mt")` should return `67`.
|
||||
|
||||
```js
|
||||
assert.equal(doMath("a.67,1$lk6ldf34@#LD@]2d32d2'2l3,@l3L#@2gh35s09if=df#$t9sm49t0df3$^%[vc;:0:4mt"), 67);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function doMath(str) {
|
||||
|
||||
return str;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function doMath(str) {
|
||||
const tokens = str.match(/(\d+)|(\D+)/g);
|
||||
let result = null;
|
||||
let pendingOp = null;
|
||||
|
||||
for (const token of tokens) {
|
||||
if (/^\d+$/.test(token)) {
|
||||
if (result === null) {
|
||||
result = parseInt(token);
|
||||
} else {
|
||||
result = pendingOp === 'add' ? result + parseInt(token) : result - parseInt(token);
|
||||
}
|
||||
} else {
|
||||
pendingOp = token.length % 2 === 0 ? 'add' : 'subtract';
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
```
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a07
|
||||
title: "Challenge 250: Hidden Key"
|
||||
challengeType: 28
|
||||
dashedName: challenge-250
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Welcome to the 250th daily challenge!
|
||||
|
||||
Given an encoded string, decode it using an encryption key and return it.
|
||||
|
||||
To find the key:
|
||||
|
||||
- Look at all daily challenges up to today whose challenge number is a multiple of 25 (including this one).
|
||||
- Take the first letter from each of those challenge titles and combine them into a string. If the title starts with a non-letter, find its first letter.
|
||||
|
||||
To decode the message, go over each letter in the encoded message and:
|
||||
|
||||
1. Look at the corresponding letter in the key (repeat the key if the message is longer than the key).
|
||||
2. Convert the key letter to its corresponding number: `"A"` = 1, `"B"` = 2, ..., `"Z"` = 26.
|
||||
3. Shift the encoded letter backward in the alphabet by that number.
|
||||
4. If the shift goes before `"A"`, wrap around to `"Z"`.
|
||||
|
||||
For example, if the encoded message starts with `"Y"` and the first key letter is `"V"` (22), shift `"Y"` back 22 places to get `"C"`. Repeat this process for each letter to decode the full message.
|
||||
|
||||
- Only letters are shifted, spaces are returned as-is.
|
||||
- All given and returned letters are uppercase.
|
||||
|
||||
# --hints--
|
||||
|
||||
`decode("YAVJYNXE")` should return `"CONGRATS"`.
|
||||
|
||||
```js
|
||||
assert.equal(decode("YAVJYNXE"), "CONGRATS");
|
||||
```
|
||||
|
||||
`decode("YALLUT PQUMJP")` should return `"CODING LEGEND"`.
|
||||
|
||||
```js
|
||||
assert.equal(decode("YALLUT PQUMJP"), "CODING LEGEND");
|
||||
```
|
||||
|
||||
`decode("UAC DYR EISAKYM")` should return `"YOU ARE AWESOME"`.
|
||||
|
||||
```js
|
||||
assert.equal(decode("UAC DYR EISAKYM"), "YOU ARE AWESOME");
|
||||
```
|
||||
|
||||
`decode("GQMS NBMZU")` should return `"KEEP GOING"`.
|
||||
|
||||
```js
|
||||
assert.equal(decode("GQMS NBMZU"), "KEEP GOING");
|
||||
```
|
||||
|
||||
`decode("W IQQURV UG I ZDMDTRV IVW JQDHY TMHSA QB")` should return `"A WINNER IS A DREAMER WHO NEVER GIVES UP"`.
|
||||
|
||||
```js
|
||||
assert.equal(decode("W IQQURV UG I ZDMDTRV IVW JQDHY TMHSA QB"), "A WINNER IS A DREAMER WHO NEVER GIVES UP");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function decode(message) {
|
||||
|
||||
return message;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function decode(message) {
|
||||
const key = "VLHCGMDLNH";
|
||||
let keyIndex = 0;
|
||||
return message.split('').map(char => {
|
||||
if (char === ' ') return char;
|
||||
const shift = key[keyIndex % key.length].charCodeAt(0) - 64;
|
||||
keyIndex++;
|
||||
return String.fromCharCode(((char.charCodeAt(0) - 65 - shift + 26) % 26) + 65);
|
||||
}).join('');
|
||||
}
|
||||
```
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a08
|
||||
title: "Challenge 251: Array Sum Finder"
|
||||
challengeType: 28
|
||||
dashedName: challenge-251
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of numbers and a target number, return the first subset of two or more numbers that adds up to the target.
|
||||
|
||||
- The "first" subset is the one whose elements have the lowest possible indices, prioritizing the earliest index first.
|
||||
- Each number in the array may only be used once.
|
||||
- If no valid subset exists, return `"Sum not found"`.
|
||||
|
||||
Return the matching numbers as an array in the order they appear in the original array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`findSum([1, 3, 5, 7], 6)` should return `[1, 5]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSum([1, 3, 5, 7], 6), [1, 5]);
|
||||
```
|
||||
|
||||
`findSum([1, 2, 3, 4, 5], 5)` should return `[1, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSum([1, 2, 3, 4, 5], 5), [1, 4]);
|
||||
```
|
||||
|
||||
`findSum([1, 2, 3, 4, 5], 6)` should return `[1, 2, 3]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSum([1, 2, 3, 4, 5], 6), [1, 2, 3]);
|
||||
```
|
||||
|
||||
`findSum([-1, -2, 3, 4], 1)` should return `[-1, -2, 4]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSum([-1, -2, 3, 4], 1), [-1, -2, 4]);
|
||||
```
|
||||
|
||||
`findSum([3, 1, 4, 1, 5, 9, 2, 6], 10)` should return `[3, 1, 4, 2]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSum([3, 1, 4, 1, 5, 9, 2, 6], 10), [3, 1, 4, 2]);
|
||||
```
|
||||
|
||||
`findSum([1, 2, 3, 4, 5, 6, 7, 8, 9], 20)` should return `[1, 2, 3, 5, 9]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(findSum([1, 2, 3, 4, 5, 6, 7, 8, 9], 20), [1, 2, 3, 5, 9]);
|
||||
```
|
||||
|
||||
`findSum([7, 9, 4, 2, 5], 10)` should return `"Sum not found"`.
|
||||
|
||||
```js
|
||||
assert.equal(findSum([7, 9, 4, 2, 5], 10), "Sum not found");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function findSum(arr, target) {
|
||||
|
||||
return arr;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function findSum(arr, target) {
|
||||
function search(start, remaining, current) {
|
||||
if (remaining === 0 && current.length >= 2) return current;
|
||||
if (start >= arr.length) return null;
|
||||
|
||||
for (let i = start; i < arr.length; i++) {
|
||||
const result = search(i + 1, remaining - arr[i], [...current, arr[i]]);
|
||||
if (result) return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
return search(0, target, []) ?? "Sum not found";
|
||||
}
|
||||
```
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a09
|
||||
title: "Challenge 252: Unique Stair Climber"
|
||||
challengeType: 28
|
||||
dashedName: challenge-252
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a number of stairs, return how many distinct ways someone can climb them taking either 1 or 2 steps at a time.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getUniqueClimbs(4)` should return `5`.
|
||||
|
||||
```js
|
||||
assert.equal(getUniqueClimbs(4), 5);
|
||||
```
|
||||
|
||||
`getUniqueClimbs(5)` should return `8`.
|
||||
|
||||
```js
|
||||
assert.equal(getUniqueClimbs(5), 8);
|
||||
```
|
||||
|
||||
`getUniqueClimbs(10)` should return `89`.
|
||||
|
||||
```js
|
||||
assert.equal(getUniqueClimbs(10), 89);
|
||||
```
|
||||
|
||||
`getUniqueClimbs(18)` should return `4181`.
|
||||
|
||||
```js
|
||||
assert.equal(getUniqueClimbs(18), 4181);
|
||||
```
|
||||
|
||||
`getUniqueClimbs(29)` should return `832040`.
|
||||
|
||||
```js
|
||||
assert.equal(getUniqueClimbs(29), 832040);
|
||||
```
|
||||
|
||||
`getUniqueClimbs(50)` should return `20365011074`.
|
||||
|
||||
```js
|
||||
assert.equal(getUniqueClimbs(50), 20365011074);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getUniqueClimbs(steps) {
|
||||
|
||||
return steps;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getUniqueClimbs(steps) {
|
||||
if (steps <= 0) return 0;
|
||||
if (steps === 1) return 1;
|
||||
if (steps === 2) return 2;
|
||||
|
||||
let prev2 = 1, prev1 = 2;
|
||||
for (let i = 3; i <= steps; i++) {
|
||||
[prev2, prev1] = [prev1, prev2 + prev1];
|
||||
}
|
||||
return prev1;
|
||||
}
|
||||
```
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8b7
|
||||
title: "Challenge 253: Acronym Finder"
|
||||
challengeType: 28
|
||||
dashedName: challenge-253
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing an acronym, return the full name of the organization it belongs to from the list below:
|
||||
|
||||
- `"National Avocado Storage Authority"`
|
||||
- `"Cats Infiltration Agency"`
|
||||
- `"Fluffy Beanbag Inspectors"`
|
||||
- `"Department Of Jelly"`
|
||||
- `"Wild Honey Organization"`
|
||||
- `"Eating Pancakes Administration"`
|
||||
|
||||
Each letter in the given acronym should match the first letter of each word in the organization it belongs to, in the same order.
|
||||
|
||||
# --hints--
|
||||
|
||||
`findOrg("NASA")` should return `"National Avocado Storage Authority"`.
|
||||
|
||||
```js
|
||||
assert.equal(findOrg("NASA"), "National Avocado Storage Authority");
|
||||
```
|
||||
|
||||
`findOrg("CIA")` should return `"Cats Infiltration Agency"`.
|
||||
|
||||
```js
|
||||
assert.equal(findOrg("CIA"), "Cats Infiltration Agency");
|
||||
```
|
||||
|
||||
`findOrg("FBI")` should return `"Fluffy Beanbag Inspectors"`.
|
||||
|
||||
```js
|
||||
assert.equal(findOrg("FBI"), "Fluffy Beanbag Inspectors");
|
||||
```
|
||||
|
||||
`findOrg("DOJ")` should return `"Department Of Jelly"`.
|
||||
|
||||
```js
|
||||
assert.equal(findOrg("DOJ"), "Department Of Jelly");
|
||||
```
|
||||
|
||||
`findOrg("WHO")` should return `"Wild Honey Organization"`.
|
||||
|
||||
```js
|
||||
assert.equal(findOrg("WHO"), "Wild Honey Organization");
|
||||
```
|
||||
|
||||
`findOrg("EPA")` should return `"Eating Pancakes Administration"`.
|
||||
|
||||
```js
|
||||
assert.equal(findOrg("EPA"), "Eating Pancakes Administration");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function findOrg(acronym) {
|
||||
|
||||
return acronym;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function findOrg(acronym) {
|
||||
const orgs = [
|
||||
"National Avocado Storage Authority",
|
||||
"Cats Infiltration Agency",
|
||||
"Fluffy Beanbag Inspectors",
|
||||
"Department Of Jelly",
|
||||
"Wild Honey Organization",
|
||||
"Eating Pancakes Administration"
|
||||
];
|
||||
|
||||
for (let org of orgs) {
|
||||
const initials = org
|
||||
.split(" ")
|
||||
.map(word => word[0])
|
||||
.join("");
|
||||
|
||||
if (initials.toUpperCase() === acronym.toUpperCase()) {
|
||||
return org;
|
||||
}
|
||||
}
|
||||
|
||||
return "not found";
|
||||
}
|
||||
```
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8b8
|
||||
title: "Challenge 254: Odd Words"
|
||||
challengeType: 28
|
||||
dashedName: challenge-254
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string of words, return only the words with an odd number of letters.
|
||||
|
||||
- Words in the given string will be separated by a single space.
|
||||
- Return the words separated by a single space.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getOddWords("This is a super good test")` should return `"a super"`.
|
||||
|
||||
```js
|
||||
assert.equal(getOddWords("This is a super good test"), "a super");
|
||||
```
|
||||
|
||||
`getOddWords("one two three four")` should return `"one two three"`.
|
||||
|
||||
```js
|
||||
assert.equal(getOddWords("one two three four"), "one two three");
|
||||
```
|
||||
|
||||
`getOddWords("banana split sundae with rainbow sprinkles on top")` should return `"split rainbow sprinkles top"`.
|
||||
|
||||
```js
|
||||
assert.equal(getOddWords("banana split sundae with rainbow sprinkles on top"), "split rainbow sprinkles top");
|
||||
```
|
||||
|
||||
`getOddWords("The quick brown fox jumped over the lazy river")` should return `"The quick brown fox the river"`.
|
||||
|
||||
```js
|
||||
assert.equal(getOddWords("The quick brown fox jumped over the lazy river"), "The quick brown fox the river");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getOddWords(str) {
|
||||
|
||||
return str;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getOddWords(str) {
|
||||
return str
|
||||
.split(" ")
|
||||
.filter(word => word.length % 2 === 1)
|
||||
.join(" ");
|
||||
}
|
||||
```
|
||||
+135
@@ -0,0 +1,135 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8b9
|
||||
title: "Challenge 255: Earth Day Cleanup Crew"
|
||||
challengeType: 28
|
||||
dashedName: challenge-255
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Today is Earth Day. Given an array of items you cleaned up, return your total cleanup score based on the rules below.
|
||||
|
||||
Given items will be one of:
|
||||
|
||||
| Item | Base Value |
|
||||
| - | - |
|
||||
| `"bottle"` | 10 |
|
||||
| `"can"` | 6 |
|
||||
| `"bag"` | 8 |
|
||||
| `"tire"` | 35 |
|
||||
| `"straw"` | 4 |
|
||||
| `"cardboard"` | 3 |
|
||||
| `"newspaper"` | 3 |
|
||||
| `"shoe"` | 12 |
|
||||
| `"electronics"` | 25 |
|
||||
| `"battery"` | 18 |
|
||||
| `"mattress"` | 38 |
|
||||
|
||||
A Rare item is represented as `["rare", value]`. For example, `["rare", 80]`. Rare items do not get a streak bonus.
|
||||
|
||||
- Streak bonus: If the same item appears consecutively, it gets increasing bonus points.
|
||||
- First consecutive occurrence: base value
|
||||
- Second: base value + 1
|
||||
- Third: base value + 2
|
||||
- etc.
|
||||
- Fifth Item Multiplier: Every fifth item collected gets a multiplier.
|
||||
- Fifth item: *2
|
||||
- Tenth item: *3
|
||||
- etc.
|
||||
|
||||
- Apply the multiplier after calculating any bonuses.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getCleanupScore(["bottle", "straw", "shoe", "battery"])` should return `44`.
|
||||
|
||||
```js
|
||||
assert.equal(getCleanupScore(["bottle", "straw", "shoe", "battery"]), 44);
|
||||
```
|
||||
|
||||
`getCleanupScore(["electronics", "straw", "newspaper", "bottle", "bag"])` should return `58`.
|
||||
|
||||
```js
|
||||
assert.equal(getCleanupScore(["electronics", "straw", "newspaper", "bottle", "bag"]), 58);
|
||||
```
|
||||
|
||||
`getCleanupScore(["shoe", "can", "can", "can", "bottle", "bottle", "straw", "straw", "straw"])` should return `79`.
|
||||
|
||||
```js
|
||||
assert.equal(getCleanupScore(["shoe", "can", "can", "can", "bottle", "bottle", "straw", "straw", "straw"]), 79);
|
||||
```
|
||||
|
||||
`getCleanupScore(["mattress", ["rare", 80], "tire", "tire", "tire", ["rare", 95]])` should return `358`.
|
||||
|
||||
```js
|
||||
assert.equal(getCleanupScore(["mattress", ["rare", 80], "tire", "tire", "tire", ["rare", 95]]), 358);
|
||||
```
|
||||
|
||||
`getCleanupScore(["bottle", "can", "can", "shoe", "shoe", ["rare", 56], "bottle", "bottle", "can", "can", "electronics", "bottle", ["rare", 48], "bottle", "can", "can", "can", "can", "can", "can", "can"])` should return `383`.
|
||||
|
||||
```js
|
||||
assert.equal(getCleanupScore(["bottle", "can", "can", "shoe", "shoe", ["rare", 56], "bottle", "bottle", "can", "can", "electronics", "bottle", ["rare", 48], "bottle", "can", "can", "can", "can", "can", "can", "can"]), 383);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getCleanupScore(items) {
|
||||
|
||||
return items;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getCleanupScore(items) {
|
||||
const values = {
|
||||
bottle: 10,
|
||||
can: 6,
|
||||
bag: 8,
|
||||
tire: 35,
|
||||
straw: 4,
|
||||
cardboard: 3,
|
||||
newspaper: 3,
|
||||
shoe: 12,
|
||||
electronics: 25,
|
||||
battery: 18,
|
||||
mattress: 38
|
||||
};
|
||||
|
||||
let total = 0;
|
||||
let prevItem = null;
|
||||
let streak = 0;
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
let item = items[i];
|
||||
let base;
|
||||
let isRare = Array.isArray(item) && item[0] === "rare";
|
||||
|
||||
if (isRare) {
|
||||
base = item[1];
|
||||
prevItem = null;
|
||||
streak = 0;
|
||||
} else {
|
||||
base = values[item];
|
||||
streak = item === prevItem ? streak + 1 : 0;
|
||||
prevItem = item;
|
||||
}
|
||||
|
||||
let points = base;
|
||||
if (!isRare) points += streak;
|
||||
|
||||
if ((i + 1) % 5 === 0) {
|
||||
const multiplier = (i + 1) / 5 + 1;
|
||||
points *= multiplier;
|
||||
}
|
||||
|
||||
total += points;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8ba
|
||||
title: "Challenge 256: Closest Time Direction"
|
||||
challengeType: 28
|
||||
dashedName: challenge-256
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two times, determine whether you can get from the first to the second faster by moving forward or backward.
|
||||
|
||||
- Times are given in 24-hour format (`"HH:MM"`)
|
||||
- The clock wraps around (23:59 goes to 00:00 when moving forward, and 00:00 goes to 23:59 when moving backwards)
|
||||
|
||||
Return:
|
||||
|
||||
- `"forward"` if moving forward is shorter
|
||||
- `"backward"` if moving backward is shorter
|
||||
- `"equal"` if both directions take the same amount of time
|
||||
|
||||
# --hints--
|
||||
|
||||
`getDirection("10:00", "12:00")` should return `"forward"`.
|
||||
|
||||
```js
|
||||
assert.equal(getDirection("10:00", "12:00"), "forward");
|
||||
```
|
||||
|
||||
`getDirection("11:00", "05:00")` should return `"backward"`.
|
||||
|
||||
```js
|
||||
assert.equal(getDirection("11:00", "05:00"), "backward");
|
||||
```
|
||||
|
||||
`getDirection("00:00", "12:00")` should return `"equal"`.
|
||||
|
||||
```js
|
||||
assert.equal(getDirection("00:00", "12:00"), "equal");
|
||||
```
|
||||
|
||||
`getDirection("15:45", "01:10")` should return `"forward"`.
|
||||
|
||||
```js
|
||||
assert.equal(getDirection("15:45", "01:10"), "forward");
|
||||
```
|
||||
|
||||
`getDirection("03:30", "19:50")` should return `"backward"`.
|
||||
|
||||
```js
|
||||
assert.equal(getDirection("03:30", "19:50"), "backward");
|
||||
```
|
||||
|
||||
`getDirection("06:30", "18:30")` should return `"equal"`.
|
||||
|
||||
```js
|
||||
assert.equal(getDirection("06:30", "18:30"), "equal");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getDirection(time1, time2) {
|
||||
|
||||
return time1;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getDirection(time1, time2) {
|
||||
function toMinutes(t) {
|
||||
const [h, m] = t.split(":").map(Number);
|
||||
return h * 60 + m;
|
||||
}
|
||||
|
||||
const start = toMinutes(time1);
|
||||
const end = toMinutes(time2);
|
||||
|
||||
const forward = (end - start + 1440) % 1440;
|
||||
const backward = (start - end + 1440) % 1440;
|
||||
|
||||
if (forward < backward) return "forward";
|
||||
if (backward < forward) return "backward";
|
||||
return "equal";
|
||||
}
|
||||
```
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8bb
|
||||
title: "Challenge 257: Word Compressor"
|
||||
challengeType: 28
|
||||
dashedName: challenge-257
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return a compressed version of the string using the following rules:
|
||||
|
||||
- The first occurrence of a word remains unchanged.
|
||||
- Subsequent occurrences are replaced with the position of the first occurrence, where the first word is at position 1.
|
||||
- Words are separated by a single space.
|
||||
|
||||
For example, given `"practice makes perfect and perfect practice makes perfect"`, return `"practice makes perfect and 3 1 2 3"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`compress("practice makes perfect and perfect practice makes perfect")` should return `"practice makes perfect and 3 1 2 3"`.
|
||||
|
||||
```js
|
||||
assert.equal(compress("practice makes perfect and perfect practice makes perfect"), "practice makes perfect and 3 1 2 3");
|
||||
```
|
||||
|
||||
`compress("hello hello hello")` should return `"hello 1 1"`.
|
||||
|
||||
```js
|
||||
assert.equal(compress("hello hello hello"), "hello 1 1");
|
||||
```
|
||||
|
||||
`compress("the cat sat on the mat on which the cat sat")` should return `"the cat sat on 1 mat 4 which 1 2 3"`.
|
||||
|
||||
```js
|
||||
assert.equal(compress("the cat sat on the mat on which the cat sat"), "the cat sat on 1 mat 4 which 1 2 3");
|
||||
```
|
||||
|
||||
`compress("the more you know the more you realize you don't know")` should return `"the more you know 1 2 3 realize 3 don't 4"`.
|
||||
|
||||
```js
|
||||
assert.equal(compress("the more you know the more you realize you don't know"), "the more you know 1 2 3 realize 3 don't 4");
|
||||
```
|
||||
|
||||
`compress("lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero")` should return `"lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10"`.
|
||||
|
||||
```js
|
||||
assert.equal(compress("lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero"), "lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function compress(str) {
|
||||
|
||||
return str;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function compress(str) {
|
||||
const words = str.split(" ");
|
||||
const seen = {};
|
||||
|
||||
return words.map((word, i) => {
|
||||
if (seen[word] !== undefined) {
|
||||
return seen[word];
|
||||
}
|
||||
seen[word] = i + 1;
|
||||
return word;
|
||||
}).join(" ");
|
||||
}
|
||||
```
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8bc
|
||||
title: "Challenge 258: Word Decompressor"
|
||||
challengeType: 28
|
||||
dashedName: challenge-258
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a compressed string, return the decompressed version using the following rules:
|
||||
|
||||
- The given string is made up of words and numbers separated by spaces.
|
||||
- Leave the words unchanged.
|
||||
- Replace numbers with the word at that position, where the first word is at position 1.
|
||||
|
||||
For example, given `"practice makes perfect and 3 1 2 3"`, return `"practice makes perfect and perfect practice makes perfect"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`decompress("practice makes perfect and 3 1 2 3")` should return `"practice makes perfect and perfect practice makes perfect"`.
|
||||
|
||||
```js
|
||||
assert.equal(decompress("practice makes perfect and 3 1 2 3"), "practice makes perfect and perfect practice makes perfect");
|
||||
```
|
||||
|
||||
`decompress("hello 1 1")` should return `"hello hello hello"`.
|
||||
|
||||
```js
|
||||
assert.equal(decompress("hello 1 1"), "hello hello hello");
|
||||
```
|
||||
|
||||
`decompress("the cat sat on 1 mat 4 which 1 2 3")` should return `"the cat sat on the mat on which the cat sat"`.
|
||||
|
||||
```js
|
||||
assert.equal(decompress("the cat sat on 1 mat 4 which 1 2 3"), "the cat sat on the mat on which the cat sat");
|
||||
```
|
||||
|
||||
`decompress("the more you know 1 2 3 realize 3 don't 4")` should return `"the more you know the more you realize you don't know"`.
|
||||
|
||||
```js
|
||||
assert.equal(decompress("the more you know 1 2 3 realize 3 don't 4"), "the more you know the more you realize you don't know");
|
||||
```
|
||||
|
||||
`decompress("lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10")` should return `"lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero"`.
|
||||
|
||||
```js
|
||||
assert.equal(decompress("lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10"), "lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function decompress(str) {
|
||||
|
||||
return str;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function decompress(str) {
|
||||
const tokens = str.split(" ");
|
||||
const words = [];
|
||||
|
||||
for (const token of tokens) {
|
||||
if (isNaN(parseInt(token))) {
|
||||
words.push(token)
|
||||
} else {
|
||||
words.push(tokens[token - 1])
|
||||
}
|
||||
}
|
||||
|
||||
return words.join(" ")
|
||||
}
|
||||
```
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8bd
|
||||
title: "Challenge 259: FizzBuzz Explosion"
|
||||
challengeType: 28
|
||||
dashedName: challenge-259
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer, return the number of steps it takes to turn the word `"fizzbuzz"` into a string with at least the given number of `"z"`'s using the following rules:
|
||||
|
||||
- Start with the string `"fizzbuzz"`.
|
||||
- Each step, apply the standard FizzBuzz rules using the letter position in the string (the first `"f"` is position 1).
|
||||
- If the letter position is divisible by 3, replace the letter with `"fizz"`
|
||||
- If it's divisible by 5, replace the letter with `"buzz"`
|
||||
- If it's divisible by 3 and 5, replace the letter with `"fizzbuzz"`
|
||||
|
||||
So after 1 step, `"fizzbuzz"` turns into `"fifizzzbuzzfizzzz"`, which has 9 `"z"`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
`explodeFizzbuzz(9)` should return `1`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(9), 1);
|
||||
```
|
||||
|
||||
`explodeFizzbuzz(15)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(15), 2);
|
||||
```
|
||||
|
||||
`explodeFizzbuzz(51)` should return `3`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(51), 3);
|
||||
```
|
||||
|
||||
`explodeFizzbuzz(52)` should return `4`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(52), 4);
|
||||
```
|
||||
|
||||
`explodeFizzbuzz(359)` should return `5`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(359), 5);
|
||||
```
|
||||
|
||||
`explodeFizzbuzz(789)` should return `6`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(789), 6);
|
||||
```
|
||||
|
||||
`explodeFizzbuzz(54482)` should return `11`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(54482), 11);
|
||||
```
|
||||
|
||||
`explodeFizzbuzz(1000000)` should return `14`.
|
||||
|
||||
```js
|
||||
assert.equal(explodeFizzbuzz(1000000), 14);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function explodeFizzbuzz(targetZCount) {
|
||||
|
||||
return targetZCount;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function explodeFizzbuzz(targetZCount) {
|
||||
let str = "fizzbuzz";
|
||||
let steps = 0;
|
||||
|
||||
while ((str.match(/z/g) || []).length < targetZCount) {
|
||||
let next = "";
|
||||
for (let i = 0; i < str.length; i++) {
|
||||
const pos = i + 1;
|
||||
if (pos % 15 === 0) next += "fizzbuzz";
|
||||
else if (pos % 3 === 0) next += "fizz";
|
||||
else if (pos % 5 === 0) next += "buzz";
|
||||
else next += str[i];
|
||||
}
|
||||
str = next;
|
||||
steps++;
|
||||
}
|
||||
|
||||
return steps;
|
||||
}
|
||||
```
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7926
|
||||
title: "Challenge 260: Word Score"
|
||||
challengeType: 28
|
||||
dashedName: challenge-260
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a word, return its score using a standard letter-value table:
|
||||
|
||||
| Letter | Value |
|
||||
| --- | --- |
|
||||
| A | 1 |
|
||||
| B | 2 |
|
||||
| ... | ... |
|
||||
| Z | 26 |
|
||||
|
||||
- Upper and lowercase letters have the same value.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getWordScore("hi")` should return `17`.
|
||||
|
||||
```js
|
||||
assert.equal(getWordScore("hi"), 17);
|
||||
```
|
||||
|
||||
`getWordScore("hello")` should return `52`.
|
||||
|
||||
```js
|
||||
assert.equal(getWordScore("hello"), 52);
|
||||
```
|
||||
|
||||
`getWordScore("hippopotamus")` should return `169`.
|
||||
|
||||
```js
|
||||
assert.equal(getWordScore("hippopotamus"), 169);
|
||||
```
|
||||
|
||||
`getWordScore("freeCodeCamp")` should return `94`.
|
||||
|
||||
```js
|
||||
assert.equal(getWordScore("freeCodeCamp"), 94);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getWordScore(word) {
|
||||
|
||||
return word;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getWordScore(word) {
|
||||
return word.toLowerCase().split('').reduce((sum, char) => {
|
||||
return sum + (char.charCodeAt(0) - 96);
|
||||
}, 0);
|
||||
}
|
||||
```
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7927
|
||||
title: "Challenge 261: Number Words"
|
||||
challengeType: 28
|
||||
dashedName: challenge-261
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer from 0 to 99, return its English word representation.
|
||||
|
||||
- `0` returns `"zero"`.
|
||||
- Numbers 1-19 have unique names (`"one"`, `"two"`, ..., `"ten"`, `"eleven"`, ..., `"eighteen"`, `"nineteen"`).
|
||||
- Multiples of 10 from 20-90 have their own names (`"twenty"`, `"thirty"`, ..., `"eighty"`, `"ninety"`).
|
||||
- Numbers 21-99 that are not multiples of 10 are written as two words joined by a hyphen. For example `"forty-two"` and `"fifty-three"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`getNumberWords(0)` should return `"zero"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(0), "zero");
|
||||
```
|
||||
|
||||
`getNumberWords(10)` should return `"ten"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(10), "ten");
|
||||
```
|
||||
|
||||
`getNumberWords(19)` should return `"nineteen"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(19), "nineteen");
|
||||
```
|
||||
|
||||
`getNumberWords(30)` should return `"thirty"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(30), "thirty");
|
||||
```
|
||||
|
||||
`getNumberWords(53)` should return `"fifty-three"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(53), "fifty-three");
|
||||
```
|
||||
|
||||
`getNumberWords(7)` should return `"seven"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(7), "seven");
|
||||
```
|
||||
|
||||
`getNumberWords(12)` should return `"twelve"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(12), "twelve");
|
||||
```
|
||||
|
||||
`getNumberWords(60)` should return `"sixty"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(60), "sixty");
|
||||
```
|
||||
|
||||
`getNumberWords(67)` should return `"sixty-seven"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(67), "sixty-seven");
|
||||
```
|
||||
|
||||
`getNumberWords(98)` should return `"ninety-eight"`.
|
||||
|
||||
```js
|
||||
assert.equal(getNumberWords(98), "ninety-eight");
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function getNumberWords(n) {
|
||||
|
||||
return n;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function getNumberWords(n) {
|
||||
const ones = ["zero","one","two","three","four","five","six","seven","eight","nine",
|
||||
"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen",
|
||||
"seventeen","eighteen","nineteen"];
|
||||
const tens = ["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"];
|
||||
|
||||
if (n < 20) return ones[n];
|
||||
if (n % 10 === 0) return tens[Math.floor(n / 10)];
|
||||
return tens[Math.floor(n / 10)] + "-" + ones[n % 10];
|
||||
}
|
||||
```
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7928
|
||||
title: "Challenge 262: URL Query Parser"
|
||||
challengeType: 28
|
||||
dashedName: challenge-262
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a URL that contains a query string, parse the query string into an object (or dictionary) of key-value pairs.
|
||||
|
||||
- The query string begins after the `"?"`,
|
||||
- each parameter is separated by `"&"`,
|
||||
- each key/value pair is separated by `"="`
|
||||
|
||||
For example, given `"https://example.com/search?name=Alice&age=30"`, return:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "Alice",
|
||||
"age": "30"
|
||||
}
|
||||
```
|
||||
|
||||
All values should be returned as strings.
|
||||
|
||||
# --hints--
|
||||
|
||||
`parseUrlQuery("https://example.com/search?name=Alice&age=30")` should return `{"name": "Alice", "age": "30"}`
|
||||
|
||||
```js
|
||||
assert.deepEqual(parseUrlQuery("https://example.com/search?name=Alice&age=30"), {"name": "Alice", "age": "30"});
|
||||
```
|
||||
|
||||
`parseUrlQuery("https://freecodecamp.org/learn?skill=programming&language=python")` should return `{"skill": "programming", "language": "python"}`
|
||||
|
||||
```js
|
||||
assert.deepEqual(parseUrlQuery("https://freecodecamp.org/learn?skill=programming&language=python"), {"skill": "programming", "language": "python"});
|
||||
```
|
||||
|
||||
`parseUrlQuery("https://freecodecamp.org/items?category=books&sort=asc&page=2")` should return `{"category": "books", "sort": "asc", "page": "2"}`
|
||||
|
||||
```js
|
||||
assert.deepEqual(parseUrlQuery("https://freecodecamp.org/items?category=books&sort=asc&page=2"), {"category": "books", "sort": "asc", "page": "2"});
|
||||
```
|
||||
|
||||
`parseUrlQuery("https://example.com?redirect=freecodecamp.org/learn&when=now")` should return `{"redirect": "freecodecamp.org/learn", "when": "now"}`
|
||||
|
||||
```js
|
||||
assert.deepEqual(parseUrlQuery("https://example.com?redirect=freecodecamp.org/learn&when=now"), {"redirect": "freecodecamp.org/learn", "when": "now"});
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function parseUrlQuery(url) {
|
||||
|
||||
return url;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function parseUrlQuery(url) {
|
||||
const queryString = url.split('?')[1];
|
||||
return Object.fromEntries(
|
||||
queryString.split('&').map(param => param.split('='))
|
||||
);
|
||||
}
|
||||
```
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7929
|
||||
title: "Challenge 263: Binary Crossword"
|
||||
challengeType: 28
|
||||
dashedName: challenge-263
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a character, determine if its 8-bit binary representation can be found in the following grid, horizontally or vertically in either direction:
|
||||
|
||||
```js
|
||||
0 1 0 0 0 0 0 1
|
||||
0 1 1 0 1 1 1 1
|
||||
0 1 0 0 0 1 0 0
|
||||
0 1 1 0 0 1 0 1
|
||||
0 1 0 1 0 0 1 0
|
||||
0 1 0 1 0 1 0 0
|
||||
0 1 1 0 1 0 0 0
|
||||
1 0 1 0 1 1 1 0
|
||||
```
|
||||
|
||||
For example, `"A"` has the binary representation `01000001`, which appears in the first row from left to right.
|
||||
|
||||
# --hints--
|
||||
|
||||
`isInCrossword("I")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(isInCrossword("I"));
|
||||
```
|
||||
|
||||
`isInCrossword("D")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(isInCrossword("D"));
|
||||
```
|
||||
|
||||
`isInCrossword("0")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(isInCrossword("0"));
|
||||
```
|
||||
|
||||
`isInCrossword("u")` should return `true`.
|
||||
|
||||
```js
|
||||
assert.isTrue(isInCrossword("u"));
|
||||
```
|
||||
|
||||
`isInCrossword("Y")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.isFalse(isInCrossword("Y"));
|
||||
```
|
||||
|
||||
`isInCrossword("p")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.isFalse(isInCrossword("p"));
|
||||
```
|
||||
|
||||
`isInCrossword("1")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.isFalse(isInCrossword("1"));
|
||||
```
|
||||
|
||||
`isInCrossword("Q")` should return `false`.
|
||||
|
||||
```js
|
||||
assert.isFalse(isInCrossword("Q"));
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function isInCrossword(char) {
|
||||
|
||||
return char;
|
||||
}
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function isInCrossword(char) {
|
||||
const grid = [
|
||||
"01000001",
|
||||
"01101111",
|
||||
"01000100",
|
||||
"01100101",
|
||||
"01010010",
|
||||
"01010100",
|
||||
"01101000",
|
||||
"10101110",
|
||||
];
|
||||
|
||||
const target = char.charCodeAt(0).toString(2).padStart(8, '0');
|
||||
const cols = Array.from({length: 8}, (_, c) => grid.map(r => r[c]).join(''));
|
||||
|
||||
return [...grid, ...cols].some(seq => seq.includes(target) || seq.split('').reverse().join('').includes(target));
|
||||
}
|
||||
```
|
||||
+1
-1
@@ -11,7 +11,7 @@ Given an integer `n`, return the `n`th row of Pascal's triangle as an array.
|
||||
|
||||
In Pascal's Triangle, each row begins and ends with 1, and each interior value is the sum of the two values directly above it.
|
||||
|
||||
Here's the first 5 rows of the triangle:
|
||||
Here are the first 5 rows of the triangle:
|
||||
|
||||
```js
|
||||
1
|
||||
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a03
|
||||
title: "Challenge 246: Name Initials"
|
||||
challengeType: 29
|
||||
dashedName: challenge-246
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a full name as a string, return their initials.
|
||||
|
||||
- Names to initialize are separated by a space.
|
||||
- Initials should be made uppercase.
|
||||
- Initials should be separated by dots.
|
||||
|
||||
For example, `"Tommy Millwood"` returns `"T.M."`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_initials("Tommy Millwood")` should return `"T.M."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_initials("Tommy Millwood"), "T.M.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_initials("Savanna Puddlesplash")` should return `"S.P."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_initials("Savanna Puddlesplash"), "S.P.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_initials("Frances Cowell Conrad")` should return `"F.C.C."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_initials("Frances Cowell Conrad"), "F.C.C.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_initials("Dragon")` should return `"D."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_initials("Dragon"), "D.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_initials("Dorothy Vera Clump Haverstock Norris")` should return `"D.V.C.H.N."`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_initials("Dorothy Vera Clump Haverstock Norris"), "D.V.C.H.N.")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_initials(name):
|
||||
|
||||
return name
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_initials(name):
|
||||
return ''.join(word[0].upper() + '.' for word in name.split())
|
||||
```
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a04
|
||||
title: "Challenge 247: Last Letter"
|
||||
challengeType: 29
|
||||
dashedName: challenge-247
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return the letter from the string that appears last in the alphabet.
|
||||
|
||||
- If two or more letters tie for the last in the alphabet, return the first one.
|
||||
- Ignore all non-letter characters.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_last_letter("world")` should return `"w"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_last_letter("world"), "w")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_last_letter("Hello World")` should return `"W"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_last_letter("Hello World"), "W")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_last_letter("The quick brown fox jumped over the lazy dog.")` should return `"z"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_last_letter("The quick brown fox jumped over the lazy dog."), "z")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_last_letter("HeLl0")` should return `"L"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_last_letter("HeLl0"), "L")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_last_letter("!#$ er@R asd fT.,> 2t0e9")` should return `"T"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_last_letter("!#$ er@R asd fT.,> 2t0e9"), "T")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_last_letter(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_last_letter(s):
|
||||
letters = [c for c in s if c.isalpha()]
|
||||
return max(letters, key=lambda c: c.lower())
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a05
|
||||
title: "Challenge 248: Sorted Array Swap"
|
||||
challengeType: 29
|
||||
dashedName: challenge-248
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of integers, return a new array using the following rules:
|
||||
|
||||
1. Sort the integers in ascending order
|
||||
2. Then swap all values whose index is a multiple of 3 with the value before it.
|
||||
|
||||
# --hints--
|
||||
|
||||
`sort_and_swap([3, 1, 2, 4, 6, 5])` should return `[1, 2, 4, 3, 5, 6]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort_and_swap([3, 1, 2, 4, 6, 5]), [1, 2, 4, 3, 5, 6])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort_and_swap([9, 7, 5, 3, 1, 2, 4, 6, 8])` should return `[1, 2, 4, 3, 5, 7, 6, 8, 9]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort_and_swap([9, 7, 5, 3, 1, 2, 4, 6, 8]), [1, 2, 4, 3, 5, 7, 6, 8, 9])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort_and_swap([1, 2, 3, 4, 5, 6, 7, 8, 9])` should return `[1, 2, 4, 3, 5, 7, 6, 8, 9]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort_and_swap([1, 2, 3, 4, 5, 6, 7, 8, 9]), [1, 2, 4, 3, 5, 7, 6, 8, 9])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort_and_swap([12, 5, 8, 1, 3, 10, 2, 7, 6, 4, 9, 11])` should return `[1, 2, 4, 3, 5, 7, 6, 8, 10, 9, 11, 12]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort_and_swap([12, 5, 8, 1, 3, 10, 2, 7, 6, 4, 9, 11]), [1, 2, 4, 3, 5, 7, 6, 8, 10, 9, 11, 12])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort_and_swap([100, -50, 0, 75, -25, 50, -75, 25])` should return `[-75, -50, 0, -25, 25, 75, 50, 100]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort_and_swap([100, -50, 0, 75, -25, 50, -75, 25]), [-75, -50, 0, -25, 25, 75, 50, 100])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`sort_and_swap([5, 9, 13, 77, 88, 313, -10, -65, 0, 8, 99, 101, -4, 2])` should return `[-65, -10, 0, -4, 2, 8, 5, 9, 77, 13, 88, 101, 99, 313]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(sort_and_swap([5, 9, 13, 77, 88, 313, -10, -65, 0, 8, 99, 101, -4, 2]), [-65, -10, 0, -4, 2, 8, 5, 9, 77, 13, 88, 101, 99, 313])`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def sort_and_swap(arr):
|
||||
|
||||
return arr
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def sort_and_swap(arr):
|
||||
result = sorted(arr)
|
||||
for i in range(3, len(result), 3):
|
||||
result[i], result[i - 1] = result[i - 1], result[i]
|
||||
return result
|
||||
```
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a06
|
||||
title: "Challenge 249: String Math"
|
||||
challengeType: 29
|
||||
dashedName: challenge-249
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string with numbers and other characters, perform math on the numbers based on the count of non-digit characters between the numbers.
|
||||
|
||||
- If the count of characters separating two numbers is even, use addition.
|
||||
- If it's odd, use subtraction.
|
||||
- Consecutive digits form a single number.
|
||||
- Operations are applied left to right.
|
||||
- Ignore leading and trailing characters that aren't digits.
|
||||
|
||||
For example, given `"3ab10c8"`, return `5`. Add 3 and 10 to get 13 because there's an even number of characters between them. Then subtract 8 from 13 because there's an odd number of characters between the result and 8.
|
||||
|
||||
# --hints--
|
||||
|
||||
`do_math("3ab10c8")` should return `5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(do_math("3ab10c8"), 5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`do_math("6MINUS4")` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(do_math("6MINUS4"), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`do_math("9plus3")` should return `12`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(do_math("9plus3"), 12)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`do_math("5fkwo#10i#%.<>15P=@20!#B/25")` should return `15`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(do_math("5fkwo#10i#%.<>15P=@20!#B/25"), 15)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`do_math("a.67,1$lk6ldf34@#LD@]2d32d2'2l3,@l3L#@2gh35s09if=df#$t9sm49t0df3$^%[vc;:0:4mt")` should return `67`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(do_math("a.67,1$lk6ldf34@#LD@]2d32d2'2l3,@l3L#@2gh35s09if=df#$t9sm49t0df3$^%[vc;:0:4mt"), 67)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def do_math(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
import re
|
||||
|
||||
def do_math(s):
|
||||
tokens = re.findall(r'\d+|\D+', s)
|
||||
result = None
|
||||
pending_op = None
|
||||
for token in tokens:
|
||||
if re.match(r'^\d+$', token):
|
||||
if result is None:
|
||||
result = int(token)
|
||||
elif pending_op == 'add':
|
||||
result += int(token)
|
||||
else:
|
||||
result -= int(token)
|
||||
else:
|
||||
pending_op = 'add' if len(token) % 2 == 0 else 'subtract'
|
||||
return result
|
||||
```
|
||||
+103
@@ -0,0 +1,103 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a07
|
||||
title: "Challenge 250: Hidden Key"
|
||||
challengeType: 29
|
||||
dashedName: challenge-250
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Welcome to the 250th daily challenge!
|
||||
|
||||
Given an encoded string, decode it using an encryption key and return it.
|
||||
|
||||
To find the key:
|
||||
|
||||
- Look at all daily challenges up to today whose challenge number is a multiple of 25 (including this one).
|
||||
- Take the first letter from each of those challenge titles and combine them into a string. If the title starts with a non-letter, find its first letter.
|
||||
|
||||
To decode the message, go over each letter in the encoded message and:
|
||||
|
||||
1. Look at the corresponding letter in the key (repeat the key if the message is longer than the key).
|
||||
2. Convert the key letter to its corresponding number: `"A"` = 1, `"B"` = 2, ..., `"Z"` = 26.
|
||||
3. Shift the encoded letter backward in the alphabet by that number.
|
||||
4. If the shift goes before `"A"`, wrap around to `"Z"`.
|
||||
|
||||
For example, if the encoded message starts with `"Y"` and the first key letter is `"V"` (22), shift `"Y"` back 22 places to get `"C"`. Repeat this process for each letter to decode the full message.
|
||||
|
||||
- Only letters are shifted, spaces are returned as-is.
|
||||
- All given and returned letters are uppercase.
|
||||
|
||||
# --hints--
|
||||
|
||||
`decode("YAVJYNXE")` should return `"CONGRATS"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("YAVJYNXE"), "CONGRATS")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("YALLUT PQUMJP")` should return `"CODING LEGEND"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("YALLUT PQUMJP"), "CODING LEGEND")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("UAC DYR EISAKYM")` should return `"YOU ARE AWESOME"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("UAC DYR EISAKYM"), "YOU ARE AWESOME")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("GQMS NBMZU")` should return `"KEEP GOING"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("GQMS NBMZU"), "KEEP GOING")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decode("W IQQURV UG I ZDMDTRV IVW JQDHY TMHSA QB")` should return `"A WINNER IS A DREAMER WHO NEVER GIVES UP"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decode("W IQQURV UG I ZDMDTRV IVW JQDHY TMHSA QB"), "A WINNER IS A DREAMER WHO NEVER GIVES UP")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def decode(message):
|
||||
|
||||
return message
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def decode(message):
|
||||
key = "VLHCGMDLNH"
|
||||
result = []
|
||||
key_index = 0
|
||||
for char in message:
|
||||
if char == ' ':
|
||||
result.append(char)
|
||||
else:
|
||||
shift = ord(key[key_index % len(key)]) - 64
|
||||
key_index += 1
|
||||
result.append(chr((ord(char) - 65 - shift) % 26 + 65))
|
||||
return ''.join(result)
|
||||
```
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a08
|
||||
title: "Challenge 251: Array Sum Finder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-251
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an array of numbers and a target number, return the first subset of two or more numbers that adds up to the target.
|
||||
|
||||
- The "first" subset is the one whose elements have the lowest possible indices, prioritizing the earliest index first.
|
||||
- Each number in the array may only be used once.
|
||||
- If no valid subset exists, return `"Sum not found"`.
|
||||
|
||||
Return the matching numbers as an array in the order they appear in the original array.
|
||||
|
||||
# --hints--
|
||||
|
||||
`find_sum([1, 3, 5, 7], 6)` should return `[1, 5]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_sum([1, 3, 5, 7], 6), [1, 5])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_sum([1, 2, 3, 4, 5], 5)` should return `[1, 4]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_sum([1, 2, 3, 4, 5], 5), [1, 4])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_sum([1, 2, 3, 4, 5], 6)` should return `[1, 2, 3]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_sum([1, 2, 3, 4, 5], 6), [1, 2, 3])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_sum([-1, -2, 3, 4], 1)` should return `[-1, -2, 4]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_sum([-1, -2, 3, 4], 1), [-1, -2, 4])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_sum([3, 1, 4, 1, 5, 9, 2, 6], 10)` should return `[3, 1, 4, 2]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_sum([3, 1, 4, 1, 5, 9, 2, 6], 10), [3, 1, 4, 2])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_sum([1, 2, 3, 4, 5, 6, 7, 8, 9], 20)` should return `[1, 2, 3, 5, 9]`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_sum([1, 2, 3, 4, 5, 6, 7, 8, 9], 20), [1, 2, 3, 5, 9])`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_sum([7, 9, 4, 2, 5], 10)` should return `"Sum not found"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_sum([7, 9, 4, 2, 5], 10), "Sum not found")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def find_sum(arr, target):
|
||||
|
||||
return arr
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def find_sum(arr, target):
|
||||
def search(start, remaining, current):
|
||||
if remaining == 0 and len(current) >= 2:
|
||||
return current
|
||||
if start >= len(arr):
|
||||
return None
|
||||
for i in range(start, len(arr)):
|
||||
result = search(i + 1, remaining - arr[i], current + [arr[i]])
|
||||
if result is not None:
|
||||
return result
|
||||
return None
|
||||
|
||||
return search(0, target, []) or "Sum not found"
|
||||
```
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
---
|
||||
id: 69bc6cb30c1d112a2e110a09
|
||||
title: "Challenge 252: Unique Stair Climber"
|
||||
challengeType: 29
|
||||
dashedName: challenge-252
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a number of stairs, return how many distinct ways someone can climb them taking either 1 or 2 steps at a time.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_unique_climbs(4)` should return `5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_unique_climbs(4), 5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_unique_climbs(5)` should return `8`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_unique_climbs(5), 8)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_unique_climbs(10)` should return `89`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_unique_climbs(10), 89)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_unique_climbs(18)` should return `4181`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_unique_climbs(18), 4181)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_unique_climbs(29)` should return `832040`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_unique_climbs(29), 832040)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_unique_climbs(50)` should return `20365011074`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_unique_climbs(50), 20365011074)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_unique_climbs(steps):
|
||||
|
||||
return steps
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_unique_climbs(steps):
|
||||
if steps <= 0:
|
||||
return 0
|
||||
if steps == 1:
|
||||
return 1
|
||||
if steps == 2:
|
||||
return 2
|
||||
prev2, prev1 = 1, 2
|
||||
for _ in range(3, steps + 1):
|
||||
prev2, prev1 = prev1, prev2 + prev1
|
||||
return prev1
|
||||
```
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8b7
|
||||
title: "Challenge 253: Acronym Finder"
|
||||
challengeType: 29
|
||||
dashedName: challenge-253
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string representing an acronym, return the full name of the organization it belongs to from the list below:
|
||||
|
||||
- `"National Avocado Storage Authority"`
|
||||
- `"Cats Infiltration Agency"`
|
||||
- `"Fluffy Beanbag Inspectors"`
|
||||
- `"Department Of Jelly"`
|
||||
- `"Wild Honey Organization"`
|
||||
- `"Eating Pancakes Administration"`
|
||||
|
||||
Each letter in the given acronym should match the first letter of each word in the organization it belongs to, in the same order.
|
||||
|
||||
# --hints--
|
||||
|
||||
`find_org("NASA")` should return `"National Avocado Storage Authority"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_org("NASA"), "National Avocado Storage Authority")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_org("CIA")` should return `"Cats Infiltration Agency"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_org("CIA"), "Cats Infiltration Agency")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_org("FBI")` should return `"Fluffy Beanbag Inspectors"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_org("FBI"), "Fluffy Beanbag Inspectors")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_org("DOJ")` should return `"Department Of Jelly"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_org("DOJ"), "Department Of Jelly")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_org("WHO")` should return `"Wild Honey Organization"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_org("WHO"), "Wild Honey Organization")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`find_org("EPA")` should return `"Eating Pancakes Administration"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(find_org("EPA"), "Eating Pancakes Administration")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def find_org(acronym):
|
||||
|
||||
return acronym
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def find_org(acronym):
|
||||
orgs = [
|
||||
"National Avocado Storage Authority",
|
||||
"Cats Infiltration Agency",
|
||||
"Fluffy Beanbag Inspectors",
|
||||
"Department Of Jelly",
|
||||
"Wild Honey Organization",
|
||||
"Eating Pancakes Administration",
|
||||
]
|
||||
for org in orgs:
|
||||
initials = ''.join(word[0] for word in org.split())
|
||||
if initials.upper() == acronym.upper():
|
||||
return org
|
||||
return "not found"
|
||||
```
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8b8
|
||||
title: "Challenge 254: Odd Words"
|
||||
challengeType: 29
|
||||
dashedName: challenge-254
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string of words, return only the words with an odd number of letters.
|
||||
|
||||
- Words in the given string will be separated by a single space.
|
||||
- Return the words separated by a single space.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_odd_words("This is a super good test")` should return `"a super"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_odd_words("This is a super good test"), "a super")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_odd_words("one two three four")` should return `"one two three"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_odd_words("one two three four"), "one two three")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_odd_words("banana split sundae with rainbow sprinkles on top")` should return `"split rainbow sprinkles top"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_odd_words("banana split sundae with rainbow sprinkles on top"), "split rainbow sprinkles top")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_odd_words("The quick brown fox jumped over the lazy river")` should return `"The quick brown fox the river"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_odd_words("The quick brown fox jumped over the lazy river"), "The quick brown fox the river")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_odd_words(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_odd_words(s):
|
||||
return ' '.join(word for word in s.split(' ') if len(word) % 2 == 1)
|
||||
```
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8b9
|
||||
title: "Challenge 255: Earth Day Cleanup Crew"
|
||||
challengeType: 29
|
||||
dashedName: challenge-255
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Today is Earth Day. Given an array of items you cleaned up, return your total cleanup score based on the rules below.
|
||||
|
||||
Given items will be one of:
|
||||
|
||||
| Item | Base Value |
|
||||
| - | - |
|
||||
| `"bottle"` | 10 |
|
||||
| `"can"` | 6 |
|
||||
| `"bag"` | 8 |
|
||||
| `"tire"` | 35 |
|
||||
| `"straw"` | 4 |
|
||||
| `"cardboard"` | 3 |
|
||||
| `"newspaper"` | 3 |
|
||||
| `"shoe"` | 12 |
|
||||
| `"electronics"` | 25 |
|
||||
| `"battery"` | 18 |
|
||||
| `"mattress"` | 38 |
|
||||
|
||||
A Rare item is represented as `["rare", value]`. For example, `["rare", 80]`. Rare items do not get a streak bonus.
|
||||
|
||||
- Streak bonus: If the same item appears consecutively, it gets increasing bonus points.
|
||||
- First consecutive occurrence: base value
|
||||
- Second: base value + 1
|
||||
- Third: base value + 2
|
||||
- etc.
|
||||
- Fifth Item Multiplier: Every fifth item collected gets a multiplier.
|
||||
- Fifth item: *2
|
||||
- Tenth item: *3
|
||||
- etc.
|
||||
|
||||
- Apply the multiplier after calculating any bonuses.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_cleanup_score(["bottle", "straw", "shoe", "battery"])` should return `44`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_cleanup_score(["bottle", "straw", "shoe", "battery"]), 44)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_cleanup_score(["electronics", "straw", "newspaper", "bottle", "bag"])` should return `58`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_cleanup_score(["electronics", "straw", "newspaper", "bottle", "bag"]), 58)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_cleanup_score(["shoe", "can", "can", "can", "bottle", "bottle", "straw", "straw", "straw"])` should return `79`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_cleanup_score(["shoe", "can", "can", "can", "bottle", "bottle", "straw", "straw", "straw"]), 79)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_cleanup_score(["mattress", ["rare", 80], "tire", "tire", "tire", ["rare", 95]])` should return `358`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_cleanup_score(["mattress", ["rare", 80], "tire", "tire", "tire", ["rare", 95]]), 358)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_cleanup_score(["bottle", "can", "can", "shoe", "shoe", ["rare", 56], "bottle", "bottle", "can", "can", "electronics", "bottle", ["rare", 48], "bottle", "can", "can", "can", "can", "can", "can", "can"])` should return `383`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_cleanup_score(["bottle", "can", "can", "shoe", "shoe", ["rare", 56], "bottle", "bottle", "can", "can", "electronics", "bottle", ["rare", 48], "bottle", "can", "can", "can", "can", "can", "can", "can"]), 383)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_cleanup_score(items):
|
||||
|
||||
return items
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_cleanup_score(items):
|
||||
values = {
|
||||
'bottle': 10, 'can': 6, 'bag': 8, 'tire': 35,
|
||||
'straw': 4, 'cardboard': 3, 'newspaper': 3,
|
||||
'shoe': 12, 'electronics': 25, 'battery': 18, 'mattress': 38
|
||||
}
|
||||
total = 0
|
||||
prev_item = None
|
||||
streak = 0
|
||||
for i, item in enumerate(items):
|
||||
is_rare = isinstance(item, list) and item[0] == 'rare'
|
||||
if is_rare:
|
||||
base = item[1]
|
||||
prev_item = None
|
||||
streak = 0
|
||||
else:
|
||||
base = values[item]
|
||||
streak = streak + 1 if item == prev_item else 0
|
||||
prev_item = item
|
||||
points = base if is_rare else base + streak
|
||||
if (i + 1) % 5 == 0:
|
||||
multiplier = (i + 1) // 5 + 1
|
||||
points *= multiplier
|
||||
total += points
|
||||
return total
|
||||
```
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8ba
|
||||
title: "Challenge 256: Closest Time Direction"
|
||||
challengeType: 29
|
||||
dashedName: challenge-256
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given two times, determine whether you can get from the first to the second faster by moving forward or backward.
|
||||
|
||||
- Times are given in 24-hour format (`"HH:MM"`)
|
||||
- The clock wraps around (23:59 goes to 00:00 when moving forward, and 00:00 goes to 23:59 when moving backwards)
|
||||
|
||||
Return:
|
||||
|
||||
- `"forward"` if moving forward is shorter
|
||||
- `"backward"` if moving backward is shorter
|
||||
- `"equal"` if both directions take the same amount of time
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_direction("10:00", "12:00")` should return `"forward"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_direction("10:00", "12:00"), "forward")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_direction("11:00", "05:00")` should return `"backward"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_direction("11:00", "05:00"), "backward")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_direction("00:00", "12:00")` should return `"equal"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_direction("00:00", "12:00"), "equal")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_direction("15:45", "01:10")` should return `"forward"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_direction("15:45", "01:10"), "forward")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_direction("03:30", "19:50")` should return `"backward"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_direction("03:30", "19:50"), "backward")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_direction("06:30", "18:30")` should return `"equal"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_direction("06:30", "18:30"), "equal")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_direction(time1, time2):
|
||||
|
||||
return time1
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_direction(time1, time2):
|
||||
def to_minutes(t):
|
||||
h, m = map(int, t.split(':'))
|
||||
return h * 60 + m
|
||||
|
||||
start = to_minutes(time1)
|
||||
end = to_minutes(time2)
|
||||
forward = (end - start + 1440) % 1440
|
||||
backward = (start - end + 1440) % 1440
|
||||
|
||||
if forward < backward:
|
||||
return 'forward'
|
||||
if backward < forward:
|
||||
return 'backward'
|
||||
return 'equal'
|
||||
```
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8bb
|
||||
title: "Challenge 257: Word Compressor"
|
||||
challengeType: 29
|
||||
dashedName: challenge-257
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a string, return a compressed version of the string using the following rules:
|
||||
|
||||
- The first occurrence of a word remains unchanged.
|
||||
- Subsequent occurrences are replaced with the position of the first occurrence, where the first word is at position 1.
|
||||
- Words are separated by a single space.
|
||||
|
||||
For example, given `"practice makes perfect and perfect practice makes perfect"`, return `"practice makes perfect and 3 1 2 3"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`compress("practice makes perfect and perfect practice makes perfect")` should return `"practice makes perfect and 3 1 2 3"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(compress("practice makes perfect and perfect practice makes perfect"), "practice makes perfect and 3 1 2 3")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`compress("hello hello hello")` should return `"hello 1 1"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(compress("hello hello hello"), "hello 1 1")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`compress("the cat sat on the mat on which the cat sat")` should return `"the cat sat on 1 mat 4 which 1 2 3"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(compress("the cat sat on the mat on which the cat sat"), "the cat sat on 1 mat 4 which 1 2 3")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`compress("the more you know the more you realize you don't know")` should return `"the more you know 1 2 3 realize 3 don't 4"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(compress("the more you know the more you realize you don't know"), "the more you know 1 2 3 realize 3 don't 4")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`compress("lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero")` should return `"lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(compress("lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero"), "lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def compress(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def compress(s):
|
||||
words = s.split(' ')
|
||||
seen = {}
|
||||
result = []
|
||||
for i, word in enumerate(words):
|
||||
if word in seen:
|
||||
result.append(str(seen[word]))
|
||||
else:
|
||||
seen[word] = i + 1
|
||||
result.append(word)
|
||||
return ' '.join(result)
|
||||
```
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8bc
|
||||
title: "Challenge 258: Word Decompressor"
|
||||
challengeType: 29
|
||||
dashedName: challenge-258
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a compressed string, return the decompressed version using the following rules:
|
||||
|
||||
- The given string is made up of words and numbers separated by spaces.
|
||||
- Leave the words unchanged.
|
||||
- Replace numbers with the word at that position, where the first word is at position 1.
|
||||
|
||||
For example, given `"practice makes perfect and 3 1 2 3"`, return `"practice makes perfect and perfect practice makes perfect"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`decompress("practice makes perfect and 3 1 2 3")` should return `"practice makes perfect and perfect practice makes perfect"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decompress("practice makes perfect and 3 1 2 3"), "practice makes perfect and perfect practice makes perfect")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decompress("hello 1 1")` should return `"hello hello hello"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decompress("hello 1 1"), "hello hello hello")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decompress("the cat sat on 1 mat 4 which 1 2 3")` should return `"the cat sat on the mat on which the cat sat"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decompress("the cat sat on 1 mat 4 which 1 2 3"), "the cat sat on the mat on which the cat sat")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decompress("the more you know 1 2 3 realize 3 don't 4")` should return `"the more you know the more you realize you don't know"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decompress("the more you know 1 2 3 realize 3 don't 4"), "the more you know the more you realize you don't know")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`decompress("lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10")` should return `"lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(decompress("lorem ipsum dolor sit per elit donec 4 nostra libero 5 7 ligula 4 gravida at 6 vitae a 6 sodales 7 en 7 16 3 nam 13 dignissim risus 16 13 5 27 2 2 15 23 6 5 2 13 23 15 5 21 4 16 27 1 4 5 10 23 2 6 4 21 4 30 6 30 2 6 16 15 18 23 29 27 4 18 sollicitudin 5 9 5 4 10"), "lorem ipsum dolor sit per elit donec sit nostra libero per donec ligula sit gravida at elit vitae a elit sodales donec en donec at dolor nam ligula dignissim risus at ligula per nam ipsum ipsum gravida en elit per ipsum ligula en gravida per sodales sit at nam lorem sit per libero en ipsum elit sit sodales sit risus elit risus ipsum elit at gravida vitae en dignissim nam sit vitae sollicitudin per nostra per sit libero")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def decompress(s):
|
||||
|
||||
return s
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def decompress(s):
|
||||
tokens = s.split(' ')
|
||||
words = []
|
||||
for token in tokens:
|
||||
try:
|
||||
idx = int(token)
|
||||
words.append(tokens[idx - 1])
|
||||
except ValueError:
|
||||
words.append(token)
|
||||
return ' '.join(words)
|
||||
```
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
---
|
||||
id: 69c5f3d787b1725d5f00c8bd
|
||||
title: "Challenge 259: FizzBuzz Explosion"
|
||||
challengeType: 29
|
||||
dashedName: challenge-259
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer, return the number of steps it takes to turn the word `"fizzbuzz"` into a string with at least the given number of `"z"`'s using the following rules:
|
||||
|
||||
- Start with the string `"fizzbuzz"`.
|
||||
- Each step, apply the standard FizzBuzz rules using the letter position in the string (the first `"f"` is position 1).
|
||||
- If the letter position is divisible by 3, replace the letter with `"fizz"`
|
||||
- If it's divisible by 5, replace the letter with `"buzz"`
|
||||
- If it's divisible by 3 and 5, replace the letter with `"fizzbuzz"`
|
||||
|
||||
So after 1 step, `"fizzbuzz"` turns into `"fifizzzbuzzfizzzz"`, which has 9 `"z"`'s.
|
||||
|
||||
# --hints--
|
||||
|
||||
`explode_fizzbuzz(9)` should return `1`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(9), 1)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`explode_fizzbuzz(15)` should return `2`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(15), 2)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`explode_fizzbuzz(51)` should return `3`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(51), 3)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`explode_fizzbuzz(52)` should return `4`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(52), 4)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`explode_fizzbuzz(359)` should return `5`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(359), 5)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`explode_fizzbuzz(789)` should return `6`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(789), 6)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`explode_fizzbuzz(54482)` should return `11`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(54482), 11)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`explode_fizzbuzz(1000000)` should return `14`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(explode_fizzbuzz(1000000), 14)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def explode_fizzbuzz(target_z_count):
|
||||
|
||||
return target_z_count
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def explode_fizzbuzz(target_z_count):
|
||||
s = "fizzbuzz"
|
||||
steps = 0
|
||||
while s.count('z') < target_z_count:
|
||||
next_s = ""
|
||||
for i, c in enumerate(s):
|
||||
pos = i + 1
|
||||
if pos % 15 == 0:
|
||||
next_s += "fizzbuzz"
|
||||
elif pos % 3 == 0:
|
||||
next_s += "fizz"
|
||||
elif pos % 5 == 0:
|
||||
next_s += "buzz"
|
||||
else:
|
||||
next_s += c
|
||||
s = next_s
|
||||
steps += 1
|
||||
return steps
|
||||
```
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7926
|
||||
title: "Challenge 260: Word Score"
|
||||
challengeType: 29
|
||||
dashedName: challenge-260
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a word, return its score using a standard letter-value table:
|
||||
|
||||
| Letter | Value |
|
||||
| --- | --- |
|
||||
| A | 1 |
|
||||
| B | 2 |
|
||||
| ... | ... |
|
||||
| Z | 26 |
|
||||
|
||||
- Upper and lowercase letters have the same value.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_word_score("hi")` should return `17`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_word_score("hi"), 17)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_word_score("hello")` should return `52`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_word_score("hello"), 52)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_word_score("hippopotamus")` should return `169`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_word_score("hippopotamus"), 169)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_word_score("freeCodeCamp")` should return `94`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_word_score("freeCodeCamp"), 94)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_word_score(word):
|
||||
|
||||
return word
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_word_score(word):
|
||||
return sum(ord(c) - 96 for c in word.lower())
|
||||
```
|
||||
+133
@@ -0,0 +1,133 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7927
|
||||
title: "Challenge 261: Number Words"
|
||||
challengeType: 29
|
||||
dashedName: challenge-261
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given an integer from 0 to 99, return its English word representation.
|
||||
|
||||
- `0` returns `"zero"`.
|
||||
- Numbers 1-19 have unique names (`"one"`, `"two"`, ..., `"ten"`, `"eleven"`, ..., `"eighteen"`, `"nineteen"`).
|
||||
- Multiples of 10 from 20-90 have their own names (`"twenty"`, `"thirty"`, ..., `"eighty"`, `"ninety"`).
|
||||
- Numbers 21-99 that are not multiples of 10 are written as two words joined by a hyphen. For example `"forty-two"` and `"fifty-three"`.
|
||||
|
||||
# --hints--
|
||||
|
||||
`get_number_words(0)` should return `"zero"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(0), "zero")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(10)` should return `"ten"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(10), "ten")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(19)` should return `"nineteen"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(19), "nineteen")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(30)` should return `"thirty"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(30), "thirty")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(53)` should return `"fifty-three"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(53), "fifty-three")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(7)` should return `"seven"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(7), "seven")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(12)` should return `"twelve"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(12), "twelve")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(60)` should return `"sixty"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(60), "sixty")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(67)` should return `"sixty-seven"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(67), "sixty-seven")`)
|
||||
}})
|
||||
```
|
||||
|
||||
`get_number_words(98)` should return `"ninety-eight"`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(get_number_words(98), "ninety-eight")`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def get_number_words(n):
|
||||
|
||||
return n
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def get_number_words(n):
|
||||
ones = ["zero","one","two","three","four","five","six","seven","eight","nine",
|
||||
"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen",
|
||||
"seventeen","eighteen","nineteen"]
|
||||
tens = ["","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"]
|
||||
|
||||
if n < 20:
|
||||
return ones[n]
|
||||
if n % 10 == 0:
|
||||
return tens[n // 10]
|
||||
return tens[n // 10] + "-" + ones[n % 10]
|
||||
```
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7928
|
||||
title: "Challenge 262: URL Query Parser"
|
||||
challengeType: 29
|
||||
dashedName: challenge-262
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a URL that contains a query string, parse the query string into an object (or dictionary) of key-value pairs.
|
||||
|
||||
- The query string begins after the `"?"`,
|
||||
- each parameter is separated by `"&"`,
|
||||
- each key/value pair is separated by `"="`
|
||||
|
||||
For example, given `"https://example.com/search?name=Alice&age=30"`, return:
|
||||
|
||||
```json
|
||||
{
|
||||
"name": "Alice",
|
||||
"age": "30"
|
||||
}
|
||||
```
|
||||
|
||||
All values should be returned as strings.
|
||||
|
||||
# --hints--
|
||||
|
||||
`parse_url_query("https://example.com/search?name=Alice&age=30")` should return `{"name": "Alice", "age": "30"}`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_url_query("https://example.com/search?name=Alice&age=30"), {"name": "Alice", "age": "30"})`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_url_query("https://freecodecamp.org/learn?skill=programming&language=python")` should return `{"skill": "programming", "language": "python"}`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_url_query("https://freecodecamp.org/learn?skill=programming&language=python"), {"skill": "programming", "language": "python"})`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_url_query("https://freecodecamp.org/items?category=books&sort=asc&page=2")` should return `{"category": "books", "sort": "asc", "page": "2"}`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_url_query("https://freecodecamp.org/items?category=books&sort=asc&page=2"), {"category": "books", "sort": "asc", "page": "2"})`)
|
||||
}})
|
||||
```
|
||||
|
||||
`parse_url_query("https://example.com?redirect=freecodecamp.org/learn&when=now")` should return `{"redirect": "freecodecamp.org/learn", "when": "now"}`
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertEqual(parse_url_query("https://example.com?redirect=freecodecamp.org/learn&when=now"), {"redirect": "freecodecamp.org/learn", "when": "now"})`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def parse_url_query(url):
|
||||
|
||||
return url
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def parse_url_query(url):
|
||||
query_string = url.split('?')[1]
|
||||
return dict(param.split('=') for param in query_string.split('&'))
|
||||
```
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
---
|
||||
id: 69c6ff713a52713463aa7929
|
||||
title: "Challenge 263: Binary Crossword"
|
||||
challengeType: 29
|
||||
dashedName: challenge-263
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Given a character, determine if its 8-bit binary representation can be found in the following grid, horizontally or vertically in either direction:
|
||||
|
||||
```js
|
||||
0 1 0 0 0 0 0 1
|
||||
0 1 1 0 1 1 1 1
|
||||
0 1 0 0 0 1 0 0
|
||||
0 1 1 0 0 1 0 1
|
||||
0 1 0 1 0 0 1 0
|
||||
0 1 0 1 0 1 0 0
|
||||
0 1 1 0 1 0 0 0
|
||||
1 0 1 0 1 1 1 0
|
||||
```
|
||||
|
||||
For example, `"A"` has the binary representation `01000001`, which appears in the first row from left to right.
|
||||
|
||||
# --hints--
|
||||
|
||||
`is_in_crossword("I")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("I"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_in_crossword("D")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("D"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_in_crossword("0")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("0"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_in_crossword("u")` should return `True`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("u"), True)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_in_crossword("Y")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("Y"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_in_crossword("p")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("p"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_in_crossword("1")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("1"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
`is_in_crossword("Q")` should return `False`.
|
||||
|
||||
```js
|
||||
({test: () => { runPython(`
|
||||
from unittest import TestCase
|
||||
TestCase().assertIs(is_in_crossword("Q"), False)`)
|
||||
}})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```py
|
||||
def is_in_crossword(char):
|
||||
|
||||
return char
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```py
|
||||
def is_in_crossword(char):
|
||||
grid = [
|
||||
"01000001",
|
||||
"01101111",
|
||||
"01000100",
|
||||
"01100101",
|
||||
"01010010",
|
||||
"01010100",
|
||||
"01101000",
|
||||
"10101110",
|
||||
]
|
||||
target = format(ord(char), '08b')
|
||||
cols = [''.join(grid[r][c] for r in range(8)) for c in range(8)]
|
||||
return any(target in seq or target in seq[::-1] for seq in grid + cols)
|
||||
```
|
||||
@@ -985,6 +985,78 @@
|
||||
{
|
||||
"id": "69b5b2be76ec8135a7fbe974",
|
||||
"title": "Challenge 245: Spiral Matrix"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a03",
|
||||
"title": "Challenge 246: Name Initials"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a04",
|
||||
"title": "Challenge 247: Last Letter"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a05",
|
||||
"title": "Challenge 248: Sorted Array Swap"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a06",
|
||||
"title": "Challenge 249: String Math"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a07",
|
||||
"title": "Challenge 250: Hidden Key"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a08",
|
||||
"title": "Challenge 251: Array Sum Finder"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a09",
|
||||
"title": "Challenge 252: Unique Stair Climber"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8b7",
|
||||
"title": "Challenge 253: Acronym Finder"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8b8",
|
||||
"title": "Challenge 254: Odd Words"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8b9",
|
||||
"title": "Challenge 255: Earth Day Cleanup Crew"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8ba",
|
||||
"title": "Challenge 256: Closest Time Direction"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8bb",
|
||||
"title": "Challenge 257: Word Compressor"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8bc",
|
||||
"title": "Challenge 258: Word Decompressor"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8bd",
|
||||
"title": "Challenge 259: FizzBuzz Explosion"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7926",
|
||||
"title": "Challenge 260: Word Score"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7927",
|
||||
"title": "Challenge 261: Number Words"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7928",
|
||||
"title": "Challenge 262: URL Query Parser"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7929",
|
||||
"title": "Challenge 263: Binary Crossword"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -984,6 +984,78 @@
|
||||
{
|
||||
"id": "69b5b2be76ec8135a7fbe974",
|
||||
"title": "Challenge 245: Spiral Matrix"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a03",
|
||||
"title": "Challenge 246: Name Initials"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a04",
|
||||
"title": "Challenge 247: Last Letter"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a05",
|
||||
"title": "Challenge 248: Sorted Array Swap"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a06",
|
||||
"title": "Challenge 249: String Math"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a07",
|
||||
"title": "Challenge 250: Hidden Key"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a08",
|
||||
"title": "Challenge 251: Array Sum Finder"
|
||||
},
|
||||
{
|
||||
"id": "69bc6cb30c1d112a2e110a09",
|
||||
"title": "Challenge 252: Unique Stair Climber"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8b7",
|
||||
"title": "Challenge 253: Acronym Finder"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8b8",
|
||||
"title": "Challenge 254: Odd Words"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8b9",
|
||||
"title": "Challenge 255: Earth Day Cleanup Crew"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8ba",
|
||||
"title": "Challenge 256: Closest Time Direction"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8bb",
|
||||
"title": "Challenge 257: Word Compressor"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8bc",
|
||||
"title": "Challenge 258: Word Decompressor"
|
||||
},
|
||||
{
|
||||
"id": "69c5f3d787b1725d5f00c8bd",
|
||||
"title": "Challenge 259: FizzBuzz Explosion"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7926",
|
||||
"title": "Challenge 260: Word Score"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7927",
|
||||
"title": "Challenge 261: Number Words"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7928",
|
||||
"title": "Challenge 262: URL Query Parser"
|
||||
},
|
||||
{
|
||||
"id": "69c6ff713a52713463aa7929",
|
||||
"title": "Challenge 263: Binary Crossword"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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 = 245;
|
||||
const EXPECTED_CHALLENGE_COUNT = 263;
|
||||
|
||||
// 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