Please Help me solve this problem i have trying to solve Range extraction

Advertisements

The problem i try to solve is this using js :

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"

so my idea was to use 3 functions :
1- newRangeStart: creates a new Array in results to store the range numbers and puts in it the first element of the range (RangeStarter).

2-olSupp : deletes elements from the list that were used by the 3rd function RangeArr so that we get a new Arr with a new RangeStarter using 1st function.

3-RangeArr : uses the 1st function than adds elements from the list to the array created by it which are consecutive starting from the Range starter, and then uses the 2nd function to delete the elements used from the ol so the next time we use the RangeArr function it creates another range.

By repeating the RangeArr function with a while loop that runs until ol becomes empty we will have a resuts array with arrays inside of it that contains ranges.

now the poblem is when i run RangeArr function it doesn’t delete the used elements from the ol as i want i tried to fix the olSupp function several times but it just doesn’t work i think there is a problem in my entire code pls someone help me to fix it here is my code:

function solution(list){
    // TODO: complete solution

    let ol = [...list];
    let results = [];


    /*This adds a new array for a range by adding the first number of the range to 
    an array (2D array) and stores it in the resuts array  */
    function newRangeStart(orderedlist,result){
        result.push([orderedlist[0]]);
        return result;
    }
    /*This functions takes the ol and deletes elements that are found in the results
    so that the next time we run the newRangeStart function it creates an other array
    for another range with a different start number*/
    function olSupp(orderedlist,result){
        let toRemove = result.flat();
        let newList = [];
        for (let i = 0; i < orderedlist.length; i++) {
            if(!toRemove.includes(orderedlist[i])){
                newList.push(orderedlist[i]);
            }
        }
        orderedlist = [...newList];
        return orderedlist;
    }

    /*Finally RangeArr function creates a range from the ol (ordered list)
    starting by the first element of the results array and then uses olSupp to delete
    the used numbers from the ol */
    function RangeArr (orderedlist,result){
        newRangeStart(orderedlist,result);
        let i = 0;
        while(orderedlist[i+1]- orderedlist[i] == 1 && orderedlist[i+2]- orderedlist[i+1]== 1) {
            result[i].push(orderedlist[i+1],orderedlist[i+2]);
            i = i+1;
        }
        olSupp(orderedlist,result);
        return result;        
    }

    /*we execute the RangeArr function until ol becomes emepty
    and this will give us multiple arrays in the result array containing
    the elements of each range found in the ol */
    //PS: i didnt put the code beacuse it causes an infinte loop using while


    RangeArr(ol,results);
    console.log(ol,results);
    
}   

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

>Solution :

const solution = arr => {
    let s = null, d = 0;

    const result = arr.sort((a, b) => a - b).reduce((p, c, i, arr) => {
        d++;
        if (!s) s = c;
        if (arr[i + 1] - s > d) {
            s === c ? p.push(s) : d < 3 ? p.push(s, c) : p.push(`${s}-${c}`);
            s = null;
            d = 0;
        }
        if (arr[i + 1] === undefined) s === c ? p.push(s) : p.push(`${s}-${c}`);

        return p;
    }, []);

    return result;
}

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]));

Leave a ReplyCancel reply