3

I have two arrays (with same length), the size is variable.

To do simplest example I have two arrays with length=3.

Persons -> P1, P2, P3
Seats -> Sa, Sb, Sc

I need generate all combinations Cn

If person P1 take Sa, is possible that:

  • person P2 take Sb then P3 will take Sc. The combination will be P1Sa, P2Sb, P3Sc.
  • or Person P2 take Scthen P3 will take Sb. The combination will be P1Sa, P2Sc, P3Sb.

Now Here all possible combinations.

C0 -> P1Sa, P2Sb, P3Sc
C1 -> P1Sa, P2Sc, P3Sb
C2 -> P1Sb, P2Sa, P3Sc
C3 -> P1Sb, P2Sc, P3Sa
C4 -> P1Sc, P2Sa, P3Sb
C5 -> P1Sc, P2Sb, P3Sa

This is my bad design.

  private void recursiveSeatPerson(String c, List<String> p, List<String> s) {
    if (p.size() > 1) {
      for (int i = 0; i < s.size(); i++) {
        String combination = p.get(0)+s.get(i) + ", " + c;
        System.out.print(combination);

        List<String> new_s = new ArrayList<>();
        for (int index_s = 0; index_s < s.size(); index_s++) {
          if (index_s != i) {
            new_s.add(s.get(i));
          }
        }
        List<String> new_p = new ArrayList<>();
        for (int index_p = 1; index_p < p.size(); index_p++) {
          new_p.add(p.get(index_p));
        }

        recursiveSeatPerson(combination, new_p, new_s);

      }
    } else {
      System.out.print(c + p.get(0)+s.get(0) + " ");
      System.out.println();
    }
  }

This is my Testing.

List<String> persons = Arrays.asList("P1", "P2", "P3");
List<String> seats = Arrays.asList("Sa", "Sb", "Sc");
recursiveSeatPerson("", persons, seats);

This is my results:

P1Sa, P2Sa, P1Sa, P2Sa, P1Sa, P3Sa 
P2Sa, P1Sa, P2Sa, P1Sa, P3Sa 
P1Sb, P2Sb, P1Sb, P2Sb, P1Sb, P3Sb 
P2Sb, P1Sb, P2Sb, P1Sb, P3Sb 
P1Sc, P2Sc, P1Sc, P2Sc, P1Sc, P3Sc 
P2Sc, P1Sc, P2Sc, P1Sc, P3Sc 

Now sending empty String like argument in this line:

recursiveSeatPerson("", new_p, new_s);

This is the bad result (again).

P1Sa, P2Sa, P3Sa 
P2Sa, P3Sa 
P1Sb, P2Sb, P3Sb 
P2Sb, P3Sb 
P1Sc, P2Sc, P3Sc 
P2Sc, P3Sc 

How solve my code?

Chepe Questn
  • 562
  • 3
  • 13
  • https://stackoverflow.com/questions/32438350/python-merging-two-lists-with-all-possible-permutations/32438848 – btilly Apr 12 '18 at 22:04
  • Possible duplicate of [Python merging two lists with all possible permutations](https://stackoverflow.com/questions/32438350/python-merging-two-lists-with-all-possible-permutations) – Prune Apr 13 '18 at 00:58
  • Thank you but: I was waiting for pseudo-code, to pass it to Java, but since I have never touched python, I did not understand the logic of the algorithm. Due to changes to my question using *Java*, then answers in Python are not applicable. – Chepe Questn Apr 13 '18 at 01:37

1 Answers1

2

Working code:

private void recursiveSeatPerson(String snippet, List<String> p, List<String> s) {
  if (p.size() > 1) {
    for (int i = 0; i < s.size(); i++) {
      String combination = snippet + p.get(0) + s.get(i) + ", ";

      List<String> new_s = new ArrayList<>();
      for (int index_s = 0; index_s < s.size(); index_s++) {
        if (index_s != i) {
          new_s.add(s.get(index_s));
        }
      }
      List<String> new_p = new ArrayList<>();
      for (int index_p = 1; index_p < p.size(); index_p++) {
        new_p.add(p.get(index_p));
      }
      recursiveSeatPerson(combination, new_p, new_s);
    }
  } else {
    System.out.println(snippet + p.get(0)+s.get(0) + " ");
  }
}

Testing with a 4 length arrays.

List<String> persons = Arrays.asList("P1", "P2", "P3", "P4");
List<String> seats = Arrays.asList("Sa", "Sb", "Sc", "Sd");
recursiveSeatPerson("", persons, seats);

Check the output:

P1Sa, P2Sb, P3Sc, P4Sd 
P1Sa, P2Sb, P3Sd, P4Sc 
P1Sa, P2Sc, P3Sb, P4Sd 
P1Sa, P2Sc, P3Sd, P4Sb 
P1Sa, P2Sd, P3Sb, P4Sc 
P1Sa, P2Sd, P3Sc, P4Sb 
P1Sb, P2Sa, P3Sc, P4Sd 
P1Sb, P2Sa, P3Sd, P4Sc 
P1Sb, P2Sc, P3Sa, P4Sd 
P1Sb, P2Sc, P3Sd, P4Sa 
P1Sb, P2Sd, P3Sa, P4Sc 
P1Sb, P2Sd, P3Sc, P4Sa 
P1Sc, P2Sa, P3Sb, P4Sd 
P1Sc, P2Sa, P3Sd, P4Sb 
P1Sc, P2Sb, P3Sa, P4Sd 
P1Sc, P2Sb, P3Sd, P4Sa 
P1Sc, P2Sd, P3Sa, P4Sb 
P1Sc, P2Sd, P3Sb, P4Sa 
P1Sd, P2Sa, P3Sb, P4Sc 
P1Sd, P2Sa, P3Sc, P4Sb 
P1Sd, P2Sb, P3Sa, P4Sc 
P1Sd, P2Sb, P3Sc, P4Sa 
P1Sd, P2Sc, P3Sa, P4Sb 
P1Sd, P2Sc, P3Sb, P4Sa 
QA_Col
  • 1,095
  • 9
  • 23