Jump to content

Creating a product code from defined letters/numbers


 Share

Recommended Posts

I think this produces the right results, but I haven't checked it properly yet. The code now produces 31776 results, but the problem is that it now takes several minutes to run as opposed to just a few seconds it took to run before the fix. I'm not entirely happy with that. I'm pretty certain that there exists a better method.

Code Contains Errors

#include <Array.au3>
#include <String.au3>

Main()

Func Main()
    Local $aChars[4] = ["1", "2", "a", "b"]
    $aCharPermuted = _ArrayPermute($aChars, ",")

    Local $aCombinations[16], $n = 0
    For $i = 0 To 3
        For $j = 0 To 3
            $aCombinations[$n] = $aChars[$i] & $aChars[$j]
            $n +=1
        Next
    Next

    Local $aSplit, $aCharBase1[864], $aCharBase2[1152], $m = 0
    $n = 0
    For $i = 1 To $aCharPermuted[0]
        $aSplit = StringSplit($aCharPermuted[$i], ",", 2)
        For $j = 0 To 2
            $aCombiTemp = _SelectCombinations($aSplit, $aCombinations, $j)

            For $k = 0 To 11 ; Add combinations of two consecutive characters
                $aCharBase1[$n] = _InsertCombination($aSplit, $aCombiTemp[$k], $j)
                $n +=1
            Next
        Next

        For $k = 0 To 3 ; Add two separated characters
            For $l = 0 To 3
                $aCharBase2[$m] = _InsertChars($aSplit, $aChars[$k], $aChars[$l], 0, 1)
                $m +=1
                $aCharBase2[$m] = _InsertChars($aSplit, $aChars[$k], $aChars[$l], 0, 2)
                $m +=1
                $aCharBase2[$m] = _InsertChars($aSplit, $aChars[$k], $aChars[$l], 1, 2)
                $m +=1
            Next
        Next
    Next

    $aCharBase1 = _ArrayUnique($aCharBase1)
    $aCharBase2 = _ArrayUnique($aCharBase2)

    Local $aCombined[$aCharBase1[0] + $aCharBase2[0]]

    For $i = 1 To $aCharBase1[0]
        $aCombined[$i -1] = $aCharBase1[$i]
    Next
    $aCharBase1 = 0

    $n = $i -1
    For $i = 1 To $aCharBase2[0]
        $aCombined[$n] = $aCharBase2[$i]
        $n +=1
    Next
    $aCharBase2 = 0

    $aCombined = _ArrayUnique($aCombined)

    Local $aFinalResult[31776]
    $n = 0

    For $i = 1 To $aCombined[0]
        For $j = 0 To 15
            $sOutput = $aCombined[$i] & $aCombinations[$j]
            If Not StringRegExp($sOutput, "1111|2222|aaaa|bbbb") Then
                $aFinalResult[$n] = $sOutput
                $n +=1
            EndIf
        Next
    Next

    For $i = 0 To 20183
        $sOutput = _StringReverse($aFinalResult[$i])
        For $j = 0 To 20183
            If $sOutput = $aFinalResult[$j] Then ContinueLoop 2
        Next

        $aFinalResult[$n] = $sOutput
        $n += 1
    Next

    _ArrayDisplay($aFinalResult)
    For $i = 0 To 31775
        FileWriteLine("Output.txt", $aFinalResult[$i])
    Next
EndFunc ;==> Main()

Func _InsertChars($aSplit, $char1, $char2, $pos1, $pos2)
    $aSplit[$pos1] &= $char1
    $aSplit[$pos2] &= $char2

    Local $sTemp = ""
    For $a = 0 To 3
        $sTemp &= $aSplit[$a]
    Next
    Return $sTemp
EndFunc

Func _InsertCombination($aSplit, $aCharCombi, $j)
    $aSplit[$j] &= $aCharCombi
    Local $sTemp = ""
    For $a = 0 To 3
        $sTemp &= $aSplit[$a]
    Next
    Return $sTemp
EndFunc

Func _SelectCombinations($aSplit, $aCombinations, $j)
    Local $sCharAvoid
    $aTemp = $aCombinations
    ; When inseting chracters =>
    ; Character are only allowed to repeat after they occur in the original 4 char sequence.
    $sCharAvoid = $aSplit[$j +1]
    For $a = UBound($aTemp) -1 To 0 Step -1
        If StringRight($aTemp[$a], 1) = $sCharAvoid Then _ArrayDelete($aTemp, $a)
    Next
    Return $aTemp
EndFunc

That's just too slow, although I think I can tweak it.

How many characters are you using? Also, what do you mean a few permutations are missing? That would be quite remarkable if that were true because the _ArrayPermute function will output every possible permutation of the information fed to it.

Yes but the sought after combinations are specific: allowing no more than two consecutive repeats of the same character. Reread the first post. Edited by czardas
Link to comment
Share on other sites

  • Replies 44
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Accually first post says ...

No character should repeat four times aaaa isn't acceptable.

So 'aaa' should be fine.

Personally I thing the OP specs are kinda fussy.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Isn't that what I just said? :mellow:

a => 0 consecutive repeats

aa => 1 consecutive repeat

aaa => 2 consecutive repeats

That's not what the OP said though. He stated that there can't be 4 characters repeated in a row, such as aaaa bbbb 1111 or 2222. My code doesn't allow that to happen as the longest repeat sequence is only going to be 2 characters long. Although with only 4 characters to play with, the total number of permutations is only going to give you 24 possible combinations. More characters, more choices, but that's not what he was asking for. You could add in to the array another round of 1,2,a,b and that will give you an 8 character array with about 40,000+ permutations with no character repeating more than twice or 4 times if you use both arrays. Global $Array1[8] = [1, 2, "a", "b", 1, 2, "a", "b"]. If you wanted to be really fancy, take the code I posted, and use random selections of either array and combine them together.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

You could add in to the array another round of 1,2,a,b and that will give you an 8 character array with about 40,000+ permutations with no character repeating more than twice or 4 times if you use both arrays. Global $Array1[8] = [1, 2, "a", "b", 1, 2, "a", "b"].

I tried your suggestion, but it doesn't fulfil all the criteria.

Many patterns are missing:

What about this? 1a2abaaa yes again, as long as no char repeats

Edited by czardas
Link to comment
Share on other sites

Isn't that what I just said? :mellow:

:) ... Think I should have quoted it.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

#include <array.au3>

Global $chararray[4] = ["a", "b", "1", "2"]
Global $permutationarray[16777216]


For $a = 0 To 3
    For $b = 0 To 3
        For $c = 0 To 3
            For $d = 0 To 3
                For $e = 0 To 3
                    For $f = 0 To 3
                        For $g = 0 To 3
                            For $h = 0 To 3
                                For $i = 0 To 3
                                    For $j = 0 To 3
                                        For $k = 0 To 3
                                            For $l = 0 To 3
                                                $permutationarray[$a+$b*4+$c*16+$d*64+$e*256+$f*1024+$g*4096+$h*16384+$i*65536+$j*262144+$k*1048576+$l*4194304] = $chararray[$a] & $chararray[$b] & $chararray[$c] & $chararray[$d] & $chararray[$e] & $chararray[$f] & $chararray[$g] & $chararray[$h] & $chararray[$i] & $chararray[$j] & $chararray[$k] & $chararray[$l]
                                            Next
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next
    Next
Next

For $m = 16777215 To 0 Step -1
    If StringInStr($permutationarray[$m], "aaaa") <> 0 Or StringInStr($permutationarray[$m], "bbbb") <> 0 Or StringInStr($permutationarray[$m], "1111") <> 0 Or StringInStr($permutationarray[$m], "2222") <> 0 Then _ArrayDelete($permutationarray, $m)
Next

Edit: Fixed some bugs.

Edited by nAutoIT
Link to comment
Share on other sites

And then there was a long long pause ... nAutoIT: you got some estimated run-time to go with that. :)

---

Trying to get rid of some of the array calls ... "Error allocating memory." Oops. Guess that means I hit the string limit. (or just memoried out) :mellow:

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

#include <array.au3>

Global $chararray[3] = ["a", "b", "1", "2"]
Global $permutationarray[16777215]


For $a = 0 To 3
    For $b = 0 To 3
        For $c = 0 To 3
            For $d = 0 To 3
                For $e = 0 To 3
                    For $f = 0 To 3
                        For $g = 0 To 3
                            For $h = 0 To 3
                                For $i = 0 To 3
                                    For $j = 0 To 3
                                        For $k = 0 To 3
                                            For $l = 0 To 3
                                                $permutationarray[$a+$b*4+$c*16+$d*64+$e*256+$f*1024+$g*4096+$h*16384+$i*65536+$j*262144+$k*1048576+$l*4194304] = $chararray[$a] & $chararray[$b] & $chararray[$c] & $chararray[$d] & $chararray[$e] & $chararray[$f] & $chararray[$g] & $chararray[$h] & $chararray[$i] & $chararray[$j] & $chararray[$k] & $chararray[$l]
                                            Next
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next
    Next
Next

For $m = 16777215 To 0 Step -1
    If StringInStr($permutationarray[$m], "aaaa") <> 0 Or StringInStr($permutationarray[$m], "bbbb") <> 0 Or StringInStr($permutationarray[$m], "1111") <> 0 Or StringInStr($permutationarray[$m], "2222") <> 0 Then _ArrayDelete($permutationarray, $m)
Next

I haven't tried to run this yet, how long does it take? You also need to ensure that all four characters appear at least once. Edited by czardas
Link to comment
Share on other sites

#include <array.au3>

Global $chararray[4] = ["a", "b", "1", "2"]
;Global $permutationarray[16777216]
Global $permutationarray[1048576]

For $a = 0 To 3
    For $b = 0 To 3
        For $c = 0 To 3
            For $d = 0 To 3
                For $e = 0 To 3
                    For $f = 0 To 3
                        For $g = 0 To 3
                            For $h = 0 To 3
                                For $i = 0 To 3
                                    For $j = 0 To 3
                                        ;For $k = 0 To 3
                                            ;For $l = 0 To 3
                                                $permutationarray[$a+$b*4+$c*16+$d*64+$e*256+$f*1024+$g*4096+$h*16384+$i*65536+$j*262144] = $chararray[$a] & $chararray[$b] & $chararray[$c] & $chararray[$d] & $chararray[$e] & $chararray[$f] & $chararray[$g] & $chararray[$h] & $chararray[$i] & $chararray[$j]; & $chararray[$k] & $chararray[$l]  ;+$k*1048576+$l*4194304
                                            ;Next
                                        ;Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
        Next
    Next
Next
MsgBox(1,"Progress","Permutation Complete")

For $m = 1048575 To 0 Step -1   ; $m = 16777215
    If StringInStr($permutationarray[$m], "aaaa") <> 0 Or StringInStr($permutationarray[$m], "bbbb") <> 0 Or StringInStr($permutationarray[$m], "1111") <> 0 Or StringInStr($permutationarray[$m], "2222") <> 0 Or StringInStr($permutationarray[$m], "a") = 0 Or StringInStr($permutationarray[$m], "b") = 0 Or StringInStr($permutationarray[$m], "1") = 0 Or StringInStr($permutationarray[$m], "2") = 0 Then _ArrayDelete($permutationarray, $m)
Next

The Permutation Part needs 3 minutes on my mediocre PC.

I split it to 1/16 of the original size, to test the 2nd part and it is still running.

Dont know how slow StringInStr and _ArrayDelete are because i have never used them before.

Link to comment
Share on other sites

I tested and calculated a little and my estimation is: 500-600 hours for the 2nd part of the script. :mellow:

- _ArrayDelete() would be my first choice to remove from that loop. (Redim-ing being notorious slow.) Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

I tried your suggestion, but it doesn't fulfil all the criteria.

Many patterns are missing:

I see that now. Well, I guess 40,000+ choices wouldn't be enough if you have to account for all of the possibilities that would entail. Although I can't imagine what kind of product code would only have 4 possible character choices, and why you'd need that many of them.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Haha, I thought maybe it has to do with the human genome. :mellow:

:) Then he'd have to be using GCTA and not 12AB.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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