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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:30:20+00:00 2026-06-01T14:30:20+00:00

i need all permutation from N lists, i dont know N until the program

  • 0

i need all permutation from N lists, i dont know N until the program start, here is my SSCCE (i have implemented algorithm which was adviced to me, but it has some bugs).

First , create Place class:

public class Place {
public List<Integer> tokens ;

//constructor
public Place() {

  this.tokens = new ArrayList<Integer>();  
}

}

And then testing class:

public class TestyParmutace {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        List<Place> places = new ArrayList<Place>();

        Place place1 = new Place();
        place1.tokens.add(1);
        place1.tokens.add(2);
        place1.tokens.add(3);
        places.add(place1); //add place to the list

        Place place2 = new Place();
        place2.tokens.add(3);
        place2.tokens.add(4);
        place2.tokens.add(5);
        places.add(place2); //add place to the list

        Place place3 = new Place();
        place3.tokens.add(6);
        place3.tokens.add(7);
        place3.tokens.add(8);
        places.add(place3); //add place to the list


        //so we have
        //P1 = {1,2,3}
        //P2 = {3,4,5}
        //P3 = {6,7,8}


        List<Integer> tokens = new ArrayList<Integer>();

        Func(places,0,tokens);

    }

    /**
     * 
     * @param places list of places
     * @param index index of current place
     * @param tokens list of tokens
     * @return true if we passed guard, false if we did not
     */


    public static boolean Func( List<Place> places, int index, List<Integer> tokens) 

    {

        if (index >= places.size())
            {

                // if control reaches here, it means that we've recursed through a particular combination
                // ( consisting of exactly 1 token from each place ), and there are no more "places" left



                String outputTokens = "";
                for (int i = 0; i< tokens.size(); i++) {

                    outputTokens+= tokens.get(i) +",";
                }
                System.out.println("Tokens: "+outputTokens);

                if (tokens.get(0) == 4 && tokens.get(1) == 5 && tokens.get(2) == 10) {
                    System.out.println("we passed the guard with 3,5,8");
                    return true;
                }

                else {
                    tokens.remove(tokens.get(tokens.size()-1));
                    return false;
                }

            }

        Place p = places.get(index);

        for (int i = 0; i< p.tokens.size(); i++)
            {

                tokens.add(p.tokens.get(i));
                //System.out.println("Pridali sme token:" + p.tokens.get(i));

                if ( Func( places, index+1, tokens ) ) return true;

            }
        if (tokens.size()>0)
            tokens.remove(tokens.get(0));

        return false;

    }
}

and here is the output of this code:

Tokens: 1,3,6,
Tokens: 1,3,7,
Tokens: 1,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,
Tokens: 2,3,6,
Tokens: 2,3,7,
Tokens: 2,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,
Tokens: 3,3,6,
Tokens: 3,3,7,
Tokens: 3,3,8,
Tokens: 3,4,6,
Tokens: 3,4,7,
Tokens: 3,4,8,
Tokens: 4,5,6,
Tokens: 4,5,7,
Tokens: 4,5,8,

So, you see, some combinations are correct (1,3,6), some are incorrect (4,5,8) and some are completely missing (2,4,8,..) how to solve this problem ? number of places and also number of tokens in places can vary, i just used 3 places since with 2 places its working, but with more places it is buggy.
Thanks.

  • 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-01T14:30:21+00:00Added an answer on June 1, 2026 at 2:30 pm

    You algorithm is almost correct. I think you don’t need to return true or false and stop current iteration when you get true. I modified your method Func:

    public static void Func( List<Place> places, int index, Deque<Integer> tokens) {
        if (index == places.size()) {
            // if control reaches here, it means that we've recursed through a particular combination
            // ( consisting of exactly 1 token from each place ), and there are no more "places" left
            String outputTokens = "";
            for (int token : tokens) {
                outputTokens += token + ",";
            }
            System.out.println("Tokens: "+outputTokens);
        } else {
            Place p = places.get(index);
            for (int token : p.tokens) {
                tokens.addLast(token);
                Func(places, index+1, tokens);
                token.removeLast();
            }
        }
    }
    

    I used Deque because it offers handy removeLast method to remove last added token. You can pass LinkedList as implementation of Deque.

    Update

    List<List<Integer>> combinations;
    
    // Instead of printing result:
    List<Integer> copy = new ArrayList<Integer>(tokens);
    combinations.add(copy);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need all records which have year entered from search criteria. for ex: String
I need loging all HTTP request (from any application). I have Delphi 7.0. Anybody
i have a string which has the following format (3,1,Mayer,Jack). I need all 4
I need get all items these have no categories int? categoryId = null; var
I have multiple threads who all need to write to the same Dictionary. I
I have a list of objects. I need all possible permutations of those objects.
I need to generate all permutation of a string with selecting some of the
I have a list of words and I need to generate all possible permutations
I need to generate a Deterministic Finite Automata (DFA), selected from all possible DFAs
I need to generate all combinations (not permutations) in VB .NET, I've been search

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.