Jump to content
Radiohead22

_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?

 

 

Share this post


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

 

Share this post


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.

Share this post


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.

Share this post


Link to post
Share on other sites

I tried:

 _Excel_RangeWritE($oWorkbook, Default, $Str, "A1")

OR

 _Excel_RangeWritE($oWorkbook, Default, $Str, "A1:A16")

OR

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

 

but always writes the same column on the same line

Share this post


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

Share this post


Link to post
Share on other sites

Like I said you can increase the size of the array respecting the same pattern X-N.  If you change the pattern like you did, you need to change the continueLoop condition.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Ahahahaha look what i was doing:

If StringInStr ($Str, "-",  1, -1, StringLeft ($aArray[$i], -1)) Then ContinueLoop

your example works perfectly thanks for your patience :)

Share this post


Link to post
Share on other sites

Do you have any idea how I can increase the number of combinations in a row?

es.
from:
Combi ( "", - 1,0,4)

to
Combi ( "", - 1,0,5)

in an array of 8.

The _ArrayCombinations function does this but does it with duplicates

Share this post


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...