Skip to content

Commit 8866c7f

Browse files
committed
feat: add range extraction solution
1 parent 1b6fe37 commit 8866c7f

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

codewars/range-extraction/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Range Extraction
2+
3+
## Description
4+
5+
A format for expressing an ordered list of integers is to use a comma separated list of either
6+
7+
- individual integers
8+
- or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, '-'. The range includes all integers in the interval including both endpoints. It is not considered a range unless it spans at least 3 numbers. For example "12,13,15-17"
9+
10+
Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format.
11+
12+
Example:
13+
14+
```js
15+
solution([
16+
-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18,
17+
19, 20,
18+
]);
19+
// returns "-10--8,-6,-3-1,3-5,7-11,14,15,17-20"
20+
```

codewars/range-extraction/solution.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const solution = (list) => {
2+
let result = '';
3+
let start = list[0];
4+
let end = list[0];
5+
let i = 1;
6+
7+
while (i < list.length) {
8+
if (list[i] - list[i - 1] === 1) {
9+
end = list[i];
10+
} else {
11+
if (start === end) {
12+
result += `${start},`;
13+
} else if (end - start === 1) {
14+
result += `${start},${end},`;
15+
} else {
16+
result += `${start}-${end},`;
17+
}
18+
start = list[i];
19+
end = list[i];
20+
}
21+
i += 1;
22+
}
23+
if (start === end) {
24+
result += `${start}`;
25+
} else if (end - start === 1) {
26+
result += `${start},${end}`;
27+
} else {
28+
result += `${start}-${end}`;
29+
}
30+
31+
return result;
32+
};
33+
34+
module.exports = solution;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const solution = require('./solution');
2+
3+
describe('Range extraction', () => {
4+
const testCases = [
5+
{
6+
input: [-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20],
7+
output: '-6,-3-1,3-5,7-11,14,15,17-20',
8+
},
9+
{
10+
input: [-3, -2, -1, 2, 10, 15, 16, 18, 19, 20],
11+
output: '-3--1,2,10,15,16,18-20',
12+
},
13+
{
14+
input: [-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17,
15+
18, 19, 20],
16+
output: '-10--8,-6,-3-1,3-5,7-11,14,15,17-20',
17+
},
18+
{
19+
input: [-3, -2, -1, 2, 10, 15, 16, 18, 19, 20, 22, 23],
20+
output: '-3--1,2,10,15,16,18-20,22,23',
21+
},
22+
];
23+
24+
it('should return a string', () => {
25+
expect(typeof solution([-1, 0, 1, 2, 6, 7, 9])).toBe('string');
26+
});
27+
28+
it.each(testCases)('should return the correct output', (testCase) => {
29+
expect(solution(testCase.input)).toBe(testCase.output);
30+
});
31+
});

0 commit comments

Comments
 (0)