I have a list List A consisted of strings {"a", "b", "c", "d", "e"}.
My program runs in iterations and for each iteration I want to create a new list List B which is going to contain the same strings, but each of them should move to one position to the left. Here is the example of what the List B should look like in first three iterations:
- iteration, the List B should be:
listB = {"a", "b", "c", "d", "e"} - iteration, the List B should be:
listB = {"b", "c", "d", "e", "a"} - iteration, the List B should be:
listB = {"c", "d", "e", "a", "b"}
and so on…
I have achieved the desired functionality with the following method:
private List<string> CalculateQueueOrder(List<string> listA, int iterationNum)
{
int listACount = listA.Count;
List<string> listB = new List<string>();
for (int i = 0; i < listACount; i++)
{
for (int j = 0; j < listACount; j++)
{
int order = ((j - iterationNum) % listACount + 1);
if (order == i)
{
string listItem = listA[j];
listB.Add(listItem);
break;
}
}
}
return listB;
}
However, there is an issue with this method. In time as the number of iterations increases, the j - iterationNum starts returning negative values, which makes modulus start returning negative values as well. The whole function fails. I need to make the modulus always return the positive value, like it does in Microsofot Excel (mod function).
Could you help me out fixing the formula for int order? Thanks.
I achieved this here: http://rextester.com/HXACA68585
Method is:
Which you can stuff back into a new list if you wanted:
But to test your required results used this:
Output as follows: