Skip to content

Commit 66b1547

Browse files
feat: ✨ add solution, tests and docs for challenge 21
1 parent 64d5158 commit 66b1547

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Challenge 21 - Binary message 🪐
2+
3+
The elves are receiving strange binary messages from Mars 🪐. Are the aliens trying to communicate with them? 👽
4+
5+
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.
6+
7+
## Example
8+
```ts
9+
findBalancedSegment([1, 1, 0, 1, 1, 0, 1, 1])
10+
// |________|
11+
// position of segment: [2, 5]
12+
// longest balanced
13+
// of 0s and 1s
14+
15+
findBalancedSegment([1, 1, 0])
16+
// |__|
17+
// [1, 2]
18+
19+
findBalancedSegment([1, 1, 1])
20+
// no balanced segments: []
21+
```
22+
23+
## Notes:
24+
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.
25+
26+
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { test, describe, expect, expectTypeOf } from 'vitest'
2+
import { findBalancedSegment } from './challenge21'
3+
4+
describe('Binary message', () => {
5+
test('Test 1', () => {
6+
expectTypeOf(findBalancedSegment).returns.toEqualTypeOf([])
7+
})
8+
9+
test('Test 2', () => {
10+
const received = findBalancedSegment([1, 1, 0, 1, 1, 0, 1, 1])
11+
const expected = [
12+
2,
13+
5
14+
]
15+
expect(received).toEqual(expected)
16+
})
17+
18+
test('Test 3', () => {
19+
const received = findBalancedSegment([1, 1, 0])
20+
const expected = [
21+
1,
22+
2
23+
]
24+
expect(received).toEqual(expected)
25+
})
26+
27+
test('Test 4', () => {
28+
const received = findBalancedSegment([1, 1, 1])
29+
const expected = []
30+
expect(received).toEqual(expected)
31+
})
32+
33+
test('Test 5', () => {
34+
const received = findBalancedSegment([1, 0, 1])
35+
const expected = [
36+
0,
37+
1
38+
]
39+
expect(received).toEqual(expected)
40+
})
41+
42+
test('Test 6', () => {
43+
const received = findBalancedSegment([1, 0, 1, 0])
44+
const expected = [
45+
0,
46+
3
47+
]
48+
expect(received).toEqual(expected)
49+
})
50+
51+
test('Test 7', () => {
52+
const received = findBalancedSegment([1, 1, 0, 1, 0, 1])
53+
const expected = [
54+
1,
55+
4
56+
]
57+
expect(received).toEqual(expected)
58+
})
59+
60+
test('Test 8', () => {
61+
const received = findBalancedSegment([1, 0, 0, 0, 1, 1, 1, 0, 0, 0])
62+
const expected = [
63+
0,
64+
7
65+
]
66+
expect(received).toEqual(expected)
67+
})
68+
69+
test('Test 6', () => {
70+
const received = findBalancedSegment([0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1])
71+
const expected = [
72+
5,
73+
10
74+
]
75+
expect(received).toEqual(expected)
76+
})
77+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export function findBalancedSegment (message: number[]): number[] {
2+
let range: number = 0
3+
let minIndex: number = -1
4+
5+
for (let i = 0; i < message.length; i += 1) {
6+
let count0: number = 0; let count1: number = 0
7+
for (let j = i; j < message.length; j += 1) {
8+
message[j] === 0 ? count0 += 1 : count1 += 1
9+
if ((count1 === count0) && ((j - i) > range)) {
10+
range = j - i
11+
minIndex = i
12+
}
13+
}
14+
}
15+
16+
return minIndex < 0 ? [] : [minIndex, range + minIndex]
17+
}

0 commit comments

Comments
 (0)