There is a list of binary values:
List<bool> myList = new List<bool>(){true, true, false, false, true, false, false, false, true, true, true, false, false};
my algorithm aims to convert any false element to true if they are adjacent to the true value:
result = {true, true, true, true, true, true, false, true, true, true, true, true, false}
My solution works as you will see. I can do this through two different loops, and then pin two lists:
List<bool> firstList = new List<bool>(); List<bool> secondList = new List<bool>(); for(int i=0; i<myList.Count()-1; i++){ if(myList[i]==true){ firstList[i]=true; firstList[i+1]=true; } } for(int i=1; i<myList.Count(); i++){ if(myList[i]==true){ secondList[i]=true; secondList[i-1]=true; } } List<bool> finalList = firstList.Zip(secondList, (a,b)=>a||b).ToList();
However, this does not seem to be the best solution, since the problem looks very simple. Any idea to do this in one loop or preferably using linq?