gulbaek
gulbaek

Reputation: 2541

Evenly distribute collection into new Arrays

I'm trying to create an method to evenly distribute an array into X numbers of new arrays, where there is only allowed 15 items pr array, and you are only allowed to create a new array, if the previous have 10 items, except if the array has less than 10 items.

EDIT

To make my question more understandable for future readers.

How many employees do you need and how do you share the work load between them?

END EDIT

 Max allowed number in array = 15;
 Min allowed number in array = 10;    

 Number = Numbers of Items in the Collection.

 Number  5 =>   [5]
 Number 13 =>   [13]
 Number 16 =>   [10] [6]
 Number 29 =>   [15] [14]
 Number 30 =>   [15] [15]
 Number 31 =>   [11] [10] [10]
 Number 32 =>   [12] [10] [10]
 Number 33 =>   [11] [11] [11]

I'm trying to solve this in C#.

This is my code so far, but it fails at numbers like 16 = [16], 29 = [19][10], 38 = [18][10][10]

const int maxAllowedOrderLines = 15;
const int minAllowedOrderLines = 10;
var optimalOrderDisp = new List<int>();

Console.WriteLine("Number of OrderLines");
int linjer = Convert.ToInt32(Console.ReadLine());

if (linjer <= maxAllowedOrderLines)
   optimalOrderDisp.Add(linjer);
else
{
   for (var i = maxAllowedOrderLines; i > 0; i--)
   {
      var maxOrderLines = linjer%i;
      if (maxOrderLines == 0 || i <= minAllowedOrderLines || linjer < maxAllowedOrderLines)
      {
         Console.WriteLine("Optimal number of order lines {0}--{1}", i, (double) linjer/(double) i);

          var optimalNumberOfOrders = linjer/i;
          for (var orderNumber = 0; orderNumber < optimalNumberOfOrders; orderNumber++)
          {
             optimalOrderDisp.Add(i);
          }

          if (maxOrderLines != 0)
             optimalOrderDisp[0] += maxOrderLines;
          break;
       }
    }
 }
 foreach (var i1 in optimalOrderDisp)
 {
    Console.Write("[{0}]", i1);
 }
 Console.WriteLine();

Upvotes: 2

Views: 2203

Answers (2)

Jodrell
Jodrell

Reputation: 35696

Erm ...

const double bucketSize = 15.0;
var totalItems = (double)linjer;
var optimumBuckets = Math.Ceiling(totalItems / bucketSize);
var itemsPerBucket = (int)Math.Ceiling(totalItems / optimumBuckets);

var buckets = new int[(int)optimumBuckets];

var itemsLeft = (int)totalItems
for (var i = 0; i < buckets.length; i++)
{
    if (itemsLeft < itemsPerBucket)
    {
        buckets[i] = itemsLeft;
    }
    else
    {
        buckets[i] = itemsPerBucket;
    }
    itemsLeft -= itemsPerBucket;
}

seems to do what you want.

Upvotes: 5

Simon Ejsing
Simon Ejsing

Reputation: 1495

Fun question. I've given it a go:

    const int maxAllowedOrderLines = 15;
    const int minAllowedOrderLines = 10;

    static List<int> optimalOrderDisp = new List<int>();

    static void Main(string[] args)
    {
        int Lines = Convert.ToInt32(Console.ReadLine());

        int MinNumberOfBuckets = (int) Math.Ceiling((double) Lines / minAllowedOrderLines);
        int RemainingLines = Lines;
        int BucketLines = Lines / MinNumberOfBuckets;

        // Distribute evenly
        for (int i = 0; i < MinNumberOfBuckets; i++)
        {
            optimalOrderDisp.Add(i != MinNumberOfBuckets - 1 ? BucketLines : RemainingLines);
            RemainingLines -= BucketLines;
        }

        // Try to remove first bucket
        while (RemoveBucket())
        {
        }

        // Re-balance
        Lines = optimalOrderDisp.Sum();
        RemainingLines = Lines;
        BucketLines = (int) Math.Round((double) Lines / (optimalOrderDisp.Count));
        for (int i = 0; i < optimalOrderDisp.Count; i++)
        {
            optimalOrderDisp[i] = (i != optimalOrderDisp.Count - 1 ? BucketLines : RemainingLines);
            RemainingLines -= BucketLines;
        }

        // Re-balance to comply to min size
        for (int i = 0; i < optimalOrderDisp.Count - 1; i++)
            if (optimalOrderDisp[i] < minAllowedOrderLines)
            {
                int delta = minAllowedOrderLines - optimalOrderDisp[i];

                optimalOrderDisp[i] += delta;
                optimalOrderDisp[optimalOrderDisp.Count - 1] -= delta;
            }

        Console.WriteLine(String.Join("\n", optimalOrderDisp.ToArray()));
    }

    static bool RemoveBucket()
    {
        if (optimalOrderDisp.Sum() > maxAllowedOrderLines * (optimalOrderDisp.Count - 1))
            return false;

        int Lines = optimalOrderDisp[0];
        int RemainingLines = Lines;
        int BucketLines = Lines / (optimalOrderDisp.Count - 1);

        // Remove bucket and re-distribute content evenly
        // Distribute evenly
        for (int i = 1; i < optimalOrderDisp.Count; i++)
        {
            optimalOrderDisp[i] += (i != optimalOrderDisp.Count - 1 ? BucketLines : RemainingLines);
            RemainingLines -= BucketLines;
        }

        optimalOrderDisp.RemoveAt(0);
        return true;
    }

Upvotes: 2

Related Questions