-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2023): add solution for challenge #21
- Loading branch information
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Challenge #21: Binary message | ||
|
||
The elves are receiving strange binary messages from Mars 🪐. Are the aliens trying to communicate with them? 👽 | ||
|
||
**The message that arrives is an array of 0s and 1s**. It seems they have found a pattern… To make sure, they want to **find the longest segment of the string where the number of 0s and 1s is equal.** | ||
|
||
```js | ||
findBalancedSegment([1, 1, 0, 1, 1, 0, 1, 1]) | ||
// |________| | ||
// position of segment: [2, 5] | ||
// longest balanced | ||
// of 0s and 1s | ||
|
||
findBalancedSegment([1, 1, 0]) | ||
// |__| | ||
// [1, 2] | ||
|
||
findBalancedSegment([1, 1, 1]) | ||
// no balanced segments: [] | ||
``` | ||
|
||
Keep in mind that if there is more than one balanced pattern, **you should return the longest and the first one you find from left to right.** | ||
|
||
They say that if they find the pattern, they will be able to send a message back to Mars 🚀. It seems that they have to send it to `https://mars.codes`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe } from 'vitest' | ||
import { findBalancedSegment } from './challenge-21' | ||
|
||
const TEST_CASES: TestCases<number[], number[]> = [ | ||
{ args: [1, 1, 0, 1, 1, 0, 1, 1], expected: [2, 5] }, | ||
{ args: [1, 1, 0], expected: [1, 2] }, | ||
{ args: [1, 1, 1], expected: [] }, | ||
{ args: [1, 0, 1], expected: [0, 1] }, | ||
{ args: [1, 0, 1, 0], expected: [0, 3] }, | ||
{ args: [1, 1, 0, 1, 0, 1], expected: [1, 4] }, | ||
{ args: [1, 0, 0, 0, 1, 1, 1, 0, 0, 0], expected: [0, 7] }, | ||
{ args: [0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1], expected: [5, 10] }, | ||
] | ||
|
||
describe('Challenge #21: Binary message', () => { | ||
buildChallengeTestCases({ | ||
cases: TEST_CASES, | ||
fn: findBalancedSegment, | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export function findBalancedSegment(message: number[]) { | ||
const segment = [0, 0] | ||
|
||
for (const [i, binary] of message.entries()) { | ||
let ones = binary | ||
let zeros = +(binary === 0) | ||
const currentSegment = [i, i] | ||
|
||
for (const [j, number] of message.slice(i + 1).entries()) { | ||
ones += number | ||
zeros += +(number === 0) | ||
|
||
if (ones === zeros) { | ||
currentSegment[1] = j + i + 1 | ||
} | ||
} | ||
|
||
if (currentSegment[1] - currentSegment[0] > segment[1] - segment[0]) { | ||
segment[0] = currentSegment[0] | ||
segment[1] = currentSegment[1] | ||
} | ||
} | ||
|
||
if (segment[1] === 0) return [] | ||
|
||
return segment | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters