Name Permutation.

Get help with using AutoHotkey and its commands and hotkeys
User avatar
FredOoo
Posts: 113
Joined: 07 May 2019, 21:58
Location: Paris

Re: Name Permutation.

19 Jun 2019, 04:16

I'm stick on permutations…
Permutation class for demonstrating usage of enumerator
https://www.autohotkey.com/boards/viewtopic.php?style=7&f=7&t=65535
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Name Permutation.

19 Jun 2019, 04:47

@Nickyeo:

This was tested !!!
the trick is to use global Names0 inside the function. (see Help)

Code: Select all

; tested with AHK-Basic v1.0.48.05

^2::
NamePermutation()
Return

;Assume there is other functions
^3::
Send, FaceRecognition
Return

;Name permutation
NamePermutation()
{
    global Names0
FullName := "John Smith David James"
StringSplit, Names, FullName, %A_Space%, .  ; omit periods
Loop, % Names0
{
MsgBox, % RoundRobin(A_Index)
}

}



;-------------------------------------------------------------------------------
RoundRobin(Start) {
;-------------------------------------------------------------------------------
    Zero := 0, Result := ""
    Loop, % Count := Names%Zero%
    {
        n := (Start-1) + (A_Index-1)
        m := Mod(n, Count) + 1
        Result .= Names%m% . A_Space
    }
    return Result
}
just me
Posts: 7224
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Name Permutation.

19 Jun 2019, 06:26

This example is using a label using the global namespace to avoid confusion. I tested with AHK 1.0.48.05.

Code: Select all

;Main Name
^2::
   FullName := "John Smith David James Captain Cook"
   Gosub, NamePermutation
Return

;Assume there is other functions
^3::
   Send, FaceRecognition
Return

;Name permutation
NamePermutation:
   StringSplit, NameParts, FullName, %A_Space%, .
   Shifts := NameParts0 - 1
   Loop, %NameParts0%
   {
      RoundRobin := ""
      Loop, %NameParts0%
         RoundRobin .= (A_Index > 1 ? " " : "") . NameParts%A_Index%
      MsgBox, 0, # %A_Index%, %RoundRobin%
      I := 1
      FirstPart := NameParts%I%
      Loop, %Shifts% {
         J := I + 1
         NameParts%I% := NameParts%J%
         I += 1
      }
      NameParts%J% := FirstPart
   }
Return
User avatar
FredOoo
Posts: 113
Joined: 07 May 2019, 21:58
Location: Paris

Just insert at the end the item you removed at the beginning

19 Jun 2019, 08:58

Code: Select all

#SingleInstance force
#Persistent

Names := ["John", "Smith", "David", "James"]

loop % lenNames := Names.length(){
    print(flat( Names ))
    Names.insertAt( lenNames, Names.removeAt(1) )  ; Just insert at the end the item you removed at the beginning.
}

;==========================================================

flat( liste, sep:=", " ){
    ; Transform array to a flat line
    for index in liste
        txt .= liste[index] sep
    return rTrim( txt, sep)
}

print( text ){
    ; Who wants too many msgBox ?
    static stdOut := 0
    if !stdOut {
        dllCall("AllocConsole")
        stdOut := fileOpen("*", "w `n")
    }
    stdOut.writeLine( text )
    stdOut.read(0) ;flush
}

Code: Select all

John, Smith, David, James
Smith, David, James, John
David, James, John, Smith
James, John, Smith, David
(Alan Turing) « What would be the point of saying that A = B if it was really the same thing? »
(Albert Camus) « Misnaming things is to add to the misfortunes of the world. »

Return to “Ask For Help”

Who is online

Users browsing this forum: littlegandhi1199, RyConnMD, tatagi and 25 guests