Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save earthbound19/74fc6e2588a948f7cb608db266f7d6bb to your computer and use it in GitHub Desktop.
Save earthbound19/74fc6e2588a948f7cb608db266f7d6bb to your computer and use it in GitHub Desktop.

Revisions

  1. earthbound19 revised this gist Feb 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ public static void combination(ArrayList<Integer> e, int howManyToSelect, String
    combinationsStrs.add(tmpString);
    print(">"); for (int t:tmpIntArrayList ){ print(t + ";"); } print("\n");
    combinationsInts.add(tmpIntArrayList);
    } // 4. for each element, call combination
    } // 4. for each element, call this function itself (recursive)
    else if (e.size() > howManyToSelect) {
    for (int i = 0 ; i < e.size() ; i++) {
    // hacking note: because I get a *view* of a list, but not an ArrayList, via subList, convert it; re: https://stackoverflow.com/a/16644841 -- I create a temp ArrayList here by converting the return of subList:
  2. earthbound19 revised this gist Feb 28, 2021. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -60,8 +60,9 @@ public static void combination(ArrayList<Integer> e, int howManyToSelect, String
    void draw() {
    // Preparation for example function call:
    ArrayList<Integer> testArrayList = new ArrayList<Integer>();
    testArrayList.add(0); testArrayList.add(1); testArrayList.add(2);
    testArrayList.add(3); testArrayList.add(4); testArrayList.add(5);
    for (int i = 0; i < 6; i++) {
    testArrayList.add(i);
    }
    String accumulationSTR = "";
    ArrayList<Integer> accumulationArrayINTsList = new ArrayList<Integer>();
    // Function call
  3. earthbound19 revised this gist Feb 28, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ public static void combination(ArrayList<Integer> e, int howManyToSelect, String
    }
    // 3. add all elements in e to accumulated
    else if (e.size() == howManyToSelect) {
    ArrayList<Integer> tmpIntArrayList = new ArrayList<Integer>(combinations_too);
    ArrayList<Integer> tmpIntArrayList = new ArrayList<Integer>(combinations_too);
    for (int s:e) {
    accumulated += s;
    tmpIntArrayList.add(s);
  4. earthbound19 revised this gist Feb 28, 2021. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -19,9 +19,7 @@ public static void combination(ArrayList<Integer> e, int howManyToSelect, String
    if (e.size() < howManyToSelect)
    return;
    // 2. add each element in e to accumulated
    // THINGS AT THIS LEFTMOST OUTDENT IN THIS FUNCTION ARE EXPERIMENTAL
    if (howManyToSelect == 1) {
    int intPair;
    for (int s:e) {
    // String INFO:
    print("case2 accum:" + accumulated + ";s:" + s + " ");
  5. earthbound19 revised this gist Feb 28, 2021. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    // Processing/Java example of function that prints strings of all possible combinations of size howManyToSelect from an ArrayList<Integer>. Also, it constructs an ArrayList of ArrayLists of integers, with is a list of all possible combinations. Because I want this in Processing for my prototyping and generative art purposes. Because I am insane.
    // Processing/Java example of function that prints strings of all possible combinations of size howManyToSelect from an ArrayList<Integer>.
    // Also, it constructs an ArrayList of ArrayLists of integers, with is a list of all possible combinations. Because I want this
    // in Processing for my prototyping and generative art purposes. Because I am insane.
    // ADDENDUM: GitHub doesn't wrap the text in the viewbox despite my just setting a toggle to do so.
    // To read many comments you may need to view/copy the raw source.

    // Disclaimer: I could not explain how this works. I hacked a black box function I found until it did what I want.

  6. earthbound19 revised this gist Feb 28, 2021. No changes.
  7. earthbound19 revised this gist Feb 28, 2021. 1 changed file with 58 additions and 24 deletions.
    82 changes: 58 additions & 24 deletions processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -1,43 +1,77 @@
    // Processing/Java example of function that prints strings of all possible combinations of size howManyToSelect from an ArrayList<String>.
    // Processing/Java example of function that prints strings of all possible combinations of size howManyToSelect from an ArrayList<Integer>. Also, it constructs an ArrayList of ArrayLists of integers, with is a list of all possible combinations. Because I want this in Processing for my prototyping and generative art purposes. Because I am insane.

    // Recursive function (it calls itself) adapted from: https://hmkcode.com/calculate-find-all-possible-combinations-of-an-array-using-java/
    // Disclaimer: I could not explain how this works. I hacked a black box function I found until it did what I want.

    // This is a a recursive function (it calls itself). Adapted from: https://hmkcode.com/calculate-find-all-possible-combinations-of-an-array-using-java/
    // other implementations of this or other combinatronics algorithms: https://stackoverflow.com/a/29914908/1397555 -- https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/
    // verify debug output against this, answering "no" to "Is Order important?" and "Is Repitition allowed?" : https://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html
    public static void combination(ArrayList<String> e, int howManyToSelect, String accumulated) {
    // To test it, verify debug output against this, answering "no" to "Is Order important?" and "Is Repitition allowed?" : https://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html
    // Global ArrayList that the function modifies:
    static ArrayList<String> combinationsStrs = new ArrayList<String>();
    // Global ArrayList of ArrayLists that the function modifies:
    static ArrayList<ArrayList<Integer>> combinationsInts = new ArrayList<ArrayList<Integer>>();
    // For practical purposes, remove/adapt parameters to this function and then remove/adapt related code in the function (for example I have no use for `String accumulated`, and I might adapt the function to require an ArrayList<ArrayList<Integer>>. Also, note that the `String accumulated` should be an empty string when passed to the function, as also should ArrayList<ArrayList<Integer>> combinationsInts be empty.
    public static void combination(ArrayList<Integer> e, int howManyToSelect, String accumulated, ArrayList<Integer> combinations_too) {
    // 1. stop if/when there's no point running this function.
    if (e.size() < howManyToSelect) {
    if (e.size() < howManyToSelect)
    return;
    }
    // 2. add each element in e to accumulated
    // THINGS AT THIS LEFTMOST OUTDENT IN THIS FUNCTION ARE EXPERIMENTAL
    if (howManyToSelect == 1) {
    for(String s:e) {
    // By whatever magic of this algorithm I don't understand, this will print the next combination up to that last possible . . .
    print(accumulated+s + " ");
    int intPair;
    for (int s:e) {
    // String INFO:
    print("case2 accum:" + accumulated + ";s:" + s + " ");
    String tmpString = accumulated + s;
    combinationsStrs.add(tmpString);
    // ArrayList THINGS; equivalents of the above string operations but with an ArrayList:
    ArrayList<Integer> tmpIntArrayList = new ArrayList<Integer>(combinations_too);
    tmpIntArrayList.add(s);
    print(">"); for (int t:tmpIntArrayList ){ print(t + ";"); } print("\n");
    combinationsInts.add(tmpIntArrayList);
    }
    } // 3. add all elements in e to accumulated
    }
    // 3. add all elements in e to accumulated
    else if (e.size() == howManyToSelect) {
    for(String s:e) {
    accumulated+=s;
    ArrayList<Integer> tmpIntArrayList = new ArrayList<Integer>(combinations_too);
    for (int s:e) {
    accumulated += s;
    tmpIntArrayList.add(s);
    }
    print(accumulated + " "); // try making this print an appended newline to see how often/when this is called as the function is called recursively within itself.
    print("case3 accum:" + accumulated + " ");
    String tmpString = accumulated;
    combinationsStrs.add(tmpString);
    print(">"); for (int t:tmpIntArrayList ){ print(t + ";"); } print("\n");
    combinationsInts.add(tmpIntArrayList);
    } // 4. for each element, call combination
    else if (e.size() > howManyToSelect) {
    for(int i = 0 ; i < e.size() ; i++) {
    for (int i = 0 ; i < e.size() ; i++) {
    // hacking note: because I get a *view* of a list, but not an ArrayList, via subList, convert it; re: https://stackoverflow.com/a/16644841 -- I create a temp ArrayList here by converting the return of subList:
    ArrayList<String> tmpArrayList = new ArrayList<String>( e.subList( i+1, e.size() ) );
    combination(tmpArrayList, howManyToSelect - 1, accumulated+e.get(i));
    ArrayList<Integer> tmpArrayList = new ArrayList<Integer>( e.subList( i+1, e.size() ) );
    ArrayList<Integer> tmpArrayListToo = new ArrayList<Integer>(combinations_too);
    tmpArrayListToo.add(e.get(i));
    combination(tmpArrayList, howManyToSelect - 1, accumulated+e.get(i), tmpArrayListToo);
    }
    }
    }

    // Example code within the draw() function call of Processing because it won't work in Processing otherwise:
    void draw() {
    // Example function call and output; prep:
    ArrayList<String> testArrayList = new ArrayList<String>();
    testArrayList.add("A"); testArrayList.add("B"); testArrayList.add("C");
    testArrayList.add("D"); testArrayList.add("E"); testArrayList.add("F");
    String combinationSTR = "";
    // Example function call; the second parameter can be any static int less than or equal to testArrayList.size():
    combination(testArrayList, 3, combinationSTR);
    exit(); // exit after only one loop of draw()
    // Preparation for example function call:
    ArrayList<Integer> testArrayList = new ArrayList<Integer>();
    testArrayList.add(0); testArrayList.add(1); testArrayList.add(2);
    testArrayList.add(3); testArrayList.add(4); testArrayList.add(5);
    String accumulationSTR = "";
    ArrayList<Integer> accumulationArrayINTsList = new ArrayList<Integer>();
    // Function call
    combination(testArrayList, 3, accumulationSTR, accumulationArrayINTsList);
    // Test print of variables modified by function call:
    print("\nTest result list print from ArrayList<String> combinationsStrs:\n");
    for (String s:combinationsStrs) { print(s + " "); }
    print("\nTest result list print from ArrayList<Integer> combinationsInts:\n");
    for (ArrayList<Integer> t:combinationsInts) {
    for (int u:t) { print(u + ","); } print(" ");
    }
    print("\n");
    // exit after only one loop of draw() :
    exit();
    }
  8. earthbound19 revised this gist Feb 26, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    // Processing/Java example of function that prints strings of all possible combinations of size howManyToSelect from an ArrayList<String>.

    // Recursive function (it calls itself) Badapted from: https://hmkcode.com/calculate-find-all-possible-combinations-of-an-array-using-java/
    // Recursive function (it calls itself) adapted from: https://hmkcode.com/calculate-find-all-possible-combinations-of-an-array-using-java/
    // other implementations of this or other combinatronics algorithms: https://stackoverflow.com/a/29914908/1397555 -- https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/
    // verify debug output against this, answering "no" to "Is Order important?" and "Is Repitition allowed?" : https://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html
    public static void combination(ArrayList<String> e, int howManyToSelect, String accumulated) {
  9. earthbound19 created this gist Feb 26, 2021.
    43 changes: 43 additions & 0 deletions processing_recursive_combination_function.pde
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    // Processing/Java example of function that prints strings of all possible combinations of size howManyToSelect from an ArrayList<String>.

    // Recursive function (it calls itself) Badapted from: https://hmkcode.com/calculate-find-all-possible-combinations-of-an-array-using-java/
    // other implementations of this or other combinatronics algorithms: https://stackoverflow.com/a/29914908/1397555 -- https://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/
    // verify debug output against this, answering "no" to "Is Order important?" and "Is Repitition allowed?" : https://www.mathsisfun.com/combinatorics/combinations-permutations-calculator.html
    public static void combination(ArrayList<String> e, int howManyToSelect, String accumulated) {
    // 1. stop if/when there's no point running this function.
    if (e.size() < howManyToSelect) {
    return;
    }
    // 2. add each element in e to accumulated
    if (howManyToSelect == 1) {
    for(String s:e) {
    // By whatever magic of this algorithm I don't understand, this will print the next combination up to that last possible . . .
    print(accumulated+s + " ");
    }
    } // 3. add all elements in e to accumulated
    else if (e.size() == howManyToSelect) {
    for(String s:e) {
    accumulated+=s;
    }
    print(accumulated + " "); // try making this print an appended newline to see how often/when this is called as the function is called recursively within itself.
    } // 4. for each element, call combination
    else if (e.size() > howManyToSelect) {
    for(int i = 0 ; i < e.size() ; i++) {
    // hacking note: because I get a *view* of a list, but not an ArrayList, via subList, convert it; re: https://stackoverflow.com/a/16644841 -- I create a temp ArrayList here by converting the return of subList:
    ArrayList<String> tmpArrayList = new ArrayList<String>( e.subList( i+1, e.size() ) );
    combination(tmpArrayList, howManyToSelect - 1, accumulated+e.get(i));
    }
    }
    }

    // Example code within the draw() function call of Processing because it won't work in Processing otherwise:
    void draw() {
    // Example function call and output; prep:
    ArrayList<String> testArrayList = new ArrayList<String>();
    testArrayList.add("A"); testArrayList.add("B"); testArrayList.add("C");
    testArrayList.add("D"); testArrayList.add("E"); testArrayList.add("F");
    String combinationSTR = "";
    // Example function call; the second parameter can be any static int less than or equal to testArrayList.size():
    combination(testArrayList, 3, combinationSTR);
    exit(); // exit after only one loop of draw()
    }