Jump to content

Recommended Posts

Posted (edited)

Hi guys!, i have a problem to convert Python code to AutoIt code, in fact i had not coded with Python yet!, this code is about permutation a string's case, i will be happy with your comments :)❤;

Python code:
 

# Python code to print all permutations
# with respect to cases

# Function to generate permutations
def permute(inp):
    n = len(inp)

    # Number of permutations is 2^n
    mx = 1 << n

    # Converting string to lower case
    inp = inp.lower()

    # Using all subsequences and permuting them
    for i in range(mx):
        # If j-th bit is set, we convert it to upper case
        combination = [k for k in inp]
        for j in range(n):
            if (((i >> j) & 1) == 1):
                combination[j] = inp[j].upper()

        temp = ""
        # Printing current combination
        for i in combination:
            temp += i
        print(temp),
        
# Driver code
permute("Hello")

# This code is contributed by Sachin Bisht


My code in AutoIt:

; https://www.geeksforgeeks.org/permute-string-changing-case/

_PermuteCase("ABC")

Func _PermuteCase($sText)
    If StringRegExp($sText, "^[A-Za-z]{1,}$") Then
        Local $iLength = StringLen($sText) ; Get length of the text.
        Local $iMaxPerm = 2 ^ $iLength ; Number of permutations is 2^n
        Local $sLow_Text = StringLower($sText) ; Converting string to lower case
        Local $asChrs = StringToASCIIArray($sLow_Text) ; Split the text into array of chars.
        For $i = 1 To $iMaxPerm Step 1
            For $j = 0 To $asChrs[0]
                ;...................................................
            Next
        Next
    Else
        Return SetError(-1, 0, "Error: Input is incorrect!")
    EndIf
EndFunc   ;==>_PermuteCase

 

 

 

 

 

 

====================== SOLUTION by @TheXman ======================

 

Edited by Colduction
Posted

What is the code supposed to do? By permutations, do you mean lower case if it's upper case, and upper case if it's lower?

  Reveal hidden contents

IUIAutomation - Topic with framework and examples

Au3Record.exe

Posted
  On 5/18/2020 at 12:11 PM, careca said:

What is the code supposed to do?

Expand  

I want to get all states of a text, the formula of count of states is 2^n (n means length of the text)
For example, all states of the "ABC" text would be:

  1. abc
  2. Abc
  3. aBc
  4. ABc
  5. abC
  6. AbC
  7. aBC
  8. ABC
Posted (edited)

Well, reading the url from the autoit code, the desired output should be:

  Quote
abc Abc aBc ABc abC AbC aBC ABC
Expand  

when i look on these numbers, then i notice a pattern.

Binary numbers !  (% here represents binary number)

0 = %000, 1=%001 , 2=%010, 3=%011, 4=%100 ... 7=%111

the 1 in this example moves from right to left, but in the output, it moves from left to the right.

so basically, one needs to loop through each of the numbers in the binary format, and to apply upper case for each binary 1 .

The string is converted to lowercase at the beginning, so the first case would be "abc"

i hope this helps a bit.

 

 

Edited by Dan_555

Some of my script sourcecode

Posted
  On 5/18/2020 at 2:55 PM, Colduction said:

For example, all states of the "ABC" text would be:

Expand  

That isn't a permutation!

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

No it isn't.

A permutation is this: https://en.wikipedia.org/wiki/Permutation

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

Still not. It's a case toggle, you don't permute anything. Permuting is exchanging the order (place) of at least two elements.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted
  On 5/18/2020 at 3:36 PM, jchd said:

Still not. It's a case toggle, you don't permute anything. Permuting is exchanging the order (place) of at least two elements.

Expand  

Normal Permutation was changes order of chars, but Case Permutation is changing order (2 modes, Uppercase or Lowercase) of capital case state.

Posted

Enough for me.

  Reveal hidden contents

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

Someone wrote a function for the binarynumbers, here 


and here is what you can do:

#include <String.au3>
$y=0
$t="abc"
for $x=$y to $y+7
    $m=""
    $z=DecToBase($x,2)
    $z=_StringRepeat("0",3-StringLen($z)) & $z
    ;ConsoleWrite($z & @crlf)
    For $j=3 to 1 step -1
     $k=StringMid($z,$j,1)
     $l=StringMid($t,4-$j,1)
     If $k=1 Then
        $m=$m & StringUpper($l)
     Else
        $m=$m & $l
     EndIf
    Next
    ConsoleWrite ($m & @CRLF)
Next

Func DecToBase($iInt, $iBase) ; for bases 2 to 9
    Local $iRem, $sRet = ''
    While $iInt > $iBase -1
        $iRem = Mod($iInt, $iBase)
        $sRet = $iRem & $sRet
        $iInt = Int(($iInt - $iRem) /$iBase)
    WEnd
    Return $iInt & $sRet
EndFunc ;==> DecToBase

 

Some of my script sourcecode

Posted (edited)

This is from this GeeksForGeeks article: https://www.geeksforgeeks.org/permute-string-changing-case/

You should check out the code for this article as well, which really helps explain what they're doing to get the permutation: https://www.geeksforgeeks.org/subarraysubstring-vs-subsequence-and-programs-to-generate-them/

Careca: No, he's getting all possibilities of upper/lower case combinations for a string... so Permutate("Hi") would return an array of ["HI", "hi", "Hi", "hI"]. (I had to read the article to understand)

  Reveal hidden contents

Edit: Whoops, I've been working on this for two hours and didn't realize that others had posted... :D

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

  Reveal hidden contents
Posted
  On 5/18/2020 at 4:13 PM, seadoggie01 said:

This is from this GeeksForGeeks article: https://www.geeksforgeeks.org/permute-string-changing-case/

Expand  

Yes, i had mentioned this site in my source code above.

  On 5/18/2020 at 4:13 PM, seadoggie01 said:

so Permutate

Expand  

Do you confirm that this is a permutation? in my opinion, this is a permutation, but other dear members think that this is not a permutation.

  On 5/18/2020 at 4:13 PM, seadoggie01 said:
Expand  

I've red this section, but it was hard for me to know, because C#, C++ and other languages examples were close together (in writing code), but Python and AutoIt were different in some cases.

Posted
  On 5/18/2020 at 4:22 PM, Colduction said:

Yes, i had mentioned this site in my source code above

Expand  

Ooops, I'm a bit blind too apparently.

I was going off what you and the website said. I didn't read all the comments until a few minutes ago. It doesn't mention anywhere in the Wiki article anything about using characters in a permutation and it does sound like permutations are related to order changes, but a rose by any other name would smell as sweet. Stop worrying about what it is and worry about what it does. 😐

I found that second website enlightening because I failed to realize how the code worked... I thought using the 1's and 0's of the binary form to determine which letters to capitalize was pure genius. I also couldn't visualize it until I printed the binary numbers out :D (Hence my BitToString function)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

  Reveal hidden contents
Posted (edited)

Give this a spin:

example()

Func example()
    Const $ALPHA_STRING = "aBc"

    Local $aChars      = StringSplit($ALPHA_STRING, "")
    Local $iNbrOfChars = $aChars[0]
    Local $sString     = ""

    For $i = 0 To (2 ^ $iNbrOfChars) - 1                 ;Loop thru iterations (base2 000, 001, 010, ... 111)
        $sString = ""                                    ;   Initialize string
        For $j = 0 To $iNbrOfChars - 1                   ;   Loop thru chars
            If BitAND($i, 2 ^ $j) Then                   ;     If bit $j of $i is 1
                $sString &= StringUpper($aChars[$j+1])   ;        Append uppercase char to string
            Else                                         ;     Else
                $sString &= StringLower($aChars[$j+1])   ;        Append lowercase char to string
            EndIf                                        ;     EndIf
        Next                                             ;   End char loop
        ConsoleWrite($sString & @CRLF)                   ;   Display string iteration
    Next                                                 ;End iteration loop
EndFunc

 

Edited by TheXman
Added comments to make it easier to understand
Posted
  On 5/18/2020 at 4:34 PM, TheXman said:

Give this a spin:

example()

Func example()
    Const $ALPHA_STRING = "abc"

    Local $aChars  = StringSplit($ALPHA_STRING, "")
    Local $sString = ""

    For $i = 0 To (2 ^ $aChars[0]) - 1
        $sString = ""
        For $j = 0 To $aChars[0] - 1
            If BitAND($i, 2 ^ $j) Then
                $sString &= StringUpper($aChars[$j+1])
            Else
                $sString &= StringLower($aChars[$j+1])
            EndIf
        Next
        ConsoleWrite($sString & @CRLF)
    Next
EndFunc

 

Expand  

Thanks for your coding!
It works like a charm!

The @Dan_555's code problem was that limited to 3 chars.

But your code is very tidy and speedy, it doesn't need to use #include <String.au3> UDF.

Thanks, i got my answer from you :)

  • Colduction changed the title to [SOLVED] Permute a string by changing it's case
Posted

You're welcome. :thumbsup:

It was a fun little exercise with binary bits as @Dan_555 originally pointed out.

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
×
×
  • Create New...