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

  • Home
  • SEARCH
  • 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 8907865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:01:32+00:00 2026-06-15T03:01:32+00:00

I’m creating a display interface of lets say ‘products’. For this example lets say

  • 0

I’m creating a display interface of lets say ‘products’. For this example lets say the display will show at max 4 products. There are a variable number of sources that the products can came from. The number of sources will never be larger than the number of products to display (at most 4 sources for this example). Each source contains 1 or more products. The goal is to evenly distribute the number of products displayed across the sources.

The logic for 4 products would be handled as follows.

  • If there is 1 source, then 4 items will be selected from that source.
    (4×1)
  • If there are 4 sources, then 1 item will be selected from each
    source. (1×1+1×1+1×1+1×1)
  • If there are 2 sources, then 2 items will be selected from each
    source (2×1+2×1), unless one source only has 1 product, then it will follow as (1×1+3×1)
  • If there are 3 sources, then 1 item will be selected from 2
    sources and 2 items will be selected from 1 source (2×1+1×1+1×1)

Before I start looping to output products, I have a collection of sources and the count of items in each source.

My question is:

What is the simplest way to loop through each source and output the
appropriate amount of products?

Keep in mind that each source may only have 1 product, so it is possible that 4 products cannot be selected.

  • 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-06-15T03:01:33+00:00Added an answer on June 15, 2026 at 3:01 am

    Here is simple Python code to select the products.

    It works by looping over the sources and adding one product at a time.

    This should result in a fair distribution between the sources.

    The code could be made much more efficient if you find it is too slow.

    Z=[1,10,10]       # Number of products available for each source
    Y=len(Z)          # Number of sources
    X=4               # number of products to output
    B=[0]*Y           # Number of products to select for each source
    
    a=0               # Next source to try and take a product from
    while sum(B)<min(sum(Z),X):
        a=(a+1)%Y     # Change to a different source for the next product
        if B[a]<Z[a]: # Check that this source still has products left
            B[a]+=1   # Note that we have taken a product from source a
    
    print B
    

    In this example it prints

    [1, 2, 1]
    

    to signify taking 1 product from the first source, 2 from the second, and 1 from the third.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.