• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Dividing an amount evenly within limits

Engaged ,
Dec 14, 2009 Dec 14, 2009

Copy link to clipboard

Copied

I often need to divide quantities in recipes.  Normally, this is straightforward:  I have an array of items to divide and a total, so I can do something like

public function divideAmounts(totalAmount:Number,coll:XMLListCollection):void {      var divisor:Number = coll.length;      var amtEach:Number = Math.round((totalAmount/divisor)/ .01) * .01;      trace("dividing "+totalAmount+" by "+divisor+" for an amount each of "+amtEach);      for (var i:int=0; i<divisor; i++) {            coll.@amount = amtEach;      } }

However, this time I have been handed a curve ball: some of the items have a maximum amount they can be.

For example, item A has a maximum amount of .25, item B is maximum of 1, the other items have no limits.

So if the total amount is 3, and I am dividing it by 3 items I can't simply set each one's amount to 1 (as I normally would, as the code above does). I need to set the amount of A to .25 so there is still the .75 "left over" that I now need to apply evenly to the remaining items.

So that means the other 2 items B and C would be 1.375 each -- but, as I said above B also has a limit (of 1), so B would be 1 and C (no limits) would get the whole .75 remainder, and be 1.75.  So A=.25, B= 1, C=1.75 for the total of 3.

My question is, even conceptually, how would I approach a problem such as this in AS?

I tried

public function divideAmountsWithMaximums(totalAmount:Number,coll:XMLListCollection):void {      var divisor:Number = coll.length;      var amtEach:Number = Math.round((totalAmount/divisor)/ .01) * .01;      for (var i:int=0; i<divisor; i++) {            var max:Number = coll.@maxQty;           var amtToAdd:Number = (amtEach > max)? max :amtEach;           trace(coll.@sn+" has a max of "+coll.@maxQty+", adding "+amtToAdd);           coll.@qty = amtToAdd;      } }

Except that doesn't cascade in the proper order, "filling up" the smaller items first.

Help and suggestions appreciated.

TOPICS
ActionScript

Views

382

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 14, 2009 Dec 14, 2009

Copy link to clipboard

Copied

LATEST

the exact constraints aren't clear but if you're trying to assign non-zero values to the maximum number of objects and spread the values as evenly as possible:

1.  calculate the amount to be allocated to each item if there were no maximum values.

2.  then start assigning values to those unassigned with the smallest max first, the next smallest next,..,largest max and finally those with no max.

3.  if any of the maxes restrict the value allocated, recalculate the amount to be allocated to each item if there were no max value.

4.  back to 2.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines