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

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

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
Posted (edited)

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

`~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)`

##### 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```

`~Dan~ :Emu.Zone now with Desktop Pets / DeskMates - (Bugfixed UniScrMate 4.1)`

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

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

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

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

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

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

You're welcome.

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

• ### Similar Content

• Hi dear friends!, i'm sorry for creating a new thread (a new problem), i have over than 9 lists that i want to combine them to be this (in this example, there are 3 test files):

I've written a little code for splitting main information, but i really confused how to make results as "Output.txt", here is that code:

\$sRegex_1 = StringRegExp(FileRead("1.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) \$sRegex_2 = StringRegExp(FileRead("2.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) \$sRegex_3 = StringRegExp(FileRead("3.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) For \$i = 0 To UBound(\$sRegex_1) - 1 ConsoleWrite(\$sRegex_1[\$i] & @CRLF) For \$j = 0 To UBound(\$sRegex_2) - 1 ConsoleWrite(\$sRegex_2[\$j] & @CRLF) For \$k = 0 To UBound(\$sRegex_3) - 1 ConsoleWrite(\$sRegex_3[\$k] & @CRLF) Next Next Next

• Hi,
I have a string like this :
Global \$Msga = "urrent directory is /send.  (Submission of file with log number 29381077284 is confirmed)";
I want to extract the number 29381077284  from the string. I did StringSplit to split based on "(" and then use space to reach there, But it's not a good choice.
Can anyone help me with regular expression to find the number from String using AutoIT. TIA
• By DannyJ
I have this example string:

2020-06-08 09:23:33 : abcdefghifjklm How to trim just the right part? Or how to trim this text right to left?
I want to trim this string right to left until ' : '.
In ohter words: I need the 'abcdefghifklm' text.
I have tried StringTrimRight, but that requires
The number of characters to trim. Unfortunately StringSplit trims the left part of the strings.

• By careca
This is my take on string triggers, triggers on specific strings.
Able to simple text pasting,
opening links (as long as there's a www. http:\\ or https:\\ at the beggining)
and is able to open applications, if there is a parameter in the parameter field, it uses it.
Shows your external, lan, and gateway ip's.
Able to change system volume by a set percentage, reading from the inputbox the number the user sets, if 0 or empty uses system default.
I made this because the existing string trigger applications didn't do it for me.
I did this for me, but if someone finds it useful, all the better.
• By jmp
i am trying to get number from string using this code :
#include <IE.au3> \$oIE = _IEAttach ("Edu.corner") Local \$aName = "Student name & Code:", \$iaName = "0" Local \$oTds = _IETagNameGetCollection(\$oIE, "td") For \$oTd In \$oTds If \$oTd.InnerText = \$aName Then \$iaName = \$oTd.NextElementSibling.InnerText \$iGet = StringRegExpReplace(\$iaName, "\D", "") EndIf Next MsgBox(0, "", \$iGet) it was get number like 52503058
But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

×

• Wiki

• Back

• #### Beta

• Git
• FAQ
• Our Picks
×
• Create New...