-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRangeExtraction.ts
More file actions
65 lines (62 loc) · 1.92 KB
/
RangeExtraction.ts
File metadata and controls
65 lines (62 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// https://www.codewars.com/kata/51ba717bb08c1cd60f00002f/solutions/javascript
function solution(list: any[]) {
if(list.length == 0 ){
return [];
}
let rangeStart: number | undefined;
let last: number | undefined;
let result = [];
for (const i of list) {
console.log(`${rangeStart}, ${last}, ${i}`);
if(!rangeStart){
rangeStart = i;
last = i;
}
else{
if(last != undefined){
if (i - last > 1){
if (rangeStart == last) {
result.push(rangeStart);
}
else if(last - rangeStart == 1){
result.push(rangeStart);
result.push(last);
}
else{
result.push(rangeStart + '-' + last);
}
rangeStart = i;
}
else if(i - last <= 0){
throw new Error("order error!");
}
last = i;
}
else{
throw new Error("no last!" + i);
}
}
}
if(rangeStart == undefined || last == undefined){
return [];
}
if (rangeStart == last) {
result.push(rangeStart);
}
else if (last - rangeStart == 1) {
result.push(rangeStart);
result.push(last);
}
else{
result.push(rangeStart + '-' + last);
}
return result.join(',');
}
// const result = solution([-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);
// const result = solution([-6, -3, -2, -1, 0]);
// const result = solution([-6, -3, -2, -1, 0, 1]);
// const result = solution([1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);
// console.log(result);
// const result = solution([-72, -71]);
// console.log(result);
// console.log(solution([71, 72]));