'How to loop over a list "inwards"?
I have a list with objects that have a weight-property. I'm trying to rearrange the objects in the list, so that the weights are distributed evenly over the 2 halves (halves being left & right. So for example, if the list is 4 items long, indexes 0 and 1 are the left half and indexes 2 and 3 are the left half).
I'm trying to jump from (list of 4 items) index 0 -> 3 -> 1 -> 2, or maybe even 0 -> 3 -> 2 -> 1, which ever distributes the weights more evenly.
I tried messing around but haven't been able to come up with something working. (It should also work for lists with an odd number of items.)
bool left = true;
List<Column> orderedColumns = _columns.OrderByDescending(x => x._totalWeight).ToList();
for (int i = 0; i < _columns.Count; i++)
{
Column columnToPlace = orderedColumns[0];
int index = i;
if (!left)
{
index = _columns.Count - i;
}
if (left && i > 0)
{
index--;
}
_columns[index] = columnToPlace;
orderedColumns.RemoveAt(0);
left = !left;
}
Solution 1:[1]
This splits the weights (or objects) instead of providing the indexes, but you could do that with some modifications if that is what you need.
The idea is to sort the weights, then match the lightest weights with the heaviest weights. With an odd number of weights, you add that to the lightest side.
var sortedWeights = weights.OrderBy(w => w).ToList();
var wc = weights.Length;
var halfSubCount = wc/4;
var left = sortedWeights.Take(halfSubCount).Concat(sortedWeights.TakeLast(halfSubCount)).ToList();
var right = sortedWeights.Skip(halfSubCount).Take(halfSubCount).Concat(sortedWeights.TakeLast(halfSubCount*2).Take(halfSubCount)).ToList();
if (wc % 2 == 1) {
var middleWeight = sortedWeights[wc / 2];
if (left.Sum() <= right.Sum())
left.Add(middleWeight);
else
right.Add(middleWeight);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | NetMage |
