Jump to content

# [SOLVED] Permute a string by changing it's case

## Recommended Posts

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

Edited by Colduction
##### Share on other sites

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?

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

# IUIAutomation - Topic with framework and examples

##### Share on other sites

2 hours ago, careca said:

What is the code supposed to do?

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
##### Share on other sites

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

Quote
```
abc Abc aBc ABc abC AbC aBC ABC```

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

##### Share on other sites

25 minutes ago, Colduction said:

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

That isn't a permutation!

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)

##### Share on other sites

6 minutes ago, jchd said:

isn't a permutation!

But is a permutation!, read this site to know:

`https://www.geeksforgeeks.org/permute-string-changing-case/`
##### Share on other sites

No it isn't.

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

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)

##### Share on other sites

32 minutes ago, Dan_555 said:

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

Hmm, thanks, i think that BitShift must be used

##### Share on other sites

1 minute ago, jchd said:

I know! but this is a Case Permutation

##### Share on other sites

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

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)

##### Share on other sites

1 minute ago, 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.

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

##### Share on other sites

Enough for me.

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)

##### Share on other sites

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

##### Share on other sites

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)

Spoiler

This is how I (finally) managed to do it. From what I understand, >> in Python means BitShift() and & means BitAND(). I've never really worked with bit functions before, so this was very interesting.

```_ArrayDisplay(Permutate("hello"))

Func Permutate(\$sString)

Local \$iLen = StringLen(\$sString)

; Number of permutations is 2^n
Local \$iPerms = 2 ^ \$iLen

; Converting string to lower case
\$sString = StringLower(\$sString)

Local \$aCombination[\$iPerms]

; Using all subsequences and permuting them
For \$i = 0 To \$iPerms - 1
; If j-th bit is set, we convert it to upper case
\$aCombination[\$i] = \$sString
; For each character
For \$j = 1 To \$iLen
; Get the bit in \$i at position \$j (Minus 1 because it's 1 based)
If BitGet(\$i, \$j - 1) Then
; Set the character to be capitalized
\$aCombination[\$i] = StringSet(\$aCombination[\$i], \$j, StringUpper(StringMid(\$aCombination[\$i], \$j, 1)))
EndIf
Next

;~ ConsoleWrite(\$aCombination[\$i] & @CRLF)
Next

Return \$aCombination

EndFunc

Func StringSet(\$sString, \$iPoint, \$sChar)

Return StringLeft(\$sString, \$iPoint - 1) & \$sChar & StringTrimLeft(\$sString, \$iPoint)

EndFunc

Func BitToString(\$iBit)

Local \$sBit = "", \$i = 0
While (2 ^ \$i) <= \$iBit
\$sBit &= BitGet(\$iBit, \$i)
\$i+= 1
WEnd

Return StringReverse(\$sBit)

EndFunc

Func BitGet(\$iBit, \$iPos)
Return BitAND(BitShift(\$iBit, \$iPos), 1)
EndFunc```

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

Edited by seadoggie01

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

##### Share on other sites

Thanks @Dan_555, yes, i was needed to have this function, but in fact i didn't know how to rise to this section to continue.

I say thanks again :)❤

##### Share on other sites

1 minute ago, seadoggie01 said:

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

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

2 minutes ago, seadoggie01 said:

so Permutate

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.

4 minutes ago, seadoggie01 said:

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.

##### Share on other sites

2 minutes ago, Colduction said:

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

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

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

##### Share on other sites

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

"Any fool can know.  The point is to understand."  -Albert Einstein
"If you think you're a big fish, it's probably because you only swim in small ponds."  ~TheXman

##### Share on other sites

1 minute ago, 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```

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

##### Share on other sites

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

You're welcome.

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

"Any fool can know.  The point is to understand."  -Albert Einstein
"If you think you're a big fish, it's probably because you only swim in small ponds."  ~TheXman

## 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
• ### Recently Browsing   0 members

• No registered users viewing this page.
• ### Similar Content

• #### String Trigger

By careca,

• 7 comments
• 7,077 views
• #### Getting all permutations of Nested Spintax?

By noellarkin,

• 0 replies
• 243 views
• #### How to convert String to Array without any delimiters?

By LWC,

• 6 replies
• 555 views
• #### Strange behaviour using GUICtrlCreateIcon

By Jemboy,

• 11 replies
• 1,574 views
• #### [SOLVED] combine Arrays respectively

By Colduction,

• 19 replies
• 2,813 views
×

• Wiki

• Back

• #### Beta

• Git
• FAQ
×
• Create New...