Jump to content

Array help


Recommended Posts

  • Moderators

How could I make an array to go thru all possible combinations of: 12345 ?

Like so:

12345 21345 3's down 4's down 5's down

12354 21354

12435 21435

12453 21453

12534 21534

12543 21543

13245 23145

13254 23154

13425 23415

13452 23451

13524 23514

13542 23541

14235 24135

14253 24153

14325 24315

14352 24351

14523 24513

14532 24531

15234 25134

15243 25143

15324 25314

15342 25341

15423 25413

15432 25431

I don't need them displayed like this, I only want to be able to find every combination.

Thanks,

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

How could I make an array to go thru all possible combinations of:  12345 ?

you need an algorithm to calculate the permutions of 12345. Below I posted a link with a pretty good explanation and some C++ sample code. Use this to create an AutoIT function.

http://www.codeguru.com/Cpp/Cpp/algorithms...icle.php/c7605/

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Moderators

Thanks /dev/null ... Although I don't know C++ , it's at least a start in some direction.

Off to see how much more I can confuse myself.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I'll give you some hints about how to transcode the source.It's been a while since i've dealt with C== so i'll try my best

In C++ When you create a new variable you usually declare the type.in autoit that is not needed most of the times.when you see a variable with an * before it ,it's a pointer.There is nothing like this in autoit. So this code in C++

void circularPrint(char *str) {

    int  len = strlen(str);
    char *mystr = new char [len +1];
    int i,j;

    for(i=0; i <len; i++ ) {
        for(j=0;j<len; j++ )
            mystr[j] = str[(i+j)%len];
        mystr[j] =0;
    totCount++;
        // comment the line below to supress the string output
        cout << mystr  << endl;

    }

    delete []mystr;
    return;
}

would be like this in AutoIt

Func circularPrint($str)

$len=StringLen($str)

$mystr=$len+1

for $i=0 to $len -1
    for $j=0 to $len -1
        mystr[$j]=str[($j+$i)%$str] <--This doesnt exist in Autoit and i'm not sure what it does to explain it....
        mystr[$j]=0
        msgbox(0,"",$mystr)
     Next
Next

endfunc

As you can see there is a diffent approach in working with arrays in C++ cos $mystr and $str are arrays.You have to change it according to the autoit approach.

The 2 sources above dont do the same thing (the autoit one doesnt even work) but i was just trying to clarify some of the symbology of C++. I hope someone more experienced than me will tell you more....

Good Luck.

Link to comment
Share on other sites

Thanks /dev/null ... Although I don't know C++ , it's at least a start in some direction.

Off to see how much more I can confuse myself.

<{POST_SNAPBACK}>

Try this. I found my own algorithm for this problem....

func rotate($sString, $nRotateLevel)

    local $aStringArray = StringSplit($sString,"")
    local $nStartRotate = $aStringArray[0] - $nRotateLevel + 1
    local $n, $tempchar, $tempstr = "", $retval = ""

    for $n = 1 to $nStartRotate - 1
        $tempstr= $tempstr & $aStringArray[$n]
    next
    
    $tempchar = $aStringArray[$nStartRotate]

    for $n = $nStartRotate+1 to $aStringArray[0]
        $retval = $retval & $aStringArray[$n]
    next 
    
    $retval = $tempstr & $retval & $tempchar

    return $retval
endfunc


func permute_internal($sString, $nRotateLevel, byref $permutations)

    local $n, $str
    dim $arr[$nRotateLevel]
  
    if $nRotateLevel = 2 then
       $permutations = $permutations & ":" & rotate($sString,$nRotateLevel)
       return
    endif

    $str = $sString
    for $n = 0 to $nRotateLevel -1
        $str = rotate($str,$nRotateLevel)
        $arr[$n] = $str

       ;--- special check, to stop a level beeing printed twice ---
        if not (($n = 0) AND (StringLen($sString) > $nRotateLevel)) then 
           $permutations = $permutations & ":" & $arr[$n]
        endif

        permute_internal($arr[$n],$nRotateLevel-1,$permutations)
    next

endfunc 

func permute($sString)

     global $permutations = ""
     permute_internal($sString,StringLen($sString),$permutations)
     $permutations = StringTrimLeft($permutations,1)

     return StringSplit($permutations,":")
endfunc 

;-----------------------------------------------------------------------------
;--  MAIN
;-----------------------------------------------------------------------------

$permutarray = permute("1234")

$msg = ""
for $n = 1 to $permutarray[0]
   $msg = $msg & $permutarray[$n] & @CRLf
next

msgbox(0,"", $msg)

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

/dev/null gave you what you need from what i see.Last night when i saw your post I had a different approach with a simple 2-dimensional array.I'm not goin to write the code cos i didnt finish it since your issue is solved.But i'd like it posted so as to see if that indeed works....I'm goin to explain the logic behind this...This logic is used when you make truth tables in logical circuits....

The possible combinations are 5^5=3125 so you make this array $numbers[3126][6] .I use one more to exclude 0's.... So we have a matrix...

A\B   0  |   1(5^4)  |   2 (5^3)   |   3(5^2)   |    4(5^1)   |   5(5^0)

0  |   -           -                -                -                -                  -

1  |   -           1                1                1               1                 1

2  |   -           1                1                1               1                 2

3  |   -           1                1                1               1                 3

4  |   -           1                1                1               1                 4

5  |   -           1                1                1               1                 5

6  |   -           1                1                1               2                 1

7  |   -           1                1                1               2                 2

..

3125

And I'm explaining.The powers indicate every when a digit increases. So the 5th digit changes every 5^0=1 digit the 4th one every 5^1=5 digits ,the 3rd one every 5^2=25 digits and so on.So if you fill the 5th element with numbers from 1 to 5 so many times that it reaches 3125 (that is 3125\(5*1)=625 times), the 4th element with 1 to 5 and every number repeating itself (5^1)=5 times before increasing until it reaches 3125 (that is 3125\(5*5)=125 times), the 3rd element with numbers 1 to 5 with every number repeating for (5^2)=25 times before increasing until it reaches 3125 (that is 3125\(5*25)= 25 times) and so on then you will fill the array with all possible combinations.

To access that - let's say combination #234 a simple for is needed:

For $i = 1 to 5 
     $combination&=$number[234][$i]
Next

MsgBox(0,"","Combination #234 : " & $combination)

If you want to access all possible combinations then what you need is a double For

with $i=1 to 5 and $j=1 to 3125 and you are done...

That's all. I hope i made the concept clear to everyone who will bother reading it...And i hope i was right....

C ya....

Edited by hgeras
Link to comment
Share on other sites

The possible combinations are 5^5=3125 so you make this array $numbers[3126][6]  .I use one more to exclude 0's.... So we have a matrix...

actually you are talking about a permutation with repetition, while ronsrules asked for a permutation without. The difference is:

with repetition you can reuse a single value serveral times: 12355, 12555, 15555, 55555. Possible values: n^n

without repetition you can reuse a single value only once: 12345, 12354, etc. Possible values: n!

Calculationg a permutation with repetition is much easier than without, because it's just a number system with base n (like binary system, octal system, etc.) , which is basically what you described.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

  • Moderators

@hgeras - very powerful stuff ... Thanks so much, and the diagram was very useful :whistle:

@/dev/null - Woh!, this was 100% what I was looking for. Thanks so much... had my head burried in that C++ script for some time now :dance:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Well, i understand the difference . But it wasnt so clear on behalf of ronsrules and i made what he asked.he said "How could I make an array to go thru all possible combinations of: 12345 ?" and that i did....

But every possible permutation without repetition has a sum of 15 ... So if you add the $non_repetitive variable and an If statement in the reading loop:

For $i=1 to 3125
     For $j=1 to 5
          $combination&=$number[$i][$j]
           $non_repetitive+=$number[$i][$j]
      Next
   If $non_repetitive=15 Then MsgBox(0,"",$combination)
Next

You automatically exclude all permutations with repetition...And you have 2 in 1 with the same code! Like heads & shoulders.... :whistle:

/dev/null I'm not trying to reject or degrade your code.... I just made my own approach in the problem with the little brains i have....

C ya....

EDIT:I corrected /dev/null typo(\dev\null)

Edited by hgeras
Link to comment
Share on other sites

\dev\null I'm not trying to reject or degrade your code.... I just made my own approach in the problem with the little brains i have....

No problem with that. I was just trying to clarify things. Based on the examples ronsrules gave, I came to the conclusion, that he wants a permutation without repetition.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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