DEV Community

Riches
Riches

Posted on

Range Extraction "Codewars"

A format for expressing an ordered list of integers is to use a comma separated list of either

individual integers
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"
Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format.

Example:

solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]);
// returns "-10--8,-6,-3-1,3-5,7-11,14,15,17-20"

Steps:

  1. So we will start by creating four variables. the first one will be our result array. the second one will hold our starting range, the third one will hold our ending range. and the last one will hold a counter. This counter will be used to check if the range is more than 2 numbers. the initial number will be 1.
  2. let create a loop that will start from index 1 instead of 0. the starting range we created in step 1 will have a value of the index 0 of the array.
  3. Now inside the loop we do some checks to get our range if any. firstly we create a variable for the current value and a variable for the previous value.
  4. if the current value - previous value is == 1.endRange is now current value, we also increase the counter value by 1.
  5. if it is not.
    1. we check if the startRange == endRange. then we push the startRange to the result array.
    2. else.
      1. we also check if the counter value is greater than two. if yes we format the range with the '-' and push it to the result array.
      2. if not we move the startRange and the endRange to the result array. finally we set the counter to 1.

after the Loop we have to repeat the option 5 to the end.

finally we return the joined version of the result array.

Algorithm.

function solution(list) {
    let rangeArray = [];
    let startRange = list[0]; // Initialize startRange with the first element of the list
    let endRange = startRange; // Initialize endRange with the same value as startRange
    let counter = 1
    for (let index = 1; index < list.length; index++) {
        const currentNumber = list[index];
        const prevNumber = list[index - 1];
        if (currentNumber - prevNumber === 1) {
            endRange = currentNumber; // Extend the current range
            counter++; //
        } else {
            // Check if it's a single number or a range
            if (startRange === endRange) {
                rangeArray.push(startRange.toString());
            } else {
                if(counter > 2){
                    rangeArray.push(startRange + '-' + endRange);
                }else{
                    rangeArray.push(startRange.toString());
                    rangeArray.push(endRange.toString());
                }
                counter = 1
            }
            startRange = currentNumber; // Start a new range
            endRange = currentNumber;
        }
    }
    // Handle the last range or single number
    if (startRange === endRange) {
        rangeArray.push(startRange.toString());
    } else {
        if(counter > 2){
            rangeArray.push(startRange + '-' + endRange);
        }else{
            rangeArray.push(startRange.toString());
            rangeArray.push(endRange.toString());
        }
    }
    return rangeArray.join(",");
}

console.log(solution([-10, -9, -8, -6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20]));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)