Update Array Recursively

Hi,

I am trying to update an array of CDTs recursively.

Base Array : 

{
{name:"Alex",rollNumber:null},
{name:"Stephen",rollNumber:3},
{name:"Dave",rollNumber:4},
{name:"Steave",rollNumber:5},
{name:"Rosey",rollNumber:7},
{name:"John",rollNumber:8},
{name:"Nile",rollNumber:10}
}

Now, If I insert number 4  where rollNumber is null , if that number exists in CDT array then all numbers greater than 4 should increase by1 until all duplicates are change to unique number, but all numbers less than 4 should remain unchanged.

This is how numbers should change :

4->5,

5->6

Since there are no duplicates after 6 then increment should stop here,

Updated Array: {
{name:"Stephen",rollNumber:3},
{name:"Alex",rollNumber:4},
{name:"Dave",rollNumber:5},
{name:"Steave",rollNumber:6},
{name:"Rosey",rollNumber:7},
{name:"John",rollNumber:8},
{name:"Nile",rollNumber:10}
},

Please help me with it

  Discussion posts and replies are publicly visible

Parents
  • Like a lot of problems, you can break this down into smaller problems to solve.

    1. solve how to take you array and insert ONE new value into it
      1. solve inserting a value that doesn't yet exist in the array
      2. solve inserting a value that does exist in the array
    2. solve how to invoke the above for a list of values

    For 1 you can write an expression rule that takes as its input your array of integers that you want to modify and a single integer that you want to insert into the array. 

    For 2 there is the reduce() function which you can use to recursively call your new expression.

Reply
  • Like a lot of problems, you can break this down into smaller problems to solve.

    1. solve how to take you array and insert ONE new value into it
      1. solve inserting a value that doesn't yet exist in the array
      2. solve inserting a value that does exist in the array
    2. solve how to invoke the above for a list of values

    For 1 you can write an expression rule that takes as its input your array of integers that you want to modify and a single integer that you want to insert into the array. 

    For 2 there is the reduce() function which you can use to recursively call your new expression.

Children