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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:55:53+00:00 2026-05-19T01:55:53+00:00

Here is a function I would like to write but am unable to do

  • 0

Here is a function I would like to write but am unable to do so. Even if you
don’t / can’t give a solution I would be grateful for tips. For example,
I know that there is a correlation between the ordered represantions of the
sum of an integer and ordered set partitions but that alone does not help me in
finding the solution. So here is the description of the function I need:

The Task

Create an efficient* function

List<int[]> createOrderedPartitions(int n_1, int n_2,..., int n_k)

that returns a list of arrays of all set partions of the set
{0,...,n_1+n_2+...+n_k-1} in number of arguments blocks of size (in this
order) n_1,n_2,...,n_k (e.g. n_1=2, n_2=1, n_3=1 -> ({0,1},{3},{2}),...).

Here is a usage example:

int[] partition = createOrderedPartitions(2,1,1).get(0);
partition[0]; // -> 0
partition[1]; // -> 1
partition[2]; // -> 3
partition[3]; // -> 2

Note that the number of elements in the list is
(n_1+n_2+...+n_n choose n_1) * (n_2+n_3+...+n_n choose n_2) * ... *
(n_k choose n_k)
. Also, createOrderedPartitions(1,1,1) would create the
permutations of {0,1,2} and thus there would be 3! = 6 elements in the
list.

* by efficient I mean that you should not initially create a bigger list
like all partitions and then filter out results. You should do it directly.

Extra Requirements

If an argument is 0 treat it as if it was not there, e.g.
createOrderedPartitions(2,0,1,1) should yield the same result as
createOrderedPartitions(2,1,1). But at least one argument must not be 0.
Of course all arguments must be >= 0.

Remarks

The provided pseudo code is quasi Java but the language of the solution
doesn’t matter. In fact, as long as the solution is fairly general and can
be reproduced in other languages it is ideal.

Actually, even better would be a return type of List<Tuple<Set>> (e.g. when
creating such a function in Python). However, then the arguments wich have
a value of 0 must not be ignored. createOrderedPartitions(2,0,2) would then
create

[({0,1},{},{2,3}),({0,2},{},{1,3}),({0,3},{},{1,2}),({1,2},{},{0,3}),...]

Background

I need this function to make my mastermind-variation bot more efficient and
most of all the code more “beautiful”. Take a look at the filterCandidates
function in my source code. There are unnecessary
/ duplicate queries because I’m simply using permutations instead of
specifically ordered partitions. Also, I’m just interested in how to write
this function.

My ideas for (ugly) “solutions”

Create the powerset of {0,...,n_1+...+n_k}, filter out the subsets of size
n_1, n_2 etc. and create the cartesian product of the n subsets. However
this won’t actually work because there would be duplicates, e.g.
({1,2},{1})...

First choose n_1 of x = {0,...,n_1+n_2+...+n_n-1} and put them in the
first set. Then choose n_2 of x without the n_1 chosen elements
beforehand
and so on. You then get for example ({0,2},{},{1,3},{4}). Of
course, every possible combination must be created so ({0,4},{},{1,3},{2}),
too, and so on. Seems rather hard to implement but might be possible.

Research

I guess this
goes in the direction I want however I don’t see how I can utilize it for my
specific scenario.

http://rosettacode.org/wiki/Combinations

  • 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-19T01:55:53+00:00Added an answer on May 19, 2026 at 1:55 am

    You know, it often helps to phrase your thoughts in order to come up with a solution. It seems that then the subconscious just starts working on the task and notifies you when it found the solution. So here is the solution to my problem in Python:

    from itertools import combinations
    
    def partitions(*args):
        def helper(s, *args):
            if not args: return [[]]
            res = []
            for c in combinations(s, args[0]):
                s0 = [x for x in s if x not in c]
                for r in helper(s0, *args[1:]):
                    res.append([c] + r)
            return res
        s = range(sum(args))
        return helper(s, *args)
    
    print partitions(2, 0, 2)
    

    The output is:

    [[(0, 1), (), (2, 3)], [(0, 2), (), (1, 3)], [(0, 3), (), (1, 2)], [(1, 2), (), (0, 3)], [(1, 3), (), (0, 2)], [(2, 3), (), (0, 1)]]
    

    It is adequate for translating the algorithm to Lua/Java. It is basically the second idea I had.

    The Algorithm

    As I already mentionend in the question the basic idea is as follows:

    First choose n_1 elements of the set s := {0,...,n_1+n_2+...+n_n-1} and put them in the
    first set of the first tuple in the resulting list (e.g. [({0,1,2},... if the chosen elements are 0,1,2). Then choose n_2 elements of the set s_0 := s without the n_1 chosen elements beforehand and so on. One such a tuple might be ({0,2},{},{1,3},{4}). Of
    course, every possible combination is created so ({0,4},{},{1,3},{2}) is another such tuple and so on.

    The Realization

    At first the set to work with is created (s = range(sum(args))). Then this set and the arguments are passed to the recursive helper function helper.

    helper does one of the following things: If all the arguments are processed return "some kind of empty value" to stop the recursion. Otherwise iterate through all the combinations of the passed set s of the length args[0] (the first argument after s in helper). In each iteration create the set s0 := s without the elements in c (the elements in c are the chosen elements from s), which is then used for the recursive call of helper.

    So what happens with the arguments in helper is that they are processed one by one. helper may first start with helper([0,1,2,3], 2, 1, 1) and in the next invocation it is for example helper([2,3], 1, 1) and then helper([3], 1) and lastly helper([]). Of course another "tree-path" would be helper([0,1,2,3], 2, 1, 1), helper([1,2], 1, 1), helper([2], 1), helper([]). All these "tree-paths" are created and thus the required solution is generated.

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

Sidebar

Related Questions

In python, you can have a function return multiple values. Here's a contrived example:
I would like to have a function that can wrap any other function call.
I would like to write a recursive PHP function to retrive all the children
What is the function of the namespace here? I would think in this simple
So what I'm looking for here is something like PHP's print_r function. This is
How do I write the getDB() function and use it properly? Here is a
I would like to write a few Unix scripts in Emacs Lisp. However, there
Someone posted a great little function here the other day that separated the full
Here is my function ( updated ): Public Shared Function shortenUrl(ByVal URL As String)
here is some sample javascript: SomeObjectType = function() { } SomeObjectType.prototype = { field1:

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.