Jump to content

All Possible Combinations Solution


Nagasa
 Share

Recommended Posts

I don't know if this will help anyone but i felt like sharing its a pretty simple script but i feel it could be helpful to some.

#include <Misc.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <StaticConstants.au3>
$gGUI = GUICreate("All Posibilities", 100, 65);GUI Stuff
$l1Label = GUICtrlCreateLabel("3",10,3,25,15)
$l2Label = GUICtrlCreateLabel("0/0",0,50,100,15,$SS_CENTER)
$b1Button = GUICtrlCreateButton("Start", 0,30,100,20)
$p1Prog = GUICtrlCreateProgress(0,20,100,10)
$b2Button = GUICtrlCreateButton("Add Arrays",25,0,75,20)
GUISetState(@SW_SHOW,$gGUI)
Global $Arrayoarray[0][0];Setting Global Array for the combinations.
Func Startproc()
   GUICtrlSetData($l1Label,3);Time Delay
   $prog = 0
   Sleep(1000)
   GUICtrlSetData($l1Label,2)
   Sleep(1000)
   GUICtrlSetData($l1Label,1)
   Sleep(1000)
   GUICtrlSetData($l1Label,0)
   Local $y[0];Define the Counter Array
   Local $hld;Place Holder
   For $i = 0 to Ubound($Arrayoarray,2)-1;Set Default Vaules of Counter Array to 0
    _ArrayAdd($y,0)
   Next
   Local $Count = 1 ;Set The total Count of the possiblities
   For $i = 0 to Ubound($Arrayoarray,2)-1
      $Count = $Count* Ubound($Arrayoarray,1)
   Next
   For $i =1 to $Count ; main loop for combinations
      $hld = ""; reseting holder variable to ""

      For $k = 0 to Ubound($y)-1 ; Check if the combination has blank characters(happens with different sized columns)
      If $Arrayoarray[$y[$k]][$k] = "" Then $hld = $hld&"~";Adds a ~ if is blank
      $hld = $hld&$Arrayoarray[$y[$k]][$k];Combines the contents of the main Array into the holder variable
      Next
      If StringInStr($hld,"~")=0 Then; Checks for a ~ in the holder variable
         Send($hld) ; If no ~ in holder variable types the Holder Variable and ENTER
         Send("{ENTER}")
      EndIf
      If _IsPressed("1B") Then ExitLoop ; If ESC pressed exits loop
      $y[UBound($y)-1] = $y[UBound($y)-1]+1; Adds 1 to the rightmost index of the counter array
      For $k = UBound($y)-1 to 0 Step -1 ; Checks to see if counter array indices are above their max and if they are reset them to 0 while increasing the next index up 1
         If $y[$k]>UBound($Arrayoarray,1)-1 Then
            $y[$k] = 0
            If $k>0 Then $y[$k-1]=$y[$k-1]+1;Error Handeler for last operation
         EndIf
      Next

      GUICtrlSetData($p1Prog, ($i/$Count)*100);Progressbar updating
      GUICtrlSetData($l2Label,$i&"/"&$Count);Count label updating
   Next
EndFunc
Func Arrayadder()
   Dim $Arrayoarray[0][0];Clear main array
   local $hld3 = [];holder 1,2, and 3
   local $hld2
   local $hld = InputBox("Number of Arrays","Number of Arrays");Asks how many arrays youd like to use
   For $i = 0 to $hld-1
      $hld2 = InputBox("Array "&$i,"Seperate with comas");Asks for the arrays seperated by ,
      $hld3 = StringSplit($hld2,",",$STR_NOCOUNT);Splits string into array
      If Ubound($hld3) > Ubound($Arrayoarray,1) Then Redim $Arrayoarray[UBound($hld3)][UBound($Arrayoarray,2)];Resets the size of the main array to fit the largest column you enter
      _ArrayColInsert($Arrayoarray,$i);adds a new column for each array
      For $g = 0 to Ubound($hld3)-1
         $Arrayoarray[$g][$i]=$hld3[$g];sets the values of the main array
      Next
   Next
EndFunc
While 1;Event Handling for GUI
   $iMsg = GUIGetMsg()
   Switch $iMsg
   case $b1Button
      Startproc()
   case $GUI_EVENT_CLOSE
      Exit
   case $b2Button
      Arrayadder()
   EndSwitch
WEnd

 

Array Combinations.au3

~ Just Your Friendly Neighborhood Nerd With Too Much Free Time.~

Link to comment
Share on other sites

Please expose clearly the purpose of this code.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

1 hour ago, jchd said:

Please expose clearly the purpose of this code.

You input the number of arrays Exp(3) Then add the arrays such as [1,2,3,4][A,B,C][1,2,3,4,5] and it will return all possible combination of the elements of each array(1,A,1)(1,A,2)...(4,C,5)

Edited by Nagasa

~ Just Your Friendly Neighborhood Nerd With Too Much Free Time.~

Link to comment
Share on other sites

_ArrayCombinations() only deals with a single 1D array.

@Nagasa this piece of code would be much more useable if it was reduced to a single function taking parameters. All the input/output and GUI stuff doesn't belong to a general purpose UDF.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

8 hours ago, jchd said:

_ArrayCombinations() only deals with a single 1D array.

@Nagasa this piece of code would be much more useable if it was reduced to a single function taking parameters. All the input/output and GUI stuff doesn't belong to a general purpose UDF.

#include <Array.au3>
#include <Misc.au3>

Func _ArrayCombo($Aoa)
   Local $y[0];Define the Counter Array
   Local $hld;Place Holder
   Local $Ra[0];Return Array
   _ArrayDisplay($Aoa)
   For $i = 0 to Ubound($Aoa,2)-1;Set Default Vaules of Counter Array to 0
    _ArrayAdd($y,0)
   Next
   Local $Count = 1 ;Set The total Count of the possiblities
   For $i = 0 to Ubound($Aoa,2)-1
      $Count = $Count* Ubound($Aoa,1)
   Next
   For $i =1 to $Count ; main loop for combinations
      $hld = ""; reseting holder variable to ""

      For $k = 0 to Ubound($y)-1 ; Check if the combination has blank characters(happens with different sized columns)
      If $Aoa[$y[$k]][$k] = "" Then $hld = $hld&"~";Adds a ~ if is blank
      $hld = $hld&$Aoa[$y[$k]][$k];Combines the contents of the main Array into the holder variable
      Next
      If StringInStr($hld,"~")=0 Then; Checks for a ~ in the holder variable
         _ArrayAdd($Ra,$hld);Add Result To Return Array
      EndIf
      If _IsPressed("1B") Then ExitLoop ; If ESC pressed exits loop
      $y[UBound($y)-1] = $y[UBound($y)-1]+1; Adds 1 to the rightmost index of the counter array
      For $k = UBound($y)-1 to 0 Step -1 ; Checks to see if counter array indices are above their max and if they are reset them to 0 while increasing the next index up 1
         If $y[$k]>UBound($Aoa,1)-1 Then
            $y[$k] = 0
            If $k>0 Then $y[$k-1]=$y[$k-1]+1;Error Handeler for last operation
         EndIf
      Next
   Next
   Return $Ra
EndFunc
Local $Test1 = [[1,"A"],[2,"B"],[3,"C"],[4],[5]]
Local $Test = _ArrayCombo($Test1)
_ArrayDisplay($Test)

 

This Will Output 

1A ,1B ,1C ,2A ,2B ,2C ,3A ,3B ,3C ,4A ,4B ,4C ,5A ,5B ,5C

~ Just Your Friendly Neighborhood Nerd With Too Much Free Time.~

Link to comment
Share on other sites

  • 2 years later...
  • Developers

Why are you using the vertical bar multiple times in your input?
The shows output is not triggered by the shown $Test1 input, so very confusing! I assume that was: Local $Test1 = [[1,"|"],[2,"a"],[3,"b"],[4],[5]]
The issues is that ArrayAdd() uses "|" as default for the 4th parameter, so change that in the UDF to another character when you need to use the "|" in your input or like shown below forcing single item ! ($ARRAYFILL_FORCE_SINGLEITEM)

e.g.:

#include <Array.au3>
#include <Misc.au3>

Func _ArrayCombo($Aoa)
    Local $y[0] ;Define the Counter Array
    Local $hld ;Place Holder
    Local $Ra[0] ;Return Array
    _ArrayDisplay($Aoa)
    For $i = 0 To UBound($Aoa, 2) - 1 ;Set Default Vaules of Counter Array to 0
        _ArrayAdd($y, 0, 0, "", "", $ARRAYFILL_FORCE_SINGLEITEM)
    Next
    Local $Count = 1 ;Set The total Count of the possiblities
    For $i = 0 To UBound($Aoa, 2) - 1
        $Count = $Count * UBound($Aoa, 1)
    Next
    For $i = 1 To $Count ; main loop for combinations
        $hld = "" ; reseting holder variable to ""

        For $k = 0 To UBound($y) - 1 ; Check if the combination has blank characters(happens with different sized columns)
            If $Aoa[$y[$k]][$k] = "" Then $hld = $hld & "~" ;Adds a ~ if is blank
            $hld = $hld & $Aoa[$y[$k]][$k] ;Combines the contents of the main Array into the holder variable
        Next
        If StringInStr($hld, "~") = 0 Then ; Checks for a ~ in the holder variable
            _ArrayAdd($Ra, $hld, 0, "", "", $ARRAYFILL_FORCE_SINGLEITEM) ;Add Result To Return Array
        EndIf
        If _IsPressed("1B") Then ExitLoop ; If ESC pressed exits loop
        $y[UBound($y) - 1] = $y[UBound($y) - 1] + 1 ; Adds 1 to the rightmost index of the counter array
        For $k = UBound($y) - 1 To 0 Step -1 ; Checks to see if counter array indices are above their max and if they are reset them to 0 while increasing the next index up 1
            If $y[$k] > UBound($Aoa, 1) - 1 Then
                $y[$k] = 0
                If $k > 0 Then $y[$k - 1] = $y[$k - 1] + 1 ;Error Handeler for last operation
            EndIf
        Next
    Next
    Return $Ra
EndFunc   ;==>_ArrayCombo
;~ Local $Test1 = [[1,"A"],[2,"B"],[3,"C"],[4],[5]]
Local $Test1 = [[1, "|"], [2, "a"], [3, "b"], [4], [5]]
Local $Test = _ArrayCombo($Test1)
_ArrayDisplay($Test)

Jos 

Edited by Jos
Tidied source

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...