Permutations: Difference between revisions

→‎{{header|AutoHotkey}}: alternate version
(→‎{{header|Perl 6}}: use built-in)
(→‎{{header|AutoHotkey}}: alternate version)
Line 451:
ollHe
olleH</pre>
 
===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"
;k = length of result
;opt 0 = no repetitions
;opt 1 = with repetitions
;opt 2 = run for 1..k
;opt 3 = run for 1..k with repetitions
;str = string to prepend (used internally)
;returns delimited string, error message, or (if k > n) a blank string
i:=0
If !InStr(n,delim)
If n is Digit
Loop, %n%
n := A_Index = 1 ? A_Index : n . delim . A_Index
Else
Loop, Parse, n
n := A_Index = 1 ? A_LoopField : n . delim . A_LoopField
If k is not Digit
Return "k must be a digit."
If opt not in 0,1,2,3
Return "opt invalid."
If k = 0
Return str
Else
Loop, Parse, n, %delim%
If (!InStr(str,A_LoopField) || opt & 1)
s .= (!i++ ? (opt & 2 ? str . delim : "") : delim )
. P(n,k-1,opt,delim,str . A_LoopField)
Return s
}</lang>
{{out}}
<lang ahk>MsgBox % P(4)</lang>
<pre style="height:40ex;overflow:scroll">---------------------------
AutoHotkey.ahkl
---------------------------
1234
1243
1324
1342
1423
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">---------------------------
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",3,3,",")</lang>
<pre style="height:40ex;overflow:scroll">---------------------------
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
---------------------------
OK
---------------------------</pre>
 
=={{header|BBC BASIC}}==
Anonymous user