I'm trying to slice a List of strings (size N) and return a range based on the list being sliced into equal parts (X).
So for instance, if I have a list of say 10 elements, and my number of tiers is 5.
Elements 0 and 1 are tier 1. Elements 2 and 3 are tier 2. At the end of the method I return the tier specified in the params.
What I'm struggling with is if the list count isn't divisible by the number of tiers. For instance, 23 / 5 = 4.6. So that means they'll be 5 sets of 4, and then 3 left over. I'd like the result to be 5 tiers of 5, 5, 5, 5, 3 (with the final tier just the remaining number of elements).
I've included my code so far, but I'm really stuck on how to ensure the list sizes are as equal as possible and how to handle remainders.
// Gets a list and returns a range by the tier specified
public List<string> GetRangeByTierIndex(List<string> listToDivide, int numOfTiers, int tierIndexToGet)
{
int numOfElementsPerList = listToDivide.Count / numOfTiers;
int index = (tierToGet - 1) * numOfElementsPerList;
return listToDivide.GetRange(index, numOfElementsPerList);
}
Note: Forgot to mention, I can't use LINQ for this either (AOT and iOS problems).