Permutations: Difference between revisions

(→‎{{header|AutoHotkey}}: alternate version)
Line 454:
===Alternate Version===
Alternate version to produce numerical permutations of combinations.
<lang ahk>P(n,k="",opt=0,delim="`n",str="") { ; generate all n choose k permutations lexicographically
;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,delim"`n")
If n isin Digit2,3,4,5,6,7,8,9
Loop, %n%
n := A_Index = 1 ? A_Index : n . delim ."`n" A_Index
Else
Loop, Parse, n, %delim%
n := A_Index = 1 ? A_LoopField : n . delim ."`n" A_LoopField
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, %delim%`n
If (!InStr(str,A_LoopField) || opt & 1)
s .= (!i++ ? (opt & 2 ? str . delim"`n" : "") : delim"`n" )
. P(n,k-1,opt,delim,str . A_LoopField . delim)
Return s
}</lang>
{{out}}
<lang ahk>MsgBox % P(43)</lang>
<pre style="height:40ex;overflow:scroll">---------------------------
permute.ahk
AutoHotkey.ahkl
---------------------------
123
1234
132
1243
213
1324
231
1342
312
1423
321
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321
---------------------------
OK
---------------------------</pre>
<lang ahk>MsgBox % P("Hello",3,1)</lang>
<pre style="height:40ex;overflow:scroll">---------------------------
permute.ahk
AutoHotkey.ahkl
---------------------------
HHH
HHe
HHl
HHl
HHo
HeH
Hee
Hel
Hel
Heo
HlH
Hle
Hll
Hll
Hlo
HlH
Hle
Hll
Hll
Hlo
HoH
Hoe
Hol
Hol
Hoo
eHH
eHe
eHl
eHl
eHo
eeH
eee
eel
eel
eeo
elH
ele
ell
ell
elo
elH
ele
ell
ell
elo
eoH
eoe
eol
eol
eoo
lHH
lHe
lHl
lHl
lHo
leH
lee
lel
lel
leo
llH
lle
lll
lll
llo
llH
lle
lll
lll
llo
loH
loe
lol
lol
loo
lHH
lHe
lHl
lHl
lHo
leH
lee
lel
lel
leo
llH
lle
lll
lll
llo
llH
lle
lll
lll
llo
loH
loe
lol
lol
loo
oHH
oHe
oHl
oHl
oHo
oeH
oee
oel
oel
oeo
olH
ole
oll
oll
olo
olH
ole
oll
oll
olo
ooH
ooe
ool
ool
ooo
---------------------------
OK
---------------------------</pre>
<lang ahk>MsgBox % P("2,3,4,5`n3`n4`n5",32,3,",")</lang>
<pre style="height:40ex;overflow:scroll">---------------------------
permute.ahk
AutoHotkey.ahkl
---------------------------
 
2,22,222,223,224,225,23,232,233,234,235,24,242,243,244,245,25,252,253,254,255,3,32,322,323,324,325,33,332,333,334,335,34,342,343,344,345,35,352,353,354,355,4,42,422,423,424,425,43,432,433,434,435,44,442,443,444,445,45,452,453,454,455,5,52,522,523,524,525,53,532,533,534,535,54,542,543,544,545,55,552,553,554,555
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
Anonymous user