Jump to content

permutation calc


Recommended Posts

I've been unable to find such a tool online but it shouldn't be too difficult with AutoIt. I want to a make a tool that writes all permutations possible from a combination of numbers and/or letters to a file. So it would ask the length/number of characters in the output and what number(s) and letter(s) to use and then write all permutations to a file.

Getting the input and writing the output is easy, I can do that. I'm not sure how to go about the calculations to generate the output. Ideas greatly appreciated.

This signature is computer generated, nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#.......

Link to comment
Share on other sites

  • Moderators

I've been unable to find such a tool online but it shouldn't be too difficult with AutoIt. I want to a make a tool that writes all permutations possible from a combination of numbers and/or letters to a file. So it would ask the length/number of characters in the output and what number(s) and letter(s) to use and then write all permutations to a file.

Getting the input and writing the output is easy, I can do that. I'm not sure how to go about the calculations to generate the output. Ideas greatly appreciated.

<{POST_SNAPBACK}>

Dev\Null Wrote this: Permutation Edited by ronsrules

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

Dev\Null Wrote this:  Permutation

<{POST_SNAPBACK}>

Thank you. That is quite useful but I'm afraid I don't understand the uncommented code enough to modify it for my needs. I need to permutate "dynamically" if you will, not statically like dev/null/ s code does. In other words, feed it what characters to permutate and how many digits or characters the output should be.

This signature is computer generated, nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#.......

Link to comment
Share on other sites

  • Moderators

In other words, feed it what characters to permutate and how many digits or characters the output should be.

I might not be understanding? But this is an example of just stringing left as many as the Output you choose.

$permutarray = permute("1234")

#include <string.au3>
#include <array.au3>

$Digits = InputBox("Permutation", "Enter Digits", "", "", 80, 80)
$Output = InputBox("Permutation", "OutPut Number", "", "", 80, 80)

$permutarray = permute($Digits)
$msg = ""
for $n = 1 to $permutarray[0]
   $pString = StringLeft($permutarray[$n], $Output)
   $msg = $msg & $pString & @CRLf
next

msgbox(0,"", $msg)

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

Edit: Forgot an EndFunc

Edited by ronsrules

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

Lets say for example I want it to permutate 0123456789wtf as many times as possible in a 3 character string without repeating.

012

123

234

345

456

567

678

789

89w

9wt

wtf

111

112

113

124

125

126

and the list goes on

Edited by anystupidassname

This signature is computer generated, nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#.......

Link to comment
Share on other sites

  • Moderators

Err that seems it would take a very very long time :whistle:

Would be nice for myself to see if there is a quicker method.

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

  • Moderators

Guess it would of helped if I tested my own script to see if it had duplicates... sorry.

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

Lets say for example I want it to permutate 0123456789wtf as many times as possible in a 3 character string without repeating.

There are 1320 possible 3-character permutations (draw 3 out of 12). So, you need to find the 220 unique triples and then use my function to permute each of them.

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

There are 1320 possible 3-character permutations (draw 3 out of 12). So, you need to find the 220 unique triples and then use my function to permute each of them.

Cheers

Kurt

<{POST_SNAPBACK}>

@ronsrules: no worries. you've helped no matter what.

@/dev/null/: I'm confused. :whistle:

First "0123456789wtf" is 13 characters, not 12.

Which would give 1716 and 286.

Second, if you "draw 12 out of 3" what happens?

I'd like to enter ANY characters to compose the strings with and the string length to output. This could mean "35b6y8w2" output to two character strings or "k2j" output to seven character strings.

For the sake of simplicity, I'm holding off on special characters.

Am I not understand something or did we have a miscommunication?

I appreciate the help!

Edited by anystupidassname

This signature is computer generated, nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#.......

Link to comment
Share on other sites

@/dev/null/: I'm confused.  :whistle:

First "0123456789wtf" is 13 characters, not 12.

Which would give 1716 and 286.

Ups, sorry I did not count the 0 :dance:

Second, if you "draw 12 out of 3" what happens?

You can't draw 12 auf ot 3, because after 3 items there is nothing left!

(without putting items back - which you never mentioned)

I'd like to enter ANY characters to compose the strings with and the string length to output. This could mean "35b6y8w2" output to two character strings or "k2j" output to seven character strings.

O.K. now I'm confused. I have no idea what you want!

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(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

O.K. now I'm confused. I have no idea what you want!

I will e'splain, no der is too mush, I will sum up...

Perhaps I'm using terms improperly... I suppose "permutating items back" as you put it is what I want to do?!? Sorry for being so unclear...

Draw 2 out of 4 and you should get 12 right?

Then why did I get 16?

From "w1t2" output to 2 character string you get:

ww

w1

wt

w2

1w

11

1t

12

tw

t1

tt

t2

2w

21

2t

22

From "w1" output to 3 character string you get:

w1w

www

111

1w1

11w

ww1

w11

1ww

This signature is computer generated, nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#nothing can go wron#.......

Link to comment
Share on other sites

I will e'splain, no der is too mush, I will sum up...

Perhaps I'm using terms improperly... I suppose "permutating items back" as you put it is what I want to do?!? Sorry for being so unclear...

Draw 2 out of 4 and you should get 12 right?

Then why did I get 16?

From "w1t2" output to 2 character string you get:

ww

w1

wt

w2

1w

11

1t

12

tw

t1

tt

t2

2w

21

2t

22

From "w1" output to 3 character string you get:

w1w

www

111

1w1

11w

ww1

w11

1ww

<{POST_SNAPBACK}>

so you're ok with characters being repeated in output? (2 w's when there was only 1 in the output...) not sure exactly what you're trying to do, but i made a spreadsheet to cheat at wordwhomp on pogo.com a while back that used a similar concept... here is my code on it (vba code, not AutoIT but concepts seem to be the issue not syntax)

'*******************************************************************************************
'this is to figure out every possible combination of 1 - 6 without duplicating
'*******************************************************************************************'
Private Sub getnums()
arg = 1
For x = 1 To 666666 'there are 6 characters, for 9 characthers it'd be 999999999
test = CStr(x)'because x is a number have to convert it to a string to check the length
If Len(test) > 2 Then
donot = False'sets a check condition that if unchanged means the number has no duplicates
    For y = 1 To Len(test)'checks each digit in the number
        rest = Right(test, Len(test) - y)'figures out what digits are to the right  of the character we want to check
        achar = Mid(test, y, 1)'decides what char we're looking at
        If InStr(1, rest, achar) Or Val(achar) > 6 Or Val(achar) = 0 Then donot = True'if our char is at any other position in the string, or if our char is higher than the number of chars (in this case 6) then it would not be a valid combination
    Next y'next digit
    
If donot = False Then'if the entire number has been checked that no digit occurs more than once and no digit is greater than the number of characters input
        Range("a" & arg).Formula = test'then the number is added to list of good numbers
        arg = arg + 1'index is incremented so next good number doesn't overwrite...
End If
End If
Next x
End Sub

ok, so that figures out the good numbers to use.

in my code i worked with the list of numbers and for each number in the list, i made a string of the characters in the positions...

example, if the input string was 'cat' and the number was '321' it would grab 3rd number first, then second then first....'tac'. my code then spell checked it to see if it was a good word and added to a list if it was, but i didn't include that portion as it doesn't really relate. anyway, i hope my code helps somewhat, atleast conceptually...

Link to comment
Share on other sites

Draw 2 out of 4 and you should get 12 right?

Correct. Forumula: m!/(m-n)! ==> 4!/(4-2)! == 12

Then why did I get 16?

From "w1t2" output to 2 character string you get:

ww

because what you want is not a permutation. See this LINK for an explanation.

For your problem use this code:

$outputlen = 2
$inputlen = 4
dim $input[$inputlen] = ["w", "1", "t", "2"]
dim $index[$outputlen]


$nMaxVal = $inputlen ^ $outputlen

$msg = ""

for $nCounter = 1 to $nMaxVal
    
    $output = ""
    for $i = 0 to $outputlen-1
        $output &= $input[$index[$i]]
    next
    $msg = $msg & $output & @CRLF

    $val = $nCounter
    
    for $n = $outputlen-1 to 0 step -1
        $index[$n] = mod($val,$inputlen)
        $val = int($val / $inputlen)
    next
next

ClipPut($msg)

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(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

I think a better way of saying what the original poster wants is

all possible words of length n given a specific set or alphabet.

In that case, lets use your example of "w1t2" and two letter words.

x = length of alphabet

n = word length

So for a word length of 2 you have n^x = 2^4 = 16 possibilities.

Which is the number you got.

Now could do something like this (pseudocode) :

func getwords(alphabet, word length)
  depth = 1
  currword = ""
  recurseFunc(alphabet, word length, depth, currword)
endfunc

func recurseFunc(alphabet, word length, depth, currword)
  char = first char from alphabet
  while not end of alphabet
    currword = currword & char
    if depth == wordlength
      do_stuff(currword)
      return
    else
      recurseFunc(alphabet, word length, depth, currword)
    end if
  wend
endfunc

Not the greatest, but it is the best I could do off the top of my head.

Some of the vars could be global or static (if you can do that with autoit ;-)

Link to comment
Share on other sites

my code then spell checked it to see if it was a good word and added to a list if it was, but i didn't include that portion as it doesn't really relate.  anyway, i hope my code helps somewhat, atleast conceptually...

Could you post your code? I'd like to see this in action in Excel. Thanks!

My UDFs: ExitCodes

Link to comment
Share on other sites

I think a better way of saying what the original poster wants is

all possible words of length n given a specific set or alphabet.

That's exactly what my code does.

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