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 4023582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:36:58+00:00 2026-05-20T10:36:58+00:00

Given some sets (or lists) of numbers, I would like to iterate through the

  • 0

Given some sets (or lists) of numbers, I would like to iterate through the cross product of these sets in the order determined by the sum of the returned numbers. For example, if the given sets are { 1,2,3 }, { 2,4 }, { 5 }, then I would like to retrieve the cross-products in the order

<3,4,5>,
<2,4,5>,
<3,2,5> or <1,4,5>,
<2,2,5>,
<1,2,5>

I can’t compute all the cross-products first and then sort them, because there are way too many. Is there any clever way to achieve this with an iterator?

(I’m using Perl for this, in case there are modules that would help.)

  • 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-20T10:36:58+00:00Added an answer on May 20, 2026 at 10:36 am

    For two sets A and B, we can use a min heap as follows.

    1. Sort A.
    2. Sort B.
    3. Push (0, 0) into a min heap H with priority function (i, j) |-> A[i] + B[j]. Break ties preferring small i and j.
    4. While H is not empty, pop (i, j), output (A[i], B[j]), insert (i + 1, j) and (i, j + 1) if they exist and don’t already belong to H.

    For more than two sets, use the naive algorithm and sort to get down to two sets. In the best case (which happens when each set is relatively small), this requires storage for O(√#tuples) tuples instead of Ω(#tuples).


    Here’s some Python to do this. It should transliterate reasonably straightforwardly to Perl. You’ll need a heap library from CPAN and to convert my tuples to strings so that they can be keys in a Perl hash. The set can be stored as a hash as well.

    from heapq import heappop, heappush
    
    def largest_to_smallest(lists):
      """
      >>> print list(largest_to_smallest([[1, 2, 3], [2, 4], [5]]))
      [(3, 4, 5), (2, 4, 5), (3, 2, 5), (1, 4, 5), (2, 2, 5), (1, 2, 5)]
      """
      for lst in lists:
        lst.sort(reverse=True)
      num_lists = len(lists)
      index_tuples_in_heap = set()
      min_heap = []
      def insert(index_tuple):
        if index_tuple in index_tuples_in_heap:
          return
        index_tuples_in_heap.add(index_tuple)
        minus_sum = 0  # compute -sum because it's a min heap, not a max heap
        for i in xrange(num_lists):  # 0, ..., num_lists - 1
          if index_tuple[i] >= len(lists[i]):
            return
          minus_sum -= lists[i][index_tuple[i]]
        heappush(min_heap, (minus_sum, index_tuple))
      insert((0,) * num_lists)
      while min_heap:
        minus_sum, index_tuple = heappop(min_heap)
        elements = []
        for i in xrange(num_lists):
          elements.append(lists[i][index_tuple[i]])
        yield tuple(elements)  # this is where the tuple is returned
        for i in xrange(num_lists):
          neighbor = []
          for j in xrange(num_lists):
            if i == j:
              neighbor.append(index_tuple[j] + 1)
            else:
              neighbor.append(index_tuple[j])
          insert(tuple(neighbor))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given some arbitrary SQL I would like to get the data types of the
Given some JS code like that one here: for (var i = 0; i
I have been given some generated code which looks like so: <form id=form1 method=post
I am working through some practise problem sets for an upcoming CS exam. I
Given some (English) word that we shall assume is a plural , is it
Given some typical search form, I can't construct this form action when submitting a
I've been given some code with commenting unlike anything I've come across before: //{{{
I have been given some matlab code compiled using the .net compiler. I can
I was looking into sorting tables by a column designated given some input, and
My friend is trying to create a utility function that is given some Type

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.