Anonymous user
Permutations: Difference between revisions
→AutoHotkey#Alternate Version: oops
(→{{header|AutoHotkey}}: alternate version) |
(→AutoHotkey#Alternate Version: oops) |
||
Line 454:
===Alternate Version===
Alternate version to produce numerical permutations of combinations.
<lang ahk>P(n,k="",opt=0,delim="
;1..n = range, or delimited list, or string to parse
; to process with a different min index, pass a delimited list, e.g. "0`n1`n2"
Line 465:
;returns delimited string, error message, or (if k > n) a blank string
i:=0
If !InStr(n,
If n
Loop, %n%
n := A_Index = 1 ? A_Index : n
Else
Loop, Parse, n, %delim%
n := A_Index = 1 ? A_LoopField : n
If (k = "")
RegExReplace(n,"`n","",k), k++
If k is not Digit
Return "k must be a digit."
Line 479 ⟶ 481:
Return str
Else
Loop, Parse, n,
If (!InStr(str,A_LoopField) || opt & 1)
s .= (!i++ ? (opt & 2 ? str
. P(n,k-1,opt,delim,str . A_LoopField . delim)
Return s
}</lang>
{{out}}
<lang ahk>MsgBox % P(
<pre style="height:40ex;overflow:scroll">---------------------------
permute.ahk
---------------------------
123
132
213
231
312
321
---------------------------
OK
---------------------------</pre>
<lang ahk>MsgBox % P("Hello",3
<pre style="height:40ex;overflow:scroll">---------------------------
permute.ahk
---------------------------
Hel
Hel
Heo
Hle
Hlo
Hle
Hlo
Hoe
Hol
Hol
eHl
eHl
eHo
elH
elo
elH
elo
eoH
eol
eol
lHe
lHo
leH
leo
loH
loe
lHe
lHo
leH
leo
loH
loe
oHe
oHl
oHl
oeH
oel
oel
olH
ole
olH
ole
---------------------------
OK
---------------------------</pre>
<lang ahk>MsgBox % P("2
<pre style="height:40ex;overflow:scroll">---------------------------
permute.ahk
---------------------------
2
22
23
24
25
3
32
33
34
35
4
42
43
44
45
5
52
53
54
55
---------------------------
OK
---------------------------</pre>
<lang ahk>MsgBox % P("11 a text ] u+z",3,0," ")</lang>
<pre style="height:40ex;overflow:scroll">---------------------------
permute.ahk
---------------------------
11 a text
11 a ]
11 a u+z
11 text a
11 text ]
11 text u+z
11 ] a
11 ] text
11 ] u+z
11 u+z a
11 u+z text
11 u+z ]
a 11 text
a 11 ]
a 11 u+z
a text 11
a text ]
a text u+z
a ] 11
a ] text
a ] u+z
a u+z 11
a u+z text
a u+z ]
text 11 a
text 11 ]
text 11 u+z
text a 11
text a ]
text a u+z
text ] 11
text ] a
text ] u+z
text u+z 11
text u+z a
text u+z ]
] 11 a
] 11 text
] 11 u+z
] a 11
] a text
] a u+z
] text 11
] text a
] text u+z
] u+z 11
] u+z a
] u+z text
u+z 11 a
u+z 11 text
u+z 11 ]
u+z a 11
u+z a text
u+z a ]
u+z text 11
u+z text a
u+z text ]
u+z ] 11
u+z ] a
u+z ] text
---------------------------
OK
|