Sign in to follow this  
Followers 0
litlmike

Crack the Case

6 posts in this topic

I am looking for a way to list all possible permutations for a set of elements. So then, the user can input any number of elements for the set, and the script will output all possible permutations.

For example:

User inputs - A, B, C

Ouput - {A, B, C}; {A, C, B}; {B, A, C}; {B, C, A}; {C, A, B}; {C, B, A}

Can someone tell me how to get script to do this?

Share this post


Link to post
Share on other sites



http://www.autoitscript.com/forum/index.ph...st&p=190961

Did you search the forums for permutation?

Thank you. Yes I did search the forums, but there were a few options available (one was in C++) and I couldn't make sense out of the scripts I found. Thank you for clearing that up for me. Is there a way to resize the input box, or display this differently? After about 100 permutations the MsgBox is taller than my monitor.

Thanks Again.

Share this post


Link to post
Share on other sites

Look in the help file under FileWrite...

(original permutation code written by dev\null)

$file = FileOpen("test.txt", 1)

; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$permutarray = permute("1234")
$msg = ""
For $n = 1 To $permutarray[0]
    $msg = $msg & $permutarray[$n] & @CRLF
Next

FileWrite($file, $msg)
FileClose($file)

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   ;==>rotate

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   ;==>permute_internal

Func permute($sString)
    Global $permutations = ""
    permute_internal($sString, StringLen($sString), $permutations)
    $permutations = StringTrimLeft($permutations, 1)
    Return StringSplit($permutations, ":")
EndFunc   ;==>permute

[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

the short version for your use

Dim $input = "A,B,C"
Dim $List = StringSplit($input, ",")

$result = "{" & $List[1] & "," & $List[2] & "," & $List[3] & "};"
$result = $result & "{" & $List[1] & "," & $List[3] & "," & $List[2] & "};"
$result = $result & "{" & $List[2] & "," & $List[1] & "," & $List[3] & "};"
$result = $result & "{" & $List[2] & "," & $List[3] & "," & $List[1] & "};"
$result = $result & "{" & $List[3] & "," & $List[1] & "," & $List[2] & "};"
$result = $result & "{" & $List[3] & "," & $List[2] & "," & $List[1] & "};"

MsgBox(64, "combos", $result)

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites

Val,

If you are going to give the OP exactly what was asked for, then find the diff in the code below and your code. :-) :-)

Dim $input = "A,B,C"
Dim $List = StringSplit($input, ",")

$result = "{" & $List[1] & ", " & $List[2] & ", " & $List[3] & "}; "
$result = $result & "{" & $List[1] & ", " & $List[3] & ", " & $List[2] & "}; "
$result = $result & "{" & $List[2] & ", " & $List[1] & ", " & $List[3] & "}; "
$result = $result & "{" & $List[2] & ", " & $List[3] & ", " & $List[1] & "}; "
$result = $result & "{" & $List[3] & ", " & $List[1] & ", " & $List[2] & "}; "
$result = $result & "{" & $List[3] & ", " & $List[2] & ", " & $List[1] & "}"

MsgBox(64, "combos", $result)
...but don't space out about it.

(Okay, it was a stretch...)


[size="1"][font="Arial"].[u].[/u][/font][/size]

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
Sign in to follow this  
Followers 0