feat(curriculum): daily challenges 230-245 (#66511)

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Tom
2026-03-17 14:14:32 -05:00
committed by GitHub
parent e66bf09dce
commit a74e55ee4b
35 changed files with 3149 additions and 1 deletions
@@ -0,0 +1,88 @@
---
id: 69b1028d6e265413d0198a29
title: "Challenge 230: Pascal's Triangle Row"
challengeType: 28
dashedName: challenge-230
---
# --description--
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:
```js
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
# --hints--
`pascalRow(5)` should return `[1, 4, 6, 4, 1]`.
```js
assert.deepEqual(pascalRow(5), [1, 4, 6, 4, 1]);
```
`pascalRow(3)` should return `[1, 2, 1]`.
```js
assert.deepEqual(pascalRow(3), [1, 2, 1]);
```
`pascalRow(1)` should return `[1]`.
```js
assert.deepEqual(pascalRow(1), [1]);
```
`pascalRow(10)` should return `[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]`.
```js
assert.deepEqual(pascalRow(10), [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]);
```
`pascalRow(15)` should return `[1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]`.
```js
assert.deepEqual(pascalRow(15), [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]);
```
# --seed--
## --seed-contents--
```js
function pascalRow(n) {
return n;
}
```
# --solutions--
```js
function pascalRow(n) {
if (n === 1) return [1];
let row = [1];
for (let i = 2; i <= n; i++) {
const next = [1];
for (let j = 1; j < row.length; j++) {
next.push(row[j - 1] + row[j]);
}
next.push(1);
row = next;
}
return row;
}
```
@@ -0,0 +1,96 @@
---
id: 69b1028d6e265413d0198a2a
title: "Challenge 231: ISBN-10 Validator"
challengeType: 28
dashedName: challenge-231
---
# --description--
Given a string, determine if it's a valid ISBN-10.
An ISBN-10 consists of hyphens (`"-"`) and 10 other characters. After removing the hyphens (`"-"`):
- The first 9 characters must be digits, and
- The final character may be a digit or the letter `"X"`, which represents the number 10.
To validate it:
- Multiply each digit (or value) by its position (multiply the first digit by 1, the second by 2, and so on).
- Add all the results together.
- If the total is divisible by 11, it's valid.
# --hints--
`isValidIsbn10("0-306-40615-2")` should return `true`.
```js
assert.isTrue(isValidIsbn10("0-306-40615-2"));
```
`isValidIsbn10("0-306-40615-1")` should return `false`.
```js
assert.isFalse(isValidIsbn10("0-306-40615-1"));
```
`isValidIsbn10("0-8044-2957-X")` should return `true`.
```js
assert.isTrue(isValidIsbn10("0-8044-2957-X"));
```
`isValidIsbn10("X-306-40615-2")` should return `false`.
```js
assert.isFalse(isValidIsbn10("X-306-40615-2"));
```
`isValidIsbn10("0-6822-2589-4")` should return `true`.
```js
assert.isTrue(isValidIsbn10("0-6822-2589-4"));
```
# --seed--
## --seed-contents--
```js
function isValidIsbn10(str) {
return str;
}
```
# --solutions--
```js
function isValidIsbn10(str) {
const isbn = str.replace(/-/g, "");
if (isbn.length !== 10) return false;
let sum = 0;
for (let i = 0; i < 9; i++) {
const digit = Number(isbn[i]);
if (Number.isNaN(digit)) return false;
sum += digit * (i + 1);
}
const last = isbn[9];
if (last === "X") {
sum += 10 * 10;
} else {
const digit = Number(last);
if (Number.isNaN(digit)) return false;
sum += digit * 10;
}
return sum % 11 === 0;
}
```
@@ -0,0 +1,80 @@
---
id: 69b1028d6e265413d0198a2b
title: "Challenge 232: Due Date"
challengeType: 28
dashedName: challenge-232
---
# --description--
Given a date string, return the date 9 months in the future.
- The given and return strings have the format `"YYYY-MM-DD"`.
- If the month nine months into the future doesn't contain the original day number, return the last day of that month.
# --hints--
`getDueDate("2025-03-30")` should return `"2025-12-30"`.
```js
assert.equal(getDueDate("2025-03-30"), "2025-12-30");
```
`getDueDate("2025-04-27")` should return `"2026-01-27"`.
```js
assert.equal(getDueDate("2025-04-27"), "2026-01-27");
```
`getDueDate("2025-05-29")` should return `"2026-02-28"`.
```js
assert.equal(getDueDate("2025-05-29"), "2026-02-28");
```
`getDueDate("2026-06-30")` should return `"2027-03-30"`.
```js
assert.equal(getDueDate("2026-06-30"), "2027-03-30");
```
`getDueDate("2026-10-11")` should return `"2027-07-11"`.
```js
assert.equal(getDueDate("2026-10-11"), "2027-07-11");
```
# --seed--
## --seed-contents--
```js
function getDueDate(dateStr) {
return dateStr;
}
```
# --solutions--
```js
function getDueDate(dateStr) {
let [year, month, day] = dateStr.split("-").map(Number);
month += 9;
year += Math.floor((month - 1) / 12);
month = ((month - 1) % 12) + 1;
const daysInMonth = new Date(year, month, 0).getDate();
if (day > daysInMonth) {
day = daysInMonth;
}
const mm = String(month).padStart(2, "0");
const dd = String(day).padStart(2, "0");
return `${year}-${mm}-${dd}`;
}
```
@@ -0,0 +1,92 @@
---
id: 69b1028d6e265413d0198a2c
title: "Challenge 233: Wake-Up Alarm"
challengeType: 28
dashedName: challenge-233
---
# --description--
Given a string representing the time you set your alarm and a string representing the time you actually woke up, determine if you woke up early, on time, or late.
- Both times will be given in `"HH:MM"` 24-hour format.
Return:
- `"early"` if you woke up before your alarm time.
- `"on time"` if you woke up at your alarm time, or within the 10 minute snooze window after the alarm time.
- `"late"` if you woke up more than 10 minutes after your alarm time.
Both times are on the same day.
# --hints--
`alarmCheck("07:00", "06:45")` should return `"early"`.
```js
assert.equal(alarmCheck("07:00", "06:45"), "early");
```
`alarmCheck("06:30", "06:30")` should return `"on time"`.
```js
assert.equal(alarmCheck("06:30", "06:30"), "on time");
```
`alarmCheck("08:10", "08:15")` should return `"on time"`.
```js
assert.equal(alarmCheck("08:10", "08:15"), "on time");
```
`alarmCheck("09:30", "09:45")` should return `"late"`.
```js
assert.equal(alarmCheck("09:30", "09:45"), "late");
```
`alarmCheck("08:15", "08:25")` should return `"on time"`.
```js
assert.equal(alarmCheck("08:15", "08:25"), "on time");
```
`alarmCheck("05:45", "05:56")` should return `"late"`.
```js
assert.equal(alarmCheck("05:45", "05:56"), "late");
```
`alarmCheck("04:30", "04:00")` should return `"early"`.
```js
assert.equal(alarmCheck("04:30", "04:00"), "early");
```
# --seed--
## --seed-contents--
```js
function alarmCheck(alarmTime, wakeTime) {
return alarmTime;
}
```
# --solutions--
```js
function alarmCheck(alarmTime, wakeTime) {
const [alarmH, alarmM] = alarmTime.split(":").map(Number);
const [wakeH, wakeM] = wakeTime.split(":").map(Number);
const alarmMinutes = alarmH * 60 + alarmM;
const wakeMinutes = wakeH * 60 + wakeM;
const diff = wakeMinutes - alarmMinutes;
if (diff < 0) return "early";
if (diff <= 10) return "on time";
return "late";
}
```
@@ -0,0 +1,103 @@
---
id: 69b1028d6e265413d0198a2d
title: "Challenge 234: Prank Number"
challengeType: 28
dashedName: challenge-234
---
# --description--
Given an array of numbers where all but one number follow a pattern, return a new array with the one number that doesn't follow the pattern fixed.
The pattern will be one of:
- The numbers increase from one to the next by a fixed amount (addition).
- The numbers decrease from one to the next by a fixed amount (subtraction).
For example, given `[2, 4, 7, 8, 10]` return `[2, 4, 6, 8, 10]`.
# --hints--
`fixPrankNumber([2, 4, 7, 8, 10])` should return `[2, 4, 6, 8, 10]`.
```js
assert.deepEqual(fixPrankNumber([2, 4, 7, 8, 10]), [2, 4, 6, 8, 10]);
```
`fixPrankNumber([10, 10, 8, 7, 6])` should return `[10, 9, 8, 7, 6]`.
```js
assert.deepEqual(fixPrankNumber([10, 10, 8, 7, 6]), [10, 9, 8, 7, 6]);
```
`fixPrankNumber([12, 24, 36, 48, 61, 72, 84, 96])` should return `[12, 24, 36, 48, 60, 72, 84, 96]`.
```js
assert.deepEqual(fixPrankNumber([12, 24, 36, 48, 61, 72, 84, 96]), [12, 24, 36, 48, 60, 72, 84, 96]);
```
`fixPrankNumber([4, 1, -2, -5, -8, -5])` should return `[4, 1, -2, -5, -8, -11]`.
```js
assert.deepEqual(fixPrankNumber([4, 1, -2, -5, -8, -5]), [4, 1, -2, -5, -8, -11]);
```
`fixPrankNumber([0, 100, 200, 300, 150, 500])` should return `[0, 100, 200, 300, 400, 500]`.
```js
assert.deepEqual(fixPrankNumber([0, 100, 200, 300, 150, 500]), [0, 100, 200, 300, 400, 500]);
```
`fixPrankNumber([400, 425, 400, 375, 350, 325, 300])` should return `[450, 425, 400, 375, 350, 325, 300]`.
```js
assert.deepEqual(fixPrankNumber([400, 425, 400, 375, 350, 325, 300]), [450, 425, 400, 375, 350, 325, 300]);
```
`fixPrankNumber([-5, 5, 10, 15, 20])` should return `[0, 5, 10, 15, 20]`.
```js
assert.deepEqual(fixPrankNumber([-5, 5, 10, 15, 20]), [0, 5, 10, 15, 20]);
```
# --seed--
## --seed-contents--
```js
function fixPrankNumber(arr) {
return arr;
}
```
# --solutions--
```js
function fixPrankNumber(arr) {
const diffs = arr.slice(1).map((v, i) => v - arr[i]);
let step = diffs[0];
for (let i = 0; i < diffs.length - 1; i++) {
if (diffs[i] === diffs[i + 1]) {
step = diffs[i];
break;
}
}
const result = [...arr];
for (let i = 0; i < result.length - 1; i++) {
if (result[i + 1] - result[i] !== step) {
if (i === 0 && result[2] - result[1] === step) {
result[0] = result[1] - step;
} else {
result[i + 1] = result[i] + step;
}
break;
}
}
return result;
}
```
@@ -0,0 +1,79 @@
---
id: 69b1028d6e265413d0198a2e
title: "Challenge 235: Capitalized Fibonacci"
challengeType: 28
dashedName: challenge-235
---
# --description--
Given a string, return a new string where each letter is capitalized if its index is a Fibonacci number, and lowercased otherwise.
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The first 10 numbers in the sequence are `0`, `1`, `1`, `2`, `3`, `5`, `8`, `13`, `21`, `34`.
- The first character is at index `0`.
- If the index of non-letter characters is a Fibonacci number, leave it unchanged.
# --hints--
`capitalizeFibonacci("hello world")` should return `"HELLo woRld"`.
```js
assert.equal(capitalizeFibonacci("hello world"), "HELLo woRld");
```
`capitalizeFibonacci("HELLO WORLD")` should return `"HELLo woRld"`.
```js
assert.equal(capitalizeFibonacci("HELLO WORLD"), "HELLo woRld");
```
`capitalizeFibonacci("hello, world!")` should return `"HELLo, wOrld!"`.
```js
assert.equal(capitalizeFibonacci("hello, world!"), "HELLo, wOrld!");
```
`capitalizeFibonacci("The quick brown fox jumped over the lazy dog.")` should return `"THE qUicK broWn fox jUmped over thE lazy dog."`.
```js
assert.equal(capitalizeFibonacci("The quick brown fox jumped over the lazy dog."), "THE qUicK broWn fox jUmped over thE lazy dog.");
```
`capitalizeFibonacci("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin pulvinar ex nibh, vel ullamcorper ligula egestas quis. Integer tincidunt fringilla accumsan. Integer et metus placerat, gravida felis at, pellentesque nisl.")` should return `"LOREm ipSum dOlor sit amet, consecTetur adipiscing elit. proin pulvinar ex nibh, vel ullaMcorper ligula egestas quis. integer tincidunt fringillA accumsan. integer et metus placerat, gravida felis at, pellentesque nisl."`.
```js
assert.equal(capitalizeFibonacci("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin pulvinar ex nibh, vel ullamcorper ligula egestas quis. Integer tincidunt fringilla accumsan. Integer et metus placerat, gravida felis at, pellentesque nisl."), "LOREm ipSum dOlor sit amet, consecTetur adipiscing elit. proin pulvinar ex nibh, vel ullaMcorper ligula egestas quis. integer tincidunt fringillA accumsan. integer et metus placerat, gravida felis at, pellentesque nisl.");
```
# --seed--
## --seed-contents--
```js
function capitalizeFibonacci(str) {
return str;
}
```
# --solutions--
```js
function capitalizeFibonacci(str) {
const fibs = new Set([0, 1]);
let a = 0, b = 1;
while (b < str.length) {
[a, b] = [b, a + b];
fibs.add(b);
}
return str
.split("")
.map((char, i) => {
if (!/[a-zA-Z]/.test(char)) return char;
return fibs.has(i) ? char.toUpperCase() : char.toLowerCase();
})
.join("");
}
```
@@ -0,0 +1,90 @@
---
id: 69b1028d6e265413d0198a2f
title: "Challenge 236: Browser History"
challengeType: 28
dashedName: challenge-236
---
# --description--
Given an array of browser commands, return an array with two values: the history as an array of URLs, and the index of the current page.
Valid commands are:
- `"URL"` - Where URL is a web address (`"freecodecamp.org"` for example). Navigates to the given URL, adds it to the history at the next position, and discards any forward history.
- `"Back"` - moves to the previous page in history, or stays on the current page if there isn't one.
- `"Forward"` - moves to the next page in history, or stays on the current page if there isn't one.
For example, given `["freecodecamp.org", "freecodecamp.org/learn", "Back"]`, return `[["freecodecamp.org", "freecodecamp.org/learn"], 0]`.
# --hints--
`getBrowserHistory(["freecodecamp.org", "freecodecamp.org/learn", "Back"])` should return `[["freecodecamp.org", "freecodecamp.org/learn"], 0]`.
```js
assert.deepEqual(getBrowserHistory(["freecodecamp.org", "freecodecamp.org/learn", "Back"]), [["freecodecamp.org", "freecodecamp.org/learn"], 0]);
```
`getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog"])` should return `[["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3]`.
```js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog"]), [["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3]);
```
`getBrowserHistory(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"])` should return `[["example.com", "example.com/contact", "example.com/blog"], 1]`.
```js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"]), [["example.com", "example.com/contact", "example.com/blog"], 1]);
```
`getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"])` should return `[["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3]`.
```js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"]), [["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3]);
```
`getBrowserHistory(["example.com", "example.com/about", "Back", "Back"])` should return `[["example.com", "example.com/about"], 0]`.
```js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "Back", "Back"]), [["example.com", "example.com/about"], 0]);
```
`getBrowserHistory(["example.com", "example.com/about", "Forward"])` should return `[["example.com", "example.com/about"], 1]`.
```js
assert.deepEqual(getBrowserHistory(["example.com", "example.com/about", "Forward"]), [["example.com", "example.com/about"], 1]);
```
# --seed--
## --seed-contents--
```js
function getBrowserHistory(commands) {
return commands;
}
```
# --solutions--
```js
function getBrowserHistory(commands) {
const stack = [];
let index = -1;
for (const command of commands) {
if (command === "Back") {
if (index > 0) index--;
} else if (command === "Forward") {
if (index < stack.length - 1) index++;
} else {
stack.splice(index + 1);
stack.push(command);
index++;
}
}
return [stack, index];
}
```
@@ -0,0 +1,118 @@
---
id: 69b559d2903b9e4afe9075f7
title: "Challenge 237: Equation Validation"
challengeType: 28
dashedName: challenge-237
---
# --description--
Given a string representing a math equation, determine whether it is correct.
- The left side may contain up to three positive integers and the operators `+`, `-`, `*`, and `/`.
- The equation will be given in the format: `"number operator number = number"` (with two or three numbers on the left). For example: `"2 + 2 = 4"` or `"2 + 3 - 1 = 4"`.
- The right side will always be a single integer.
Follow standard order of operations: multiplication and division are evaluated before addition and subtraction, from left-to-right.
# --hints--
`isValidEquation("2 + 2 = 4")` should return `true`.
```js
assert.isTrue(isValidEquation("2 + 2 = 4"));
```
`isValidEquation("2 + 3 - 1 = 4")` should return `true`.
```js
assert.isTrue(isValidEquation("2 + 3 - 1 = 4"));
```
`isValidEquation("8 / 2 = 4")` should return `true`.
```js
assert.isTrue(isValidEquation("8 / 2 = 4"));
```
`isValidEquation("10 * 5 = 50")` should return `true`.
```js
assert.isTrue(isValidEquation("10 * 5 = 50"));
```
`isValidEquation("2 - 2 = 0")` should return `true`.
```js
assert.isTrue(isValidEquation("2 - 2 = 0"));
```
`isValidEquation("2 + 9 / 3 = 5")` should return `true`.
```js
assert.isTrue(isValidEquation("2 + 9 / 3 = 5"));
```
`isValidEquation("20 - 2 * 3 = 14")` should return `true`.
```js
assert.isTrue(isValidEquation("20 - 2 * 3 = 14"));
```
`isValidEquation("2 + 5 = 6")` should return `false`.
```js
assert.isFalse(isValidEquation("2 + 5 = 6"));
```
`isValidEquation("10 - 2 * 3 = 24")` should return `false`.
```js
assert.isFalse(isValidEquation("10 - 2 * 3 = 24"));
```
`isValidEquation("3 + 9 / 3 = 4")` should return `false`.
```js
assert.isFalse(isValidEquation("3 + 9 / 3 = 4"));
```
# --seed--
## --seed-contents--
```js
function isValidEquation(equation) {
return equation;
}
```
# --solutions--
```js
function isValidEquation(equation) {
const [left, right] = equation.split(" = ");
const tokens = left.split(" ");
for (let i = 1; i < tokens.length - 1; i += 2) {
const operator = tokens[i];
if (operator === "*" || operator === "/") {
const leftOperand = Number(tokens[i - 1]);
const rightOperand = Number(tokens[i + 1]);
const computed = operator === "*" ? leftOperand * rightOperand : leftOperand / rightOperand;
tokens.splice(i - 1, 3, String(computed));
i -= 2;
}
}
let result = Number(tokens[0]);
for (let i = 1; i < tokens.length - 1; i += 2) {
const operator = tokens[i];
const operand = Number(tokens[i + 1]);
result = operator === "+" ? result + operand : result - operand;
}
return result === Number(right);
}
```
@@ -0,0 +1,70 @@
---
id: 69b559d2903b9e4afe9075f8
title: "Challenge 238: Digit Rotation Escape"
challengeType: 28
dashedName: challenge-238
---
# --description--
Given a positive integer, determine if it, or any of its rotations, is evenly divisible by its digit count.
A rotation means to move the first digit to the end. For example, after 1 rotation, 123 becomes 231.
- Check rotation `0` (the given number) first.
- Given numbers won't contain any zeros.
- Return the first rotation number if one is found, or `"none"` if not.
# --hints--
`getRotation(123)` should return `0`.
```js
assert.equal(getRotation(123), 0);
```
`getRotation(13579)` should return `3`.
```js
assert.equal(getRotation(13579), 3);
```
`getRotation(24681)` should return `"none"`.
```js
assert.equal(getRotation(24681), "none");
```
`getRotation(84138789345)` should return `6`.
```js
assert.equal(getRotation(84138789345), 6);
```
# --seed--
## --seed-contents--
```js
function getRotation(n) {
return n;
}
```
# --solutions--
```js
function getRotation(n) {
const str = String(n);
const digitCount = str.length;
let current = str;
for (let i = 0; i < digitCount; i++) {
if (Number(current) % digitCount === 0) return i;
current = current.slice(1) + current[0];
}
return "none";
}
```
@@ -0,0 +1,73 @@
---
id: 69b559d2903b9e4afe9075f9
title: "Challenge 239: What Day Is It?"
challengeType: 28
dashedName: challenge-239
---
# --description--
Given a Unix timestamp in milliseconds, return the day of the week.
Valid return days are:
- `"Sunday"`
- `"Monday"`
- `"Tuesday"`
- `"Wednesday"`
- `"Thursday"`
- `"Friday"`
- `"Saturday"`
Be sure to ignore time zones.
# --hints--
`getDayOfWeek(1775492249000)` should return `"Monday"`.
```js
assert.equal(getDayOfWeek(1775492249000), "Monday");
```
`getDayOfWeek(1766246400000)` should return `"Saturday"`.
```js
assert.equal(getDayOfWeek(1766246400000), "Saturday");
```
`getDayOfWeek(33791256000000)` should return `"Tuesday"`.
```js
assert.equal(getDayOfWeek(33791256000000), "Tuesday");
```
`getDayOfWeek(1773576000000)` should return `"Sunday"`.
```js
assert.equal(getDayOfWeek(1773576000000), "Sunday");
```
`getDayOfWeek(0)` should return `"Thursday"`.
```js
assert.equal(getDayOfWeek(0), "Thursday");
```
# --seed--
## --seed-contents--
```js
function getDayOfWeek(timestamp) {
return timestamp;
}
```
# --solutions--
```js
function getDayOfWeek(timestamp) {
return new Date(timestamp).toLocaleDateString("en-US", { weekday: "long", timeZone: "UTC" });
}
```
@@ -0,0 +1,67 @@
---
id: 69b58ce40693f140c84c8559
title: "Challenge 240: Palindrome Characters"
challengeType: 28
dashedName: challenge-240
---
# --description--
Given a string, determine if it's a palindrome and return the middle character (if it's odd length) or middle two characters (if it's even).
- A palindrome is a string that is the same forward and backward.
- If it's not a palindrome, return `"none"`.
# --hints--
`palindromeLocator("racecar")` should return `"e"`.
```js
assert.equal(palindromeLocator("racecar"), "e");
```
`palindromeLocator("level")` should return `"v"`.
```js
assert.equal(palindromeLocator("level"), "v");
```
`palindromeLocator("freecodecamp")` should return `"none"`.
```js
assert.equal(palindromeLocator("freecodecamp"), "none");
```
`palindromeLocator("noon")` should return `"oo"`.
```js
assert.equal(palindromeLocator("noon"), "oo");
```
`palindromeLocator("11100111")` should return `"00"`.
```js
assert.equal(palindromeLocator("11100111"), "00");
```
# --seed--
## --seed-contents--
```js
function palindromeLocator(str) {
return str;
}
```
# --solutions--
```js
function palindromeLocator(str) {
if (str !== str.split("").reverse().join("")) return "none";
const mid = Math.floor(str.length / 2);
return str.length % 2 === 1 ? str[mid] : str[mid - 1] + str[mid];
}
```
@@ -0,0 +1,96 @@
---
id: 69b58ce40693f140c84c855a
title: "Challenge 241: FizzBuzz Validator"
challengeType: 28
dashedName: challenge-241
---
# --description--
Given an array of sequential integers, with multiples of 3 and 5 replaced, determine if it's a valid FizzBuzz sequence.
In a valid FizzBuzz sequence:
- Multiples of 3 are replaced with "Fizz".
- Multiples of 5 are replaced with "Buzz".
- Multiples of both 3 and 5 are replaced with "FizzBuzz".
- All other numbers remain as integers.
# --hints--
`isFizzBuzz([1, 2, "Fizz", 4, "Buzz"])` should return `true`.
```js
assert.isTrue(isFizzBuzz([1, 2, "Fizz", 4, "Buzz"]));
```
`isFizzBuzz([13, 14, "FizzBuzz", 16, 17])` should return `true`.
```js
assert.isTrue(isFizzBuzz([13, 14, "FizzBuzz", 16, 17]));
```
`isFizzBuzz([1, 2, "Fizz", 4, 5])` should return `false`.
```js
assert.isFalse(isFizzBuzz([1, 2, "Fizz", 4, 5]));
```
`isFizzBuzz(["FizzBuzz", 16, 17, "Fizz", 19, "Buzz"])` should return `true`.
```js
assert.isTrue(isFizzBuzz(["FizzBuzz", 16, 17, "Fizz", 19, "Buzz"]));
```
`isFizzBuzz([1, 2, "Fizz", "Buzz", 5])` should return `false`.
```js
assert.isFalse(isFizzBuzz([1, 2, "Fizz", "Buzz", 5]));
```
`isFizzBuzz([97, 98, "Buzz", "Fizz", 101, "Fizz", 103])` should return `false`.
```js
assert.isFalse(isFizzBuzz([97, 98, "Buzz", "Fizz", 101, "Fizz", 103]));
```
`isFizzBuzz(["Fizz", "Buzz", 101, "Fizz", 103, 104, "FizzBuzz"])` should return `true`.
```js
assert.isTrue(isFizzBuzz(["Fizz", "Buzz", 101, "Fizz", 103, 104, "FizzBuzz"]));
```
# --seed--
## --seed-contents--
```js
function isFizzBuzz(arr) {
return arr;
}
```
# --solutions--
```js
function isFizzBuzz(arr) {
let start = null;
for (let i = 0; i < arr.length; i++) {
if (typeof arr[i] === "number") {
start = arr[i] - i;
break;
}
}
if (start === null) return false;
for (let i = 0; i < arr.length; i++) {
const n = start + i;
const expected = n % 15 === 0 ? "FizzBuzz" : n % 3 === 0 ? "Fizz" : n % 5 === 0 ? "Buzz" : n;
if (arr[i] !== expected) return false;
}
return true;
}
```
@@ -0,0 +1,80 @@
---
id: 69b58ce40693f140c84c855b
title: "Challenge 242: Next Bingo Number"
challengeType: 28
dashedName: challenge-242
---
# --description--
Given a bingo number, return the next bingo number sequentially.
A bingo number is a single letter followed by a number in its range according to this chart:
| Letter | Number Range |
| - | - |
| `"B"` | 1-15 |
| `"I"` | 16-30 |
| `"N"` | 31-45 |
| `"G"` | 46-60 |
| `"O"` | 61-75 |
For example, given `"B10"`, return `"B11"`, the next bingo number. If given the last bingo number, return `"B1"`.
# --hints--
`getNextBingoNumber("B10")` should return `"B11"`.
```js
assert.equal(getNextBingoNumber("B10"), "B11");
```
`getNextBingoNumber("N33")` should return `"N34"`.
```js
assert.equal(getNextBingoNumber("N33"), "N34");
```
`getNextBingoNumber("I30")` should return `"N31"`.
```js
assert.equal(getNextBingoNumber("I30"), "N31");
```
`getNextBingoNumber("G60")` should return `"O61"`.
```js
assert.equal(getNextBingoNumber("G60"), "O61");
```
`getNextBingoNumber("O75")` should return `"B1"`.
```js
assert.equal(getNextBingoNumber("O75"), "B1");
```
# --seed--
## --seed-contents--
```js
function getNextBingoNumber(n) {
return n;
}
```
# --solutions--
```js
function getNextBingoNumber(n) {
const num = parseInt(n.slice(1));
const next = num === 75 ? 1 : num + 1;
if (next <= 15) return "B" + next;
if (next <= 30) return "I" + next;
if (next <= 45) return "N" + next;
if (next <= 60) return "G" + next;
return "O" + next;
}
```
@@ -0,0 +1,69 @@
---
id: 69b58ce40693f140c84c855c
title: "Challenge 243: Rook Attack"
challengeType: 28
dashedName: challenge-243
---
# --description--
Given two strings for the location of two rooks on a chess board, determine if they can attack each other.
A standard chessboard is 8x8, with columns labeled `A` through `H` (left to right) and rows labeled `1` through `8` (bottom to top). It looks like this:
|**A8**|**B8**|**C8**|**D8**|**E8**|**F8**|**G8**|**H8**|
|-|-|-|-|-|-|-|-|
|**A7**|**B7**|**C7**|**D7**|**E7**|**F7**|**G7**|**H7**|
|**A6**|**B6**|**C6**|**D6**|**E6**|**F6**|**G6**|**H6**|
|**A5**|**B5**|**C5**|**D5**|**E5**|**F5**|**G5**|**H5**|
|**A4**|**B4**|**C4**|**D4**|**E4**|**F4**|**G4**|**H4**|
|**A3**|**B3**|**C3**|**D3**|**E3**|**F3**|**G3**|**H3**|
|**A2**|**B2**|**C2**|**D2**|**E2**|**F2**|**G2**|**H2**|
|**A1**|**B1**|**C1**|**D1**|**E1**|**F1**|**G1**|**H1**|
Rooks can move as many squares as they want in a horizontal or vertical direction. So if they are on the same row or column, they can attack each other.
# --hints--
`rookAttack("A1", "A8")` should return `true`.
```js
assert.isTrue(rookAttack("A1", "A8"));
```
`rookAttack("B4", "F4")` should return `true`.
```js
assert.isTrue(rookAttack("B4", "F4"));
```
`rookAttack("E3", "D4")` should return `false`.
```js
assert.isFalse(rookAttack("E3", "D4"));
```
`rookAttack("H7", "F6")` should return `false`.
```js
assert.isFalse(rookAttack("H7", "F6"));
```
# --seed--
## --seed-contents--
```js
function rookAttack(rook1, rook2) {
return rook1;
}
```
# --solutions--
```js
function rookAttack(rook1, rook2) {
return rook1[0] === rook2[0] || rook1[1] === rook2[1];
}
```
@@ -0,0 +1,94 @@
---
id: 69b5b2be76ec8135a7fbe973
title: "Challenge 244: Rook and Bishop Attack"
challengeType: 28
dashedName: challenge-244
---
# --description--
Given a string for the location of a rook on a chess board, and another for the location of a bishop, determine if one piece can attack another.
A standard chessboard is 8x8, with columns labeled `A` through `H` (left to right) and rows labeled `1` through `8` (bottom to top). It looks like this:
|**A8**|**B8**|**C8**|**D8**|**E8**|**F8**|**G8**|**H8**|
|-|-|-|-|-|-|-|-|
|**A7**|**B7**|**C7**|**D7**|**E7**|**F7**|**G7**|**H7**|
|**A6**|**B6**|**C6**|**D6**|**E6**|**F6**|**G6**|**H6**|
|**A5**|**B5**|**C5**|**D5**|**E5**|**F5**|**G5**|**H5**|
|**A4**|**B4**|**C4**|**D4**|**E4**|**F4**|**G4**|**H4**|
|**A3**|**B3**|**C3**|**D3**|**E3**|**F3**|**G3**|**H3**|
|**A2**|**B2**|**C2**|**D2**|**E2**|**F2**|**G2**|**H2**|
|**A1**|**B1**|**C1**|**D1**|**E1**|**F1**|**G1**|**H1**|
- Rooks can move as many squares as they want in a horizontal or vertical direction.
- Bishops can move as many squares as they want in any diagonal direction.
- One piece can attack another if it can move to the location of that piece.
Return:
- `"rook"` if the rook can attack the bishop.
- `"bishop"` if the bishop can attack the rook.
- `"neither"` if neither piece can attack one another.
# --hints--
`rookBishopAttack("A1", "A5")` should return `"rook"`.
```js
assert.equal(rookBishopAttack("A1", "A5"), "rook");
```
`rookBishopAttack("C3", "F6")` should return `"bishop"`.
```js
assert.equal(rookBishopAttack("C3", "F6"), "bishop");
```
`rookBishopAttack("D4", "D7")` should return `"rook"`.
```js
assert.equal(rookBishopAttack("D4", "D7"), "rook");
```
`rookBishopAttack("B7", "H1")` should return `"bishop"`.
```js
assert.equal(rookBishopAttack("B7", "H1"), "bishop");
```
`rookBishopAttack("B3", "C5")` should return `"neither"`.
```js
assert.equal(rookBishopAttack("B3", "C5"), "neither");
```
`rookBishopAttack("G3", "E8")` should return `"neither"`.
```js
assert.equal(rookBishopAttack("G3", "E8"), "neither");
```
# --seed--
## --seed-contents--
```js
function rookBishopAttack(rook, bishop) {
return rook;
}
```
# --solutions--
```js
function rookBishopAttack(rook, bishop) {
const colDiff = Math.abs(rook.charCodeAt(0) - bishop.charCodeAt(0));
const rowDiff = Math.abs(rook[1] - bishop[1]);
if (rook[0] === bishop[0] || rook[1] === bishop[1]) return "rook";
if (colDiff === rowDiff) return "bishop";
return "neither";
}
```
@@ -0,0 +1,91 @@
---
id: 69b5b2be76ec8135a7fbe974
title: "Challenge 245: Spiral Matrix"
challengeType: 28
dashedName: challenge-245
---
# --description--
Given a 2D matrix, return a flat array with all of its values in clockwise order.
The returned array should have the top-left value first, move right along the top row, then down the right column, then left along the bottom row, then up the left column. Repeat inward for any remaining layers.
For example, given:
```json
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
```
Return `[1, 2, 3, 6, 9, 8, 7, 4, 5]`.
# --hints--
`spiralMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])` should return `[1, 2, 3, 6, 9, 8, 7, 4, 5]`.
```js
assert.deepEqual(spiralMatrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [1, 2, 3, 6, 9, 8, 7, 4, 5]);
```
`spiralMatrix([["a", "b", "c", "d"], ["l", "m", "n", "e"], ["k", "p", "o", "f"], ["j", "i", "h", "g"]])` should return `["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"]`.
```js
assert.deepEqual(spiralMatrix([["a", "b", "c", "d"], ["l", "m", "n", "e"], ["k", "p", "o", "f"], ["j", "i", "h", "g"]]), ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"]);
```
`spiralMatrix([[true, false, false], [false, true, true], [false, true, false], [true, true, false]])` should return `[true, false, false, true, false, false, true, true, false, false, true, true]`.
```js
assert.deepEqual(spiralMatrix([[true, false, false], [false, true, true], [false, true, false], [true, true, false]]), [true, false, false, true, false, false, true, true, false, false, true, true]);
```
`spiralMatrix([[25, 24, 23, 22, 21], [10, 9, 8, 7, 20], [11, 2, 1, 6, 19], [12, 3, 4, 5, 18], [13, 14, 15, 16, 17]])` should return `[25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]`.
```js
assert.deepEqual(spiralMatrix([[25, 24, 23, 22, 21], [10, 9, 8, 7, 20], [11, 2, 1, 6, 19], [12, 3, 4, 5, 18], [13, 14, 15, 16, 17]]), [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
```
# --seed--
## --seed-contents--
```js
function spiralMatrix(matrix) {
return matrix
}
```
# --solutions--
```js
function spiralMatrix(matrix) {
const result = [];
let top = 0, bottom = matrix.length - 1;
let left = 0, right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) result.push(matrix[top][i]);
top++;
for (let i = top; i <= bottom; i++) result.push(matrix[i][right]);
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) result.push(matrix[bottom][i]);
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) result.push(matrix[i][left]);
left++;
}
}
return result;
}
```
@@ -0,0 +1,100 @@
---
id: 69b1028d6e265413d0198a29
title: "Challenge 230: Pascal's Triangle Row"
challengeType: 29
dashedName: challenge-230
---
# --description--
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:
```js
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```
# --hints--
`pascal_row(5)` should return `[1, 4, 6, 4, 1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pascal_row(5), [1, 4, 6, 4, 1])`)
}})
```
`pascal_row(3)` should return `[1, 2, 1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pascal_row(3), [1, 2, 1])`)
}})
```
`pascal_row(1)` should return `[1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pascal_row(1), [1])`)
}})
```
`pascal_row(10)` should return `[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pascal_row(10), [1, 9, 36, 84, 126, 126, 84, 36, 9, 1])`)
}})
```
`pascal_row(15)` should return `[1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pascal_row(15), [1, 14, 91, 364, 1001, 2002, 3003, 3432, 3003, 2002, 1001, 364, 91, 14, 1])`)
}})
```
# --seed--
## --seed-contents--
```py
def pascal_row(n):
return n
```
# --solutions--
```py
def pascal_row(n):
if n == 1:
return [1]
row = [1]
for _ in range(2, n + 1):
next_row = [1]
for i in range(1, len(row)):
next_row.append(row[i - 1] + row[i])
next_row.append(1)
row = next_row
return row
```
@@ -0,0 +1,106 @@
---
id: 69b1028d6e265413d0198a2a
title: "Challenge 231: ISBN-10 Validator"
challengeType: 29
dashedName: challenge-231
---
# --description--
Given a string, determine if it's a valid ISBN-10.
An ISBN-10 consists of hyphens (`"-"`) and 10 other characters. After removing the hyphens (`"-"`):
- The first 9 characters must be digits, and
- The final character may be a digit or the letter `"X"`, which represents the number 10.
To validate it:
- Multiply each digit (or value) by its position (multiply the first digit by 1, the second by 2, and so on).
- Add all the results together.
- If the total is divisible by 11, it's valid.
# --hints--
`is_valid_isbn10("0-306-40615-2")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-306-40615-2"), True)`)
}})
```
`is_valid_isbn10("0-306-40615-1")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-306-40615-1"), False)`)
}})
```
`is_valid_isbn10("0-8044-2957-X")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-8044-2957-X"), True)`)
}})
```
`is_valid_isbn10("X-306-40615-2")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("X-306-40615-2"), False)`)
}})
```
`is_valid_isbn10("0-6822-2589-4")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-6822-2589-4"), True)`)
}})
```
# --seed--
## --seed-contents--
```py
def is_valid_isbn10(s):
return s
```
# --solutions--
```py
def is_valid_isbn10(s):
isbn = s.replace("-", "")
if len(isbn) != 10:
return False
total = 0
for i in range(9):
if not isbn[i].isdigit():
return False
total += int(isbn[i]) * (i + 1)
last = isbn[9]
if last == "X":
total += 10 * 10
elif last.isdigit():
total += int(last) * 10
else:
return False
return total % 11 == 0
```
@@ -0,0 +1,90 @@
---
id: 69b1028d6e265413d0198a2b
title: "Challenge 232: Due Date"
challengeType: 29
dashedName: challenge-232
---
# --description--
Given a date string, return the date 9 months in the future.
- The given and return strings have the format `"YYYY-MM-DD"`.
- If the month nine months into the future doesn't contain the original day number, return the last day of that month.
# --hints--
`get_due_date("2025-03-30")` should return `"2025-12-30"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2025-03-30"), "2025-12-30")`)
}})
```
`get_due_date("2025-04-27")` should return `"2026-01-27"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2025-04-27"), "2026-01-27")`)
}})
```
`get_due_date("2025-05-29")` should return `"2026-02-28"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2025-05-29"), "2026-02-28")`)
}})
```
`get_due_date("2026-06-30")` should return `"2027-03-30"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2026-06-30"), "2027-03-30")`)
}})
```
`get_due_date("2026-10-11")` should return `"2027-07-11"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_due_date("2026-10-11"), "2027-07-11")`)
}})
```
# --seed--
## --seed-contents--
```py
def get_due_date(date_str):
return date_str
```
# --solutions--
```py
import calendar
def get_due_date(date_str):
year, month, day = map(int, date_str.split("-"))
month += 9
year += (month - 1) // 12
month = (month - 1) % 12 + 1
days_in_month = calendar.monthrange(year, month)[1]
if day > days_in_month:
day = days_in_month
return f"{year}-{month:02d}-{day:02d}"
```
@@ -0,0 +1,113 @@
---
id: 69b1028d6e265413d0198a2c
title: "Challenge 233: Wake-Up Alarm"
challengeType: 29
dashedName: challenge-233
---
# --description--
Given a string representing the time you set your alarm and a string representing the time you actually woke up, determine if you woke up early, on time, or late.
- Both times will be given in `"HH:MM"` 24-hour format.
Return:
- `"early"` if you woke up before your alarm time.
- `"on time"` if you woke up at your alarm time, or within the 10 minute snooze window after the alarm time.
- `"late"` if you woke up more than 10 minutes after your alarm time.
Both times are on the same day.
# --hints--
`alarm_check("07:00", "06:45")` should return `"early"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(alarm_check("07:00", "06:45"), "early")`)
}})
```
`alarm_check("06:30", "06:30")` should return `"on time"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(alarm_check("06:30", "06:30"), "on time")`)
}})
```
`alarm_check("08:10", "08:15")` should return `"on time"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(alarm_check("08:10", "08:15"), "on time")`)
}})
```
`alarm_check("09:30", "09:45")` should return `"late"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(alarm_check("09:30", "09:45"), "late")`)
}})
```
`alarm_check("08:15", "08:25")` should return `"on time"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(alarm_check("08:15", "08:25"), "on time")`)
}})
```
`alarm_check("05:45", "05:56")` should return `"late"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(alarm_check("05:45", "05:56"), "late")`)
}})
```
`alarm_check("04:30", "04:00")` should return `"early"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(alarm_check("04:30", "04:00"), "early")`)
}})
```
# --seed--
## --seed-contents--
```py
def alarm_check(alarm_time, wake_time):
return alarm_time
```
# --solutions--
```py
def alarm_check(alarm_time, wake_time):
alarm_h, alarm_m = map(int, alarm_time.split(":"))
wake_h, wake_m = map(int, wake_time.split(":"))
alarm_minutes = alarm_h * 60 + alarm_m
wake_minutes = wake_h * 60 + wake_m
diff = wake_minutes - alarm_minutes
if diff < 0:
return "early"
if diff <= 10:
return "on time"
return "late"
```
@@ -0,0 +1,117 @@
---
id: 69b1028d6e265413d0198a2d
title: "Challenge 234: Prank Number"
challengeType: 29
dashedName: challenge-234
---
# --description--
Given an array of numbers where all but one number follow a pattern, return a new array with the one number that doesn't follow the pattern fixed.
The pattern will be one of:
- The numbers increase from one to the next by a fixed amount (addition).
- The numbers decrease from one to the next by a fixed amount (subtraction).
For example, given `[2, 4, 7, 8, 10]` return `[2, 4, 6, 8, 10]`.
# --hints--
`fix_prank_number([2, 4, 7, 8, 10])` should return `[2, 4, 6, 8, 10]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_prank_number([2, 4, 7, 8, 10]), [2, 4, 6, 8, 10])`)
}})
```
`fix_prank_number([10, 10, 8, 7, 6])` should return `[10, 9, 8, 7, 6]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_prank_number([10, 10, 8, 7, 6]), [10, 9, 8, 7, 6])`)
}})
```
`fix_prank_number([12, 24, 36, 48, 61, 72, 84, 96])` should return `[12, 24, 36, 48, 60, 72, 84, 96]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_prank_number([12, 24, 36, 48, 61, 72, 84, 96]), [12, 24, 36, 48, 60, 72, 84, 96])`)
}})
```
`fix_prank_number([4, 1, -2, -5, -8, -5])` should return `[4, 1, -2, -5, -8, -11]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_prank_number([4, 1, -2, -5, -8, -5]), [4, 1, -2, -5, -8, -11])`)
}})
```
`fix_prank_number([0, 100, 200, 300, 150, 500])` should return `[0, 100, 200, 300, 400, 500]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_prank_number([0, 100, 200, 300, 150, 500]), [0, 100, 200, 300, 400, 500])`)
}})
```
`fix_prank_number([400, 425, 400, 375, 350, 325, 300])` should return `[450, 425, 400, 375, 350, 325, 300]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_prank_number([400, 425, 400, 375, 350, 325, 300]), [450, 425, 400, 375, 350, 325, 300])`)
}})
```
`fix_prank_number([-5, 5, 10, 15, 20])` should return `[0, 5, 10, 15, 20]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_prank_number([-5, 5, 10, 15, 20]), [0, 5, 10, 15, 20])`)
}})
```
# --seed--
## --seed-contents--
```py
def fix_prank_number(arr):
return arr
```
# --solutions--
```py
def fix_prank_number(arr):
diffs = [arr[i + 1] - arr[i] for i in range(len(arr) - 1)]
step = diffs[0]
for i in range(len(diffs) - 1):
if diffs[i] == diffs[i + 1]:
step = diffs[i]
break
result = arr[:]
for i in range(len(result) - 1):
if result[i + 1] - result[i] != step:
if i == 0 and result[2] - result[1] == step:
result[0] = result[1] - step
else:
result[i + 1] = result[i] + step
break
return result
```
@@ -0,0 +1,94 @@
---
id: 69b1028d6e265413d0198a2e
title: "Challenge 235: Capitalized Fibonacci"
challengeType: 29
dashedName: challenge-235
---
# --description--
Given a string, return a new string where each letter is capitalized if its index is a Fibonacci number, and lowercased otherwise.
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The first 10 numbers in the sequence are `0`, `1`, `1`, `2`, `3`, `5`, `8`, `13`, `21`, `34`.
- The first character is at index `0`.
- If the index of non-letter characters is a Fibonacci number, leave it unchanged.
# --hints--
`capitalize_fibonacci("hello world")` should return `"HELLo woRld"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("hello world"), "HELLo woRld")`)
}})
```
`capitalize_fibonacci("HELLO WORLD")` should return `"HELLo woRld"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("HELLO WORLD"), "HELLo woRld")`)
}})
```
`capitalize_fibonacci("hello, world!")` should return `"HELLo, wOrld!"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("hello, world!"), "HELLo, wOrld!")`)
}})
```
`capitalize_fibonacci("The quick brown fox jumped over the lazy dog.")` should return `"THE qUicK broWn fox jUmped over thE lazy dog."`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("The quick brown fox jumped over the lazy dog."), "THE qUicK broWn fox jUmped over thE lazy dog.")`)
}})
```
`capitalize_fibonacci("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin pulvinar ex nibh, vel ullamcorper ligula egestas quis. Integer tincidunt fringilla accumsan. Integer et metus placerat, gravida felis at, pellentesque nisl.")` should return `"LOREm ipSum dOlor sit amet, consecTetur adipiscing elit. proin pulvinar ex nibh, vel ullaMcorper ligula egestas quis. integer tincidunt fringillA accumsan. integer et metus placerat, gravida felis at, pellentesque nisl."`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin pulvinar ex nibh, vel ullamcorper ligula egestas quis. Integer tincidunt fringilla accumsan. Integer et metus placerat, gravida felis at, pellentesque nisl."), "LOREm ipSum dOlor sit amet, consecTetur adipiscing elit. proin pulvinar ex nibh, vel ullaMcorper ligula egestas quis. integer tincidunt fringillA accumsan. integer et metus placerat, gravida felis at, pellentesque nisl.")`)
}})
```
# --seed--
## --seed-contents--
```py
def capitalize_fibonacci(s):
return s
```
# --solutions--
```py
def capitalize_fibonacci(s):
fibs = {0, 1}
a, b = 0, 1
while b < len(s):
a, b = b, a + b
fibs.add(b)
result = []
for i, char in enumerate(s):
if not char.isalpha():
result.append(char)
elif i in fibs:
result.append(char.upper())
else:
result.append(char.lower())
return "".join(result)
```
@@ -0,0 +1,106 @@
---
id: 69b1028d6e265413d0198a2f
title: "Challenge 236: Browser History"
challengeType: 29
dashedName: challenge-236
---
# --description--
Given an array of browser commands, return an array with two values: the history as an array of URLs, and the index of the current page.
Valid commands are:
- `"URL"` - Where URL is a web address (`"freecodecamp.org"` for example). Navigates to the given URL, adds it to the history at the next position, and discards any forward history.
- `"Back"` - moves to the previous page in history, or stays on the current page if there isn't one.
- `"Forward"` - moves to the next page in history, or stays on the current page if there isn't one.
For example, given `["freecodecamp.org", "freecodecamp.org/learn", "Back"]`, return `[["freecodecamp.org", "freecodecamp.org/learn"], 0]`.
# --hints--
`get_browser_history(["freecodecamp.org", "freecodecamp.org/learn", "Back"])` should return `[["freecodecamp.org", "freecodecamp.org/learn"], 0]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["freecodecamp.org", "freecodecamp.org/learn", "Back"]), [["freecodecamp.org", "freecodecamp.org/learn"], 0])`)
}})
```
`get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog"])` should return `[["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog"]), [["example.com", "example.com/about", "example.com/contact", "example.com/blog"], 3])`)
}})
```
`get_browser_history(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"])` should return `[["example.com", "example.com/contact", "example.com/blog"], 1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Back", "example.com/contact", "example.com/blog", "Back", "Back", "Forward"]), [["example.com", "example.com/contact", "example.com/blog"], 1])`)
}})
```
`get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"])` should return `[["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "example.com/contact", "example.com/blog", "Back", "Back", "Forward", "freecodecamp.org"]), [["example.com", "example.com/about", "example.com/contact", "freecodecamp.org"], 3])`)
}})
```
`get_browser_history(["example.com", "example.com/about", "Back", "Back"])` should return `[["example.com", "example.com/about"], 0]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Back", "Back"]), [["example.com", "example.com/about"], 0])`)
}})
```
`get_browser_history(["example.com", "example.com/about", "Forward"])` should return `[["example.com", "example.com/about"], 1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_browser_history(["example.com", "example.com/about", "Forward"]), [["example.com", "example.com/about"], 1])`)
}})
```
# --seed--
## --seed-contents--
```py
def get_browser_history(commands):
return commands
```
# --solutions--
```py
def get_browser_history(commands):
stack = []
index = -1
for command in commands:
if command == "Back":
if index > 0:
index -= 1
elif command == "Forward":
if index < len(stack) - 1:
index += 1
else:
del stack[index + 1:]
stack.append(command)
index += 1
return [stack, index]
```
@@ -0,0 +1,147 @@
---
id: 69b559d2903b9e4afe9075f7
title: "Challenge 237: Equation Validation"
challengeType: 29
dashedName: challenge-237
---
# --description--
Given a string representing a math equation, determine whether it is correct.
- The left side may contain up to three positive integers and the operators `+`, `-`, `*`, and `/`.
- The equation will be given in the format: `"number operator number = number"` (with two or three numbers on the left). For example: `"2 + 2 = 4"` or `"2 + 3 - 1 = 4"`.
- The right side will always be a single integer.
Follow standard order of operations: multiplication and division are evaluated before addition and subtraction, from left-to-right.
# --hints--
`is_valid_equation("2 + 2 = 4")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("2 + 2 = 4"), True)`)
}})
```
`is_valid_equation("2 + 3 - 1 = 4")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("2 + 3 - 1 = 4"), True)`)
}})
```
`is_valid_equation("8 / 2 = 4")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("8 / 2 = 4"), True)`)
}})
```
`is_valid_equation("10 * 5 = 50")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("10 * 5 = 50"), True)`)
}})
```
`is_valid_equation("2 - 2 = 0")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("2 - 2 = 0"), True)`)
}})
```
`is_valid_equation("2 + 9 / 3 = 5")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("2 + 9 / 3 = 5"), True)`)
}})
```
`is_valid_equation("20 - 2 * 3 = 14")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("20 - 2 * 3 = 14"), True)`)
}})
```
`is_valid_equation("2 + 5 = 6")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("2 + 5 = 6"), False)`)
}})
```
`is_valid_equation("10 - 2 * 3 = 24")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("10 - 2 * 3 = 24"), False)`)
}})
```
`is_valid_equation("3 + 9 / 3 = 4")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_equation("3 + 9 / 3 = 4"), False)`)
}})
```
# --seed--
## --seed-contents--
```py
def is_valid_equation(equation):
return equation
```
# --solutions--
```py
def is_valid_equation(equation):
left, right = equation.split(" = ")
tokens = left.split(" ")
i = 1
while i < len(tokens) - 1:
op = tokens[i]
if op in ("*", "/"):
a = int(tokens[i - 1])
b = int(tokens[i + 1])
computed = a * b if op == "*" else a // b
tokens[i - 1:i + 2] = [str(computed)]
else:
i += 2
result = int(tokens[0])
i = 1
while i < len(tokens) - 1:
op = tokens[i]
val = int(tokens[i + 1])
result = result + val if op == "+" else result - val
i += 2
return result == int(right)
```
@@ -0,0 +1,80 @@
---
id: 69b559d2903b9e4afe9075f8
title: "Challenge 238: Digit Rotation Escape"
challengeType: 29
dashedName: challenge-238
---
# --description--
Given a positive integer, determine if it, or any of its rotations, is evenly divisible by its digit count.
A rotation means to move the first digit to the end. For example, after 1 rotation, 123 becomes 231.
- Check rotation `0` (the given number) first.
- Given numbers won't contain any zeros.
- Return the first rotation number if one is found, or `"none"` if not.
# --hints--
`get_rotation(123)` should return `0`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(123), 0)`)
}})
```
`get_rotation(13579)` should return `3`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(13579), 3)`)
}})
```
`get_rotation(24681)` should return `"none"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(24681), "none")`)
}})
```
`get_rotation(84138789345)` should return `6`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_rotation(84138789345), 6)`)
}})
```
# --seed--
## --seed-contents--
```py
def get_rotation(n):
return n
```
# --solutions--
```py
def get_rotation(n):
s = str(n)
digit_count = len(s)
current = s
for i in range(digit_count):
if int(current) % digit_count == 0:
return i
current = current[1:] + current[0]
return "none"
```
@@ -0,0 +1,90 @@
---
id: 69b559d2903b9e4afe9075f9
title: "Challenge 239: What Day Is It?"
challengeType: 29
dashedName: challenge-239
---
# --description--
Given a Unix timestamp in milliseconds, return the day of the week.
Valid return days are:
- `"Sunday"`
- `"Monday"`
- `"Tuesday"`
- `"Wednesday"`
- `"Thursday"`
- `"Friday"`
- `"Saturday"`
Be sure to ignore time zones.
# --hints--
`get_day_of_week(1775492249000)` should return `"Monday"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(1775492249000), "Monday")`)
}})
```
`get_day_of_week(1766246400000)` should return `"Saturday"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(1766246400000), "Saturday")`)
}})
```
`get_day_of_week(33791256000000)` should return `"Tuesday"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(33791256000000), "Tuesday")`)
}})
```
`get_day_of_week(1773576000000)` should return `"Sunday"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(1773576000000), "Sunday")`)
}})
```
`get_day_of_week(0)` should return `"Thursday"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_day_of_week(0), "Thursday")`)
}})
```
# --seed--
## --seed-contents--
```py
def get_day_of_week(timestamp):
return timestamp
```
# --solutions--
```py
from datetime import datetime, timezone
def get_day_of_week(timestamp):
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
dt = datetime.fromtimestamp(timestamp / 1000, tz=timezone.utc)
return days[dt.weekday()]
```
@@ -0,0 +1,80 @@
---
id: 69b58ce40693f140c84c8559
title: "Challenge 240: Palindrome Characters"
challengeType: 29
dashedName: challenge-240
---
# --description--
Given a string, determine if it's a palindrome and return the middle character (if it's odd length) or middle two characters (if it's even).
- A palindrome is a string that is the same forward and backward.
- If it's not a palindrome, return `"none"`.
# --hints--
`palindrome_locator("racecar")` should return `"e"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("racecar"), "e")`)
}})
```
`palindrome_locator("level")` should return `"v"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("level"), "v")`)
}})
```
`palindrome_locator("freecodecamp")` should return `"none"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("freecodecamp"), "none")`)
}})
```
`palindrome_locator("noon")` should return `"oo"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("noon"), "oo")`)
}})
```
`palindrome_locator("11100111")` should return `"00"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(palindrome_locator("11100111"), "00")`)
}})
```
# --seed--
## --seed-contents--
```py
def palindrome_locator(s):
return s
```
# --solutions--
```py
def palindrome_locator(s):
if s != s[::-1]:
return "none"
mid = len(s) // 2
return s[mid] if len(s) % 2 == 1 else s[mid - 1] + s[mid]
```
@@ -0,0 +1,121 @@
---
id: 69b58ce40693f140c84c855a
title: "Challenge 241: FizzBuzz Validator"
challengeType: 29
dashedName: challenge-241
---
# --description--
Given an array of sequential integers, with multiples of 3 and 5 replaced, determine if it's a valid FizzBuzz sequence.
In a valid FizzBuzz sequence:
- Multiples of 3 are replaced with "Fizz".
- Multiples of 5 are replaced with "Buzz".
- Multiples of both 3 and 5 are replaced with "FizzBuzz".
- All other numbers remain as integers.
# --hints--
`is_fizz_buzz([1, 2, "Fizz", 4, "Buzz"])` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_fizz_buzz([1, 2, "Fizz", 4, "Buzz"]), True)`)
}})
```
`is_fizz_buzz([13, 14, "FizzBuzz", 16, 17])` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_fizz_buzz([13, 14, "FizzBuzz", 16, 17]), True)`)
}})
```
`is_fizz_buzz([1, 2, "Fizz", 4, 5])` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_fizz_buzz([1, 2, "Fizz", 4, 5]), False)`)
}})
```
`is_fizz_buzz(["FizzBuzz", 16, 17, "Fizz", 19, "Buzz"])` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_fizz_buzz(["FizzBuzz", 16, 17, "Fizz", 19, "Buzz"]), True)`)
}})
```
`is_fizz_buzz([1, 2, "Fizz", "Buzz", 5])` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_fizz_buzz([1, 2, "Fizz", "Buzz", 5]), False)`)
}})
```
`is_fizz_buzz([97, 98, "Buzz", "Fizz", 101, "Fizz", 103])` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_fizz_buzz([97, 98, "Buzz", "Fizz", 101, "Fizz", 103]), False)`)
}})
```
`is_fizz_buzz(["Fizz", "Buzz", 101, "Fizz", 103, 104, "FizzBuzz"])` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_fizz_buzz(["Fizz", "Buzz", 101, "Fizz", 103, 104, "FizzBuzz"]), True)`)
}})
```
# --seed--
## --seed-contents--
```py
def is_fizz_buzz(arr):
return arr
```
# --solutions--
```py
def is_fizz_buzz(arr):
start = None
for i, val in enumerate(arr):
if isinstance(val, int):
start = val - i
break
if start is None:
return False
for i in range(len(arr)):
n = start + i
if n % 15 == 0:
expected = "FizzBuzz"
elif n % 3 == 0:
expected = "Fizz"
elif n % 5 == 0:
expected = "Buzz"
else:
expected = n
if arr[i] != expected:
return False
return True
```
@@ -0,0 +1,97 @@
---
id: 69b58ce40693f140c84c855b
title: "Challenge 242: Next Bingo Number"
challengeType: 29
dashedName: challenge-242
---
# --description--
Given a bingo number, return the next bingo number sequentially.
A bingo number is a single letter followed by a number in its range according to this chart:
| Letter | Number Range |
| - | - |
| `"B"` | 1-15 |
| `"I"` | 16-30 |
| `"N"` | 31-45 |
| `"G"` | 46-60 |
| `"O"` | 61-75 |
For example, given `"B10"`, return `"B11"`, the next bingo number. If given the last bingo number, return `"B1"`.
# --hints--
`get_next_bingo_number("B10")` should return `"B11"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_bingo_number("B10"), "B11")`)
}})
```
`get_next_bingo_number("N33")` should return `"N34"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_bingo_number("N33"), "N34")`)
}})
```
`get_next_bingo_number("I30")` should return `"N31"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_bingo_number("I30"), "N31")`)
}})
```
`get_next_bingo_number("G60")` should return `"O61"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_bingo_number("G60"), "O61")`)
}})
```
`get_next_bingo_number("O75")` should return `"B1"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_bingo_number("O75"), "B1")`)
}})
```
# --seed--
## --seed-contents--
```py
def get_next_bingo_number(n):
return n
```
# --solutions--
```py
def get_next_bingo_number(n):
num = int(n[1:])
next_num = 1 if num == 75 else num + 1
if next_num <= 15:
return "B" + str(next_num)
if next_num <= 30:
return "I" + str(next_num)
if next_num <= 45:
return "N" + str(next_num)
if next_num <= 60:
return "G" + str(next_num)
return "O" + str(next_num)
```
@@ -0,0 +1,79 @@
---
id: 69b58ce40693f140c84c855c
title: "Challenge 243: Rook Attack"
challengeType: 29
dashedName: challenge-243
---
# --description--
Given two strings for the location of two rooks on a chess board, determine if they can attack each other.
A standard chessboard is 8x8, with columns labeled `A` through `H` (left to right) and rows labeled `1` through `8` (bottom to top). It looks like this:
|**A8**|**B8**|**C8**|**D8**|**E8**|**F8**|**G8**|**H8**|
|-|-|-|-|-|-|-|-|
|**A7**|**B7**|**C7**|**D7**|**E7**|**F7**|**G7**|**H7**|
|**A6**|**B6**|**C6**|**D6**|**E6**|**F6**|**G6**|**H6**|
|**A5**|**B5**|**C5**|**D5**|**E5**|**F5**|**G5**|**H5**|
|**A4**|**B4**|**C4**|**D4**|**E4**|**F4**|**G4**|**H4**|
|**A3**|**B3**|**C3**|**D3**|**E3**|**F3**|**G3**|**H3**|
|**A2**|**B2**|**C2**|**D2**|**E2**|**F2**|**G2**|**H2**|
|**A1**|**B1**|**C1**|**D1**|**E1**|**F1**|**G1**|**H1**|
Rooks can move as many squares as they want in a horizontal or vertical direction. So if they are on the same row or column, they can attack each other.
# --hints--
`rook_attack("A1", "A8")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(rook_attack("A1", "A8"), True)`)
}})
```
`rook_attack("B4", "F4")` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(rook_attack("B4", "F4"), True)`)
}})
```
`rook_attack("E3", "D4")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(rook_attack("E3", "D4"), False)`)
}})
```
`rook_attack("H7", "F6")` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(rook_attack("H7", "F6"), False)`)
}})
```
# --seed--
## --seed-contents--
```py
def rook_attack(rook1, rook2):
return rook1
```
# --solutions--
```py
def rook_attack(rook1, rook2):
return rook1[0] == rook2[0] or rook1[1] == rook2[1]
```
@@ -0,0 +1,112 @@
---
id: 69b5b2be76ec8135a7fbe973
title: "Challenge 244: Rook and Bishop Attack"
challengeType: 29
dashedName: challenge-244
---
# --description--
Given a string for the location of a rook on a chess board, and another for the location of a bishop, determine if one piece can attack another.
A standard chessboard is 8x8, with columns labeled `A` through `H` (left to right) and rows labeled `1` through `8` (bottom to top). It looks like this:
|**A8**|**B8**|**C8**|**D8**|**E8**|**F8**|**G8**|**H8**|
|-|-|-|-|-|-|-|-|
|**A7**|**B7**|**C7**|**D7**|**E7**|**F7**|**G7**|**H7**|
|**A6**|**B6**|**C6**|**D6**|**E6**|**F6**|**G6**|**H6**|
|**A5**|**B5**|**C5**|**D5**|**E5**|**F5**|**G5**|**H5**|
|**A4**|**B4**|**C4**|**D4**|**E4**|**F4**|**G4**|**H4**|
|**A3**|**B3**|**C3**|**D3**|**E3**|**F3**|**G3**|**H3**|
|**A2**|**B2**|**C2**|**D2**|**E2**|**F2**|**G2**|**H2**|
|**A1**|**B1**|**C1**|**D1**|**E1**|**F1**|**G1**|**H1**|
- Rooks can move as many squares as they want in a horizontal or vertical direction.
- Bishops can move as many squares as they want in any diagonal direction.
- One piece can attack another if it can move to the location of that piece.
Return:
- `"rook"` if the rook can attack the bishop.
- `"bishop"` if the bishop can attack the rook.
- `"neither"` if neither piece can attack one another.
# --hints--
`rook_bishop_attack("A1", "A5")` should return `"rook"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rook_bishop_attack("A1", "A5"), "rook")`)
}})
```
`rook_bishop_attack("C3", "F6")` should return `"bishop"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rook_bishop_attack("C3", "F6"), "bishop")`)
}})
```
`rook_bishop_attack("D4", "D7")` should return `"rook"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rook_bishop_attack("D4", "D7"), "rook")`)
}})
```
`rook_bishop_attack("B7", "H1")` should return `"bishop"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rook_bishop_attack("B7", "H1"), "bishop")`)
}})
```
`rook_bishop_attack("B3", "C5")` should return `"neither"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rook_bishop_attack("B3", "C5"), "neither")`)
}})
```
`rook_bishop_attack("G3", "E8")` should return `"neither"`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(rook_bishop_attack("G3", "E8"), "neither")`)
}})
```
# --seed--
## --seed-contents--
```py
def rook_bishop_attack(rook, bishop):
return rook
```
# --solutions--
```py
def rook_bishop_attack(rook, bishop):
col_diff = abs(ord(rook[0]) - ord(bishop[0]))
row_diff = abs(int(rook[1]) - int(bishop[1]))
if rook[0] == bishop[0] or rook[1] == bishop[1]:
return "rook"
if col_diff == row_diff:
return "bishop"
return "neither"
```
@@ -0,0 +1,102 @@
---
id: 69b5b2be76ec8135a7fbe974
title: "Challenge 245: Spiral Matrix"
challengeType: 29
dashedName: challenge-245
---
# --description--
Given a 2D matrix, return a flat array with all of its values in clockwise order.
The returned array should have the top-left value first, move right along the top row, then down the right column, then left along the bottom row, then up the left column. Repeat inward for any remaining layers.
For example, given:
```json
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
```
Return `[1, 2, 3, 6, 9, 8, 7, 4, 5]`.
# --hints--
`spiral_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])` should return `[1, 2, 3, 6, 9, 8, 7, 4, 5]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(spiral_matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), [1, 2, 3, 6, 9, 8, 7, 4, 5])`)
}})
```
`spiral_matrix([["a", "b", "c", "d"], ["l", "m", "n", "e"], ["k", "p", "o", "f"], ["j", "i", "h", "g"]])` should return `["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(spiral_matrix([["a", "b", "c", "d"], ["l", "m", "n", "e"], ["k", "p", "o", "f"], ["j", "i", "h", "g"]]), ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p"])`)
}})
```
`spiral_matrix([[True, False, False], [False, True, True], [False, True, False], [True, True, False]])` should return `[True, False, False, True, False, False, True, True, False, False, True, True]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(spiral_matrix([[True, False, False], [False, True, True], [False, True, False], [True, True, False]]), [True, False, False, True, False, False, True, True, False, False, True, True])`)
}})
```
`spiral_matrix([[25, 24, 23, 22, 21], [10, 9, 8, 7, 20], [11, 2, 1, 6, 19], [12, 3, 4, 5, 18], [13, 14, 15, 16, 17]])` should return `[25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(spiral_matrix([[25, 24, 23, 22, 21], [10, 9, 8, 7, 20], [11, 2, 1, 6, 19], [12, 3, 4, 5, 18], [13, 14, 15, 16, 17]]), [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1])`)
}})
```
# --seed--
## --seed-contents--
```py
def spiral_matrix(matrix):
return matrix
```
# --solutions--
```py
def spiral_matrix(matrix):
result = []
top, bottom = 0, len(matrix) - 1
left, right = 0, len(matrix[0]) - 1
while top <= bottom and left <= right:
for i in range(left, right + 1):
result.append(matrix[top][i])
top += 1
for i in range(top, bottom + 1):
result.append(matrix[i][right])
right -= 1
if top <= bottom:
for i in range(right, left - 1, -1):
result.append(matrix[bottom][i])
bottom -= 1
if left <= right:
for i in range(bottom, top - 1, -1):
result.append(matrix[i][left])
left += 1
return result
```
@@ -921,6 +921,70 @@
{
"id": "69a890af247de743333bd4d2",
"title": "Challenge 229: Truncate the Text 2"
},
{
"id": "69b1028d6e265413d0198a29",
"title": "Challenge 230: Pascal's Triangle Row"
},
{
"id": "69b1028d6e265413d0198a2a",
"title": "Challenge 231: ISBN-10 Validator"
},
{
"id": "69b1028d6e265413d0198a2b",
"title": "Challenge 232: Due Date"
},
{
"id": "69b1028d6e265413d0198a2c",
"title": "Challenge 233: Wake-Up Alarm"
},
{
"id": "69b1028d6e265413d0198a2d",
"title": "Challenge 234: Prank Number"
},
{
"id": "69b1028d6e265413d0198a2e",
"title": "Challenge 235: Capitalized Fibonacci"
},
{
"id": "69b1028d6e265413d0198a2f",
"title": "Challenge 236: Browser History"
},
{
"id": "69b559d2903b9e4afe9075f7",
"title": "Challenge 237: Equation Validation"
},
{
"id": "69b559d2903b9e4afe9075f8",
"title": "Challenge 238: Digit Rotation Escape"
},
{
"id": "69b559d2903b9e4afe9075f9",
"title": "Challenge 239: What Day Is It?"
},
{
"id": "69b58ce40693f140c84c8559",
"title": "Challenge 240: Palindrome Characters"
},
{
"id": "69b58ce40693f140c84c855a",
"title": "Challenge 241: FizzBuzz Validator"
},
{
"id": "69b58ce40693f140c84c855b",
"title": "Challenge 242: Next Bingo Number"
},
{
"id": "69b58ce40693f140c84c855c",
"title": "Challenge 243: Rook Attack"
},
{
"id": "69b5b2be76ec8135a7fbe973",
"title": "Challenge 244: Rook and Bishop Attack"
},
{
"id": "69b5b2be76ec8135a7fbe974",
"title": "Challenge 245: Spiral Matrix"
}
]
}
@@ -920,6 +920,70 @@
{
"id": "69a890af247de743333bd4d2",
"title": "Challenge 229: Truncate the Text 2"
},
{
"id": "69b1028d6e265413d0198a29",
"title": "Challenge 230: Pascal's Triangle Row"
},
{
"id": "69b1028d6e265413d0198a2a",
"title": "Challenge 231: ISBN-10 Validator"
},
{
"id": "69b1028d6e265413d0198a2b",
"title": "Challenge 232: Due Date"
},
{
"id": "69b1028d6e265413d0198a2c",
"title": "Challenge 233: Wake-Up Alarm"
},
{
"id": "69b1028d6e265413d0198a2d",
"title": "Challenge 234: Prank Number"
},
{
"id": "69b1028d6e265413d0198a2e",
"title": "Challenge 235: Capitalized Fibonacci"
},
{
"id": "69b1028d6e265413d0198a2f",
"title": "Challenge 236: Browser History"
},
{
"id": "69b559d2903b9e4afe9075f7",
"title": "Challenge 237: Equation Validation"
},
{
"id": "69b559d2903b9e4afe9075f8",
"title": "Challenge 238: Digit Rotation Escape"
},
{
"id": "69b559d2903b9e4afe9075f9",
"title": "Challenge 239: What Day Is It?"
},
{
"id": "69b58ce40693f140c84c8559",
"title": "Challenge 240: Palindrome Characters"
},
{
"id": "69b58ce40693f140c84c855a",
"title": "Challenge 241: FizzBuzz Validator"
},
{
"id": "69b58ce40693f140c84c855b",
"title": "Challenge 242: Next Bingo Number"
},
{
"id": "69b58ce40693f140c84c855c",
"title": "Challenge 243: Rook Attack"
},
{
"id": "69b5b2be76ec8135a7fbe973",
"title": "Challenge 244: Rook and Bishop Attack"
},
{
"id": "69b5b2be76ec8135a7fbe974",
"title": "Challenge 245: Spiral Matrix"
}
]
}
@@ -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 = 229;
const EXPECTED_CHALLENGE_COUNT = 245;
// 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)**