I've two lists and I assign one of them this way:
var query = Enumerable.Range(0, 1440).Select((n, index) =>
{
if ((index >= 525 && index <= 544) || (index >= 600 && index <= 749) || (index >= 810 && index <= 1079) || (index >= 1300 && index <= 1439))
return 0;
else if (index >= 1080 && index <= 1299)
return 1;
else if (index >= 545 && index <= 599)
return 3;
else if (index >= 750 && index <= 809)
return 4;
else
return 2;
}).ToList();
My second list is named lst2. I want to assign it "0" or "1" depending on my first list query. So, if query is "1" or "2", lst2's same indices and previous indices that are "0" value, should be "1". If the query list is "3" or "4", lst2's same indices and previous indices that are "1" value, should be "0". In addition, if the query's first indice(s) has "3" or "4" value, then lst2's same indice(s) should be "0". For example;
query = {3,3,3,0,0,0,2,2,0,0,0,0,4,4,4,4,0,0,0,0,1,1,1,0,0,2,2,0,0,4,4}
lst2 = {0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0}
How can I do this?
EDIT: If query's any indice has 3 or 4 value, lst2's same indice must have 0 value. If query's any indice has 3 or 4 value AND previous indice has 0 value, lst2's same indices must have 0 value. Likewise; If query's any indice has 1 or 2 value, lst2's same indice must have 1 value. If query's any indice has 1 or 2 value AND previous indice has 0 value, lst2's same indices must have 1 value.
An alternative approach, only filling the tracing 0's when needed. Performance wise it's still best to loop reversed, but I only just saw T_D already did that, so as far as the lookahead -> lookback goes, the below is basically the same, but with other syntax and a different trailing 0 filler.
var arr = new int[query.Count];
int cnt = query.Count - 1, toappendindex = -1;
Func<int,int,int> getval = (ind, val) =>
{
if (val == 3 || val == 4) return 0;
if (val == 2 || val == 1) return 1;
if (ind == cnt) return -1;
return arr[ind + 1];
};
for (int ind = cnt; ind >= 0; ind-- )
{
if ((arr[ind] = getval(ind,query[ind])) == -1)
toappendindex = ind; //only if there are trailing 0's
}
if (toappendindex > 0)
for (; toappendindex < arr.Length; toappendindex++) arr[toappendindex] = arr[toappendindex - 1];
//var lst2 = arr.ToList(); if list is needed instead of array, otherwise arr could be used directly