Jump to content

_ArrayCombinations without repetitions


Recommended Posts

#include<array.au3>

Local $aArray[8] = ['A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2', 'D-1', 'D-2']

Local $aNewArray = _ArrayCombinations($aArray, 4, ' / ')

_ArrayDisplay($aNewArray)

 

Greetings to the whole forum,

I'm trying to create combinations without repetition.

I would like to avoid repeating the same letter even if with a different number in the same line

EXAMPLE:

OK:
A-1 / B-1 / C-1 / D-1 /

NO:
A-1 / A-2 / C-1 / D-1 /

----------------------------------------
OK:
A-1 / B-2 / C-1 / D-2 /

NO:
A-1 / C-1 / D-1 / D-2 /

----------------------------------------

Could some good soul help me out?

 

 

Link to post
Share on other sites

Try this :

Global $aArray[8] = ['A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2', 'D-1', 'D-2']

Combi("",-1,4)

Func Combi($Str, $Pos, $MaxLen)
  if StringLen($Str) = $MaxLen*6 Then
    ConsoleWrite("Combination = " & $Str & @CRLF)
    Return
  EndIf
  For $i = $Pos+1 to UBound($aArray)-1
    If StringInStr ($Str, StringLeft ($aArray[$i],2)) Then ContinueLoop
    Combi($Str & $aArray[$i] & " / ",$i,$MaxLen)
  Next
EndFunc

 

Link to post
Share on other sites

Thanks Nine always kind,

it works fine but how do I change _consolewrite () to _Excel_RangeWrite?

With _ArrayDisplay don't run.

I also need to study it a bit to understand how to adapt it to different types of arrays or arrays greater than 8.

Anyway thanks again for your help.

Link to post
Share on other sites
#include <Array.au3>

Global $aArray[8] = ['A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2', 'D-1', 'D-2']
Global $aFinal[0]

Combi("",-1,4)

Func Combi($Str, $Pos, $MaxLen)
  if StringLen($Str) = $MaxLen*6 Then
    _ArrayAdd ($aFinal,$Str)
    Return
  EndIf
  For $i = $Pos+1 to UBound($aArray)-1
    If StringInStr ($Str, StringLeft ($aArray[$i],2)) Then ContinueLoop
    Combi($Str & $aArray[$i] & " / ",$i,$MaxLen)
  Next
EndFunc

_ArrayDisplay ($aFinal)

You can increase the size of the $aArray without any problem.  If you change the content of the array then the if...continueLoop must be modified accordingly.

Link to post
Share on other sites
#include<array.au3>
#include <Excel.au3>

$file1 = @ScriptDir & '\Combinazioni.xlsx'

Local $oExcel = _Excel_Open()

Local $oWorkbook = _Excel_BookOpen($oExcel, $file1)

Global $aArray[9] = ['A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2', 'D-1', 'D-2', 'CIAO']
Global $aFinal[0]

Combi("",-1,4)

Func Combi($Str, $Pos, $MaxLen)
  if StringLen($Str) = $MaxLen*6 Then
    _ArrayAdd ($aFinal,$Str)
    Return
  EndIf
  For $i = $Pos+1 to UBound($aArray)-1
    If StringInStr ($Str, StringLeft ($aArray[$i],2)) Then ContinueLoop
    Combi($Str & $aArray[$i] & " / ",$i,$MaxLen)
  Next
EndFunc

_Excel_RangeWritE($oWorkbook, Default, $aFinal & @CRLF, "A1")

Now on excel it works perfectly but I don't understand how to increase the array, maybe I'm too stupid :D

Link to post
Share on other sites
Link to post
Share on other sites
Global $aArray[10] = ['A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2', 'D-1', 'D-2', 'CIAO-1', 'CIAO-2']
Global $aFinal[0]

Combi("",-1,4)

Func Combi($Str, $Pos, $MaxLen)
  if StringLen($Str) = $MaxLen*6 Then
    _ArrayAdd ($aFinal,$Str)
    Return
  EndIf
  For $i = $Pos+1 to UBound($aArray)-1
;~  If StringInStr ($Str, StringLeft ($aArray[$i],3)) Then ContinueLoop
;~  If StringInStr ($Str, StringLeft ($aArray[$i],10)) Then ContinueLoop
    If StringInStr ($Str, StringLeft ($aArray[$i],4)) Then ContinueLoop
    Combi($Str & $aArray[$i] & " / ",$i,$MaxLen)
  Next
EndFunc

;~ _Excel_RangeWritE($oWorkbook, Default, $aFinal & @CRLF, "A1")

_ArrayDisplay ($aFinal)

 

Yes and thank you for your patience.
I can't understand how, I'm doing a thousand tests

Link to post
Share on other sites
#include <Array.au3>

Global $aArray[10] = ['A-1', 'A-2', 'B-1', 'B-2', 'C-1', 'C-2', 'D-1', 'D-2', 'CIAO-1', 'CIAO-2']
Global $aFinal[0]

Combi("",-1,0,4)

Func Combi($Str, $Pos, $Count, $MaxLen)
  if $Count = $MaxLen Then
    _ArrayAdd ($aFinal,$Str)
    Return
  EndIf
  For $i = $Pos+1 to UBound($aArray)-1
    If StringInStr($Str,StringLeft ($aArray[$i],StringInStr($aArray[$i],"-"))) Then ContinueLoop
    Combi($Str & $aArray[$i] & " / ",$i,$Count+1,$MaxLen)
  Next
EndFunc

_ArrayDisplay ($aFinal)

Ok, made a couple of changes to make it work with now a count instead of StrLen

Edited by Nine
remove -1 in condition
Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...