I'm a bit stuck in this:
Let's say I want to have 45 items of something, but the maximum stack I can have of that item is 10. So I want a collection that returns me [10, 10, 10, 10, 5] based on that.
This is what I currently have. (yes, I have to use unsigned int16 for my project, but it doesn't matter if your example is regular integer).
ushort Ceiling = (ushort)Math.Ceiling((double)Amount / (double)MaxStackSize);
ushort[] Sizes = new ushort[Ceiling];
for (ushort i = 0; i < Ceiling; i++)
{
if (Amount != MaxStackSize && i == Ceiling - 1)
{
Sizes[i] = (ushort)(Amount % MaxStackSize);
}
else
{
Sizes[i] = MaxStackSize;
}
}
It works to a certain degree, but sometimes the module will return 0. (for example 10 % 10 or 30 % 10). Or maybe there's a better way to do this instead of what I'm doing. I'll appreciate the help!
Using LINQ the next approach can be used:
using System.Linq;
ushort Amount = 45;
ushort MaxStackSize = 10;
ushort div = (ushort) (Amount / MaxStackSize);
ushort mod = (ushort) (Amount % MaxStackSize);
ushort[] Sizes;
if (mod == 0)
{
Sizes = Enumerable.Repeat<ushort>(MaxStackSize, div).ToArray();
}
else
{
Sizes = Enumerable.Repeat<ushort>(MaxStackSize, div).Concat(new ushort[] {mod}).ToArray();
}
Here is complete sample.
UPDATE:
@Gimly pointed out in the comment:
I wouldn't even put the else, the first part in both your if and else is repeated, so you could always do the first part and only do the Concat part in the if (reversed).
Here is a modified solution, using @Gimly's hint:
using System.Collections.Generic;
using System.Linq;
ushort Amount = 45;
ushort MaxStackSize = 10;
ushort div = (ushort) (Amount / MaxStackSize);
ushort mod = (ushort) (Amount % MaxStackSize);
IEnumerable<ushort> result = Enumerable.Repeat<ushort>(MaxStackSize, div);
if (mod != 0)
result = result.Concat(new ushort[] {mod});
ushort[] Sizes = result.ToArray();
Here is complete sample.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With