# Iterate once and only once through all the possible configurations of an array

**URL:** <https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366>\
**Category:** Coding Questions\
**Created:** [July 3, 2020, 7:37pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366 "2020-07-03T19:37:07Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![whistlinpete](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@whistlinpete](https://discourse.processing.org/u/whistlinpete)\
**Post date:** [July 3, 2020, 7:37pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/1 "2020-07-03T19:37:07Z")

</div>

I am trying to create a program that will iterate through all the possible combinations for a given array once and only once, and use those combinations to color a grid of dots. I’ve simplified the problem to a one-row, two-column grid with only two colors.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/619b33e055b19c4028f453ccabe3da85dadf485e.png)

I think I could be able to color the dots if I can programmatically create these four different configurations of an array…

> int dots = { colors[1], colors[0] };  
> int dots = { colors[1], colors[1] };  
> int dots = { colors[0], colors[0] };  
> int dots = { colors[0], colors[1] };

…assuming I have another array that is:

> int colors = {#FFFFFF, #000000};

I’m relatively familiar with creating arrays, I know they can be modified, and I’m thinking I should use an `if, else` statement to check if a particular configuration of the array has already been drawn. But I have no idea how to do that check. Also possible this is a stupid way of trying to achieve my ultimate goal which is is 3x3 grid (with 512 possible combinations) similar to [this](https://crislemon.tumblr.com/post/42535360988/obsessedbythegrid-512-different-ways-to-color-a). Anyone have any ideas? Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [July 3, 2020, 9:26pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/2 "2020-07-03T21:26:50Z")

</div>

Hi,

Your problem is a well known issue in programming :

- [https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/](https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/)

---

<div class="post-metadata">

**Author:** ![whistlinpete](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@whistlinpete](https://discourse.processing.org/u/whistlinpete)\
**Post date:** [July 3, 2020, 11:37pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/3 "2020-07-03T23:37:30Z")

</div>

Thanks @josephh, I appreciate the pointer. I read that article a few times but couldn’t quite wrap my head around what to do next. I’m a bit of a noob and probably in over my head. I’ve been able to get every dot to color with either white or black, but I can’t figure out how to (1) make sure I get all the possible combinations, and (2) make sure I get no duplicates. I gather I create a data[] array in which to produce all the possible combinations but I can’t figure out how to do that. If anyone has further advice I definitely welcome it. Here’s what I’ve got so far (with a 2 row, 2 column grid instead of one row):

```auto
 import processing.pdf.*;
 boolean savePDF = true;
 
 int colorset;
 color [] colorsetarray = {#FFFFFF, #000000}; 
 
 void setup () {
   size(400, 400);
   noLoop(); //prevents video playback
 }
 
 void draw () {
   beginRecord(PDF, "Arabesque"+year()+"-"+"0"+month()+"-"+day()+"_"+hour()+minute()+"0"+second()+".pdf" );
   
   for (int y=20; y<height; y += 50) {
     for (int x=20; x<width; x += 50) {
    drawGrid(x,y);
     }
   }
 
   // saves the image when you play the sketch with the date and seconds in the sketch folder
   endRecord();
 }
 
 void drawGrid (float x, float y) {
   noStroke();
   fill(colorsetarray[int(random(2))]);
   circle(x,y,6);
   fill(colorsetarray[int(random(2))]);
   circle(x+9,y,6);
   fill(colorsetarray[int(random(2))]);
   circle(x,y+9,6);
   fill(colorsetarray[int(random(2))]);
   circle(x+9,y+9,6);
   }

```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/48dac1077442b02aff8b0eafc17b8a00dd8dddb4.png)

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 4, 2020, 10:49am UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/4 "2020-07-04T10:49:41Z")

</div>

start of with 2 empty ArrayLists (for flexibility). In the first one you will assign a new random variable and the add it to your first arraylist after checking if the ArrayList contains the new value or not.

Now if you know how many combinations to expect you know the maximum size the first ArrayList needs to be, so we need an if statement to increment a counter that tells us when we are trying to add a value that is already in the array, but we need to make sure we only count it once so we get an accurate idea of the number of combinations we have explored. Thats why I suggest using a second Arraylist. This one shall also only be populated after a second test to see if the second array contains your new value, then finally add a condition to your loop that compares your counter to how big the first or second ArrayList is, if its the size of the predetermined max for the combinations you’ve explored all combinations and the job is done.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 4, 2020, 11:03am UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/5 "2020-07-04T11:03:36Z")

</div>

to work out total combinations you need to understand what operations you are going to do. Say you are picking from a set value which can be reused then you would calculate x^y, x will be the number of choices you can make and y being the number of times you are allowed to make a choice. The number of choices remains the same as all probabilities are still available each time you choose. ( Think putting a card back into the deck, throwing a dice or flipping a coin).  
If the choices cannot be reused then you would use the factorial to find out your max combinations, ie;

pick a card from a set of cards and then do not put your choice back in the deck.

52 total cards so 52! max combinations if you decide that you have t make 52 choices.

If you are making less than 52 choices then you have to amend the factorial calculation. We know that to calculate factorial we have to do  
`totalChoices * (maximumChoices - currentChoice) currentChoice`  
being the number of times we have already chosen. And finally we multiply our last answer with any new choices, giving us.

```auto
totalChoices * maxChoicesAllowed((maximumChoices - currentChoice) currentChoice)

```

then you can combine your knowledge of everything discussed to calculate the maximum allowed combinations for you current problem.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 4, 2020, 11:47am UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/6 "2020-07-04T11:47:12Z")

</div>

I made a working Sketch now.

I just put 9 nested for loops together to generate the bit pattern:

```auto

 for (int i0 = 0; i0 < 2; i0++) { 
    for (int i1 = 0; i1 < 2; i1++) { 
      for (int i2 = 0; i2 < 2; i2++) { 
         ....
            ......

```

* * *

Of course I wrote another Sketch to write these 9 lines with for-loops for me, so I don’t have to do it:

```auto

void setup() {

  size(800, 600);

  int numberOfDesiredForLoops = 9; 

  for (int i = 0; i < numberOfDesiredForLoops; i++) {
    println ( " for (int i"+i+" = 0; i"+i+" < 2; i"+i+"++) { " );
  }

  println ("");
  for (int i = 0; i < numberOfDesiredForLoops; i++) {
    print ( "i"+i+",");
  }
  println ("");
  println ("");

  for (int i = 0; i < numberOfDesiredForLoops; i++) {
    println ( "}");
  }
  println ("");

  exit();
} // func 
//

```

Chrisir

---

<div class="post-metadata">

**Author:** ![whistlinpete](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@whistlinpete](https://discourse.processing.org/u/whistlinpete)\
**Post date:** [July 4, 2020, 2:53pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/7 "2020-07-04T14:53:38Z")

</div>

@paulgoux and @Chrisir this is great stuff! I’m chewing through it. Hilarious to make a program that will make the for loops. 😆

I’ve got the first part of @paulgoux’s suggestion working:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5efee200aeca92b9cb85ee7d9ab5803f26094c07.png)

I’m not going to be the most efficient, but I think I’m on my way. I may report back if I have more questions, but thanks a ton for the really helpful guidance!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 4, 2020, 3:04pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/8 "2020-07-04T15:04:20Z")

</div>

> [@whistlinpete](#):
>
> Hilarious to make a program that will make the for loops.

I like that! 😉

> [@Chrisir](#):
>
> I made a working Sketch now.

I my Sketch (that I didn’t post, didn’t want to spoil the fun for you) it just produces the 512 permutations without random. That’s what the 9 nested for loops do. There is also a check for duplicates.

Chrisir

---

<div class="post-metadata">

**Author:** ![whistlinpete](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@whistlinpete](https://discourse.processing.org/u/whistlinpete)\
**Post date:** [July 4, 2020, 3:09pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/9 "2020-07-04T15:09:30Z")

</div>

> In my Sketch (that I didn’t post, didn’t want to spoil the fun for you) it just produces the 512 permutations without random.

👍 And thanks for explaining what the for loops do. I’ve got some ideas for how to proceed, and this is a fun challenge, so I’ll keep following the path I’m on. If I lose my path though I might cry for help. 😱

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [July 4, 2020, 3:53pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/10 "2020-07-04T15:53:17Z")

</div>

Here is an arraylist constructor.

`ArrayList<someClass> varName = new ArrayList<someClass>();`

To add to the arraylist;

`varName.add(someInstanceOfClass);`

Read value

`VarName.get(int);`

Set value

`varName.set(int, someInstanceOfClass);`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 4, 2020, 6:47pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/11 "2020-07-04T18:47:35Z")

</div>

Hello,

Please format your code for readability and so we can copy it easily:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

Try to cut and paste your code into Processing and you will understand.

When I hover over this code I can use the copy icon that pops up in upper right.

Example:

```auto
void setup() 
	{
  size(700, 150);
  background(0);
  fill(0, 255, 0);
  textAlign(CENTER, CENTER);
  textSize(48);
  text("Please format your code. :)", width/2, height/2-10);
	}

```

`:)`

---

<div class="post-metadata">

**Author:** ![whistlinpete](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@whistlinpete](https://discourse.processing.org/u/whistlinpete)\
**Post date:** [July 4, 2020, 6:57pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/12 "2020-07-04T18:57:43Z")

</div>

Thanks for the tip. I thought I had formatted it right, but apparently not, so I edited it to make it better (but it includes arrays instead of ArrayLists). I’m watching Shiffman videos to figure out ArrayLists, classes, etc.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 4, 2020, 7:07pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/13 "2020-07-04T19:07:21Z")

</div>

> [@whistlinpete](#):
>
> I gather I create a data array in which to produce all the possible combinations but I can’t figure out how to do that.

This may not answer your questions but will give you something to think about. 🙂

You already have all the possible combinations… you just have to think in binary (and hexadecimal) and extract the bits.

Then wield these bits as you desire to unleash your creative vision.

```auto
  for(int i=0; i<64; i++)
    {
    println(binary(i));
    }

```

Partial code to get you started:

```auto
void setup() 
  {
  size(100, 160);
  colorMode(RGB, 1); // Just using black 0 and white 1
  
  for(int i=0; i<16; i++)
    {
    println(binary(i));
    }
  
  for(int i=0; i<16; i++)
    {
    int a, b;
    
    a = i & 0x01;
    b = (a > 0) ? 1 : 0;
    print(b);
    fill(b);
    circle(20, i*10+5, 10);
    
    a = i & 0x02;
    b = (a > 0) ? 1 : 0;
    print(b);
    fill(b);
    circle(30, i*10+5, 10);
    
    a = i & 0x04;
    b = (a > 0) ? 1 : 0;
    print(b);
    fill(b);
    circle(40, i*10+5, 10);
    
    a = i & 0x08;
    b = (a > 0) ? 1 : 0;
    println(b);
    fill(b);
    circle(50, i*10+5, 10);
    }       
  }

```

References:

> **[?: (conditional) / Reference](https://processing.org/reference/conditional.html)**
>
> A shortcut for writing an if and else structure. The conditional operator, ?: is sometimes called the ternary operator, an operator that takes three arguments. If the test …

> **[& (bitwise AND) / Reference](https://processing.org/reference/bitwiseAND.html)**
>
> Compares each corresponding bit in the binary representation of the values. For each comparison two 1's yield 1, 1 and 0 yield 0, and two 0's yield 0. This is easy to see when we look at the binary re…

> **[colorMode() / Reference](https://processing.org/reference/colorMode_.html)**
>
> Changes the way Processing interprets color data. By default, the parameters for fill(), stroke(), background(), and color() are defined by values between 0 and 255 using t…

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b41372ddd64002575f88283bc973240d458a630e.png)

`:)`

---

<div class="post-metadata">

**Author:** ![whistlinpete](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@whistlinpete](https://discourse.processing.org/u/whistlinpete)\
**Post date:** [July 4, 2020, 9:59pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/14 "2020-07-04T21:59:48Z")

</div>

@glv this is fantastic; THANK YOU! 🙌

It’s gonna take me a bit to understand exactly what’s going on, but I think I’m getting the gist. Thanks also for pointing me to the reference. I love the way Processing lays out its documentation; so easy to read and understand (at least eventually if not at first).

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 4, 2020, 10:03pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/15 "2020-07-04T22:03:09Z")

</div>

> [@whistlinpete](#):
>
> Thanks also for pointing me to the reference.

More for you…

One of the best tools in a programmers tool chest is knowing the resources available to you and learning to navigate and use them.

This is a very short list:

> **Resources \< Click here to expand !**
>
> I encourage you to review the resources available here:
> 
> - The Processing _website_ has references, examples, tutorials, etc.  
> [https://processing.org/](https://processing.org/)
> - The Processing PDE (IDE) has lots to offer!  
> An exploration of the tabs will reveal what is available; libraries, tools and local examples there.
> - The Coding Train  
> [https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
> [https://thecodingtrain.com/](https://thecodingtrain.com/)
> - Happy Coding  
> [Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)
> - The source code can give insight if you are adventurous:  
> [GitHub - processing/processing: Source code for the Processing Core and Development Environment (PDE)](https://github.com/processing/processing)  
> Many of the Processing functions are here:  
> [processing/core/src/processing/core/PApplet.java at master · processing/processing · GitHub](https://github.com/processing/processing/blob/master/core/src/processing/core/PApplet.java)
> - The Processing Foundation  
> [https://processingfoundation.org/](https://processingfoundation.org/)  
> Check out the tabs.  
> In the education tab, there is a reference to the Coding Train.
> - And the Internet. `:)`

`:)`

---

<div class="post-metadata">

**Author:** ![whistlinpete](https://avatars.discourse-cdn.com/v4/letter/w/0ea827/32.png) [@whistlinpete](https://discourse.processing.org/u/whistlinpete)\
**Post date:** [July 4, 2020, 10:04pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/16 "2020-07-04T22:04:58Z")

</div>

👍 I _love_ Coding Train. Have watched many of those videos (at least the Processing-specific ones) several times.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 6, 2020, 5:28am UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/17 "2020-07-06T05:28:27Z")

</div>

> all the possible configurations of an array

If by this you mean all the permutations, then check out [Heap’s algorithm](https://en.wikipedia.org/wiki/Heap%27s_algorithm) – and a recent implementation posted to the forum, here:

> [@Storing all index permutations with Heap's algorithm](https://discourse.processing.org/t/storing-all-index-permutations-with-heaps-algorithm/22326):
>
> Hi, I am new on Processing and I am struggling with Java. I am trying to use [Heap’s algorithm](https://en.wikipedia.org/wiki/Heap%27s_algorithm#Details_of_the_algorithm) to get the list of all the permutations of numbers. I want to store that list so I wan use it as a matrix for animation. I have tried many different ways and types but I can’t fill it correctly. As I am stuck, I would appreciate some help. Here what I have: // https://en.wikipedia.org/wiki/Heap%27s\_algorithm#Details\_of\_the\_algorithm void heapPermutations(int k, IntList l, ArrayList\<IntList\> a) { …

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [July 6, 2020, 11:35am UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/18 "2020-07-06T11:35:28Z")

</div>

> [@jeremydouglass](#):
>
> If by this you mean all the permutations

Hello,

I wonder if this is the same.

- permutations would be 123, 132, 231, 213…

- configurations in the sense above: 000, 001, 101, 111, …

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [July 6, 2020, 4:10pm UTC](https://discourse.processing.org/t/iterate-once-and-only-once-through-all-the-possible-configurations-of-an-array/22366/19 "2020-07-06T16:10:53Z")

</div>

You may be right. Because the question said “I’ve simplified the problem” it wasn’t clear to me what the ultimate goal was. There are two values in the simplified case, but it may scale up to permutation with repetition…?

[https://rosettacode.org/wiki/Permutations\_with\_repetitions#Java](https://rosettacode.org/wiki/Permutations_with_repetitions#Java)

Ah, no, I see, I missed the goal link. That is combinations, not permutations. In fact, it is counting – base 2, but if there were 3 colors, it would be base 3…
