Colduction Posted May 18, 2020 Posted May 18, 2020 (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 May 20, 2020 by Colduction
careca Posted May 18, 2020 Posted May 18, 2020 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 Au3Record.exe
Colduction Posted May 18, 2020 Author Posted May 18, 2020 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: abc Abc aBc ABc abC AbC aBC ABC
Dan_555 Posted May 18, 2020 Posted May 18, 2020 (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 May 18, 2020 by Dan_555 TheXman 1 Some of my script sourcecode
jchd Posted May 18, 2020 Posted May 18, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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)
Colduction Posted May 18, 2020 Author Posted May 18, 2020 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/
jchd Posted May 18, 2020 Posted May 18, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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)
Colduction Posted May 18, 2020 Author Posted May 18, 2020 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
Colduction Posted May 18, 2020 Author Posted May 18, 2020 1 minute ago, jchd said: permutation is this: https://en.wikipedia.org/wiki/Permutation I know! but this is a Case Permutation
jchd Posted May 18, 2020 Posted May 18, 2020 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 hereRegExp tutorial: enough to get startedPCRE 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)
Colduction Posted May 18, 2020 Author Posted May 18, 2020 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.
jchd Posted May 18, 2020 Posted May 18, 2020 Enough for me. Colduction 1 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 hereRegExp tutorial: enough to get startedPCRE 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)
Dan_555 Posted May 18, 2020 Posted May 18, 2020 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 Colduction 1 Some of my script sourcecode
seadoggie01 Posted May 18, 2020 Posted May 18, 2020 (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. expandcollapse popup_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 May 18, 2020 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 functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types
Colduction Posted May 18, 2020 Author Posted May 18, 2020 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 :)❤
Colduction Posted May 18, 2020 Author Posted May 18, 2020 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: https://www.geeksforgeeks.org/subarraysubstring-vs-subsequence-and-programs-to-generate-them/ 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.
seadoggie01 Posted May 18, 2020 Posted May 18, 2020 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 functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types
TheXman Posted May 18, 2020 Posted May 18, 2020 (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 May 18, 2020 by TheXman Added comments to make it easier to understand Colduction and careca 1 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "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
Colduction Posted May 18, 2020 Author Posted May 18, 2020 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 ❤
TheXman Posted May 18, 2020 Posted May 18, 2020 You're welcome. It was a fun little exercise with binary bits as @Dan_555 originally pointed out. Colduction 1 CryptoNG UDF: Cryptography API: Next Gen jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script About Me How To Ask Good Questions On Technical And Scientific Forums (Detailed) | How to Ask Good Technical Questions (Brief) "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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now