Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7188821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:10:05+00:00 2026-05-28T19:10:05+00:00

In a similar question I asked how to distributed integers using weights. I’m curious

  • 0

In a similar question I asked how to distributed integers using weights. I’m curious how one would approach this problem if a minimum value for each distribution “bucket” was imposed. By imposing a minimum value this seems to be a much more difficult problem. Here is my greedy attempt, which doesn’t work:

def distribute(available, weights_and_mins):
    distributed_amounts = []
    total_weight = sum([i[0] for i in weights_and_mins])
    for weight, minimum in weights_and_mins:
        weight = float(weight)
        p = weight / total_weight
        distributed_amount = round(p * available)
        if distributed_amount < minimum:
            distributed_amount = minimum
        distributed_amounts.append(distributed_amount)
        available -= distributed_amount
        total_weight -= weight
    return [int(i) for i in distributed_amounts]

print distribute(10, ((10,1), (2,5), (2,4)))
print distribute(1000, ((10,1), (2,5), (2,4)))

Currently the values get distributed as [7, 5, 4], which is 16 which is 6 more than we have to distribute. The output should be [1, 5, 4] since this satisfies the minimum requirements for all columns. As the value we have to distribute grows, the distributions should be closer and closer to the correct weighted distribution. For example, by distributing 1000 the algorithm correctly distributes the values as [714, 143, 143].

As a side note, my purpose is to distribute available space (width) among several columns. All columns have a minimum size needed to “get by” and display at least some of their data, and some columns are more in need of space as the available space grows. I mention this as one real life use for this algorithm, but I don’t want this to be a discussion of GUI design.

What are some solutions to this problem? The simpler the better.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:10:06+00:00Added an answer on May 28, 2026 at 7:10 pm

    You should first allocate minimum amounts first and update accordingly. Later you can allocate the remaining amount accordingly.

    prior_available = available
    allocated = [i[1] for i in weights_and_mins]
    available = available - sum(allocated)
    if available < 0:
        The hell breaks loose
    total_weight = float(sum([i[0] for i in weights_and_mins]))
    for i in len(weights_and_min):
        v = round( weights_and_min[i][0]*prior_available/total_weight )
        nv = min( available, max(v-allocated[i],0) )
        allocated[i] += nv
        available -= nv
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a similar question about this previously, but I did not specify that
I realize a 'similar' question was asked but the other user is using a
I've already asked a similar question but this is slightly different so i Thought
I am asking this question on SO because a similar question was asked here
This question, asked a year ago, is similar: Does the Entity Framework 4 support
I realize that there was a similar question asked here , but this is
Question 828421 asked similar question, but received only one real answer (update rubygems) and
A similar question was already asked, but for Ubuntu. It would help to know
I saw a similar question asked and answered for ASP.net here How do I
A similar question was already asked ( Performing a Stress Test on Web Application?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.