Permutations by swapping: Difference between revisions

Added AutoHotkey
m (→‎{{header|Sidef}}: minor code fixes)
(Added AutoHotkey)
Line 15:
;Cf.:
* [[Matrix arithmetic]]
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>Permutations_By_Swapping(str, list:=""){
ch := SubStr(str, 1, 1) ; get left-most charachter of str
for each, line in StrSplit(list, "`n") ; for each line in list
loop % StrLen(line) + 1 ; loop each possible position
Newlist .= RegExReplace(line, mod(order,2) ? "(?=.{" A_Index-1 "}$)" : "^.{" A_Index-1 "}\K", ch) "`n"
list := Newlist ? Trim(Newlist, "`n") : ch ; recreate list
if !str := SubStr(str, 2) ; remove charachter from left hand side
return list ; done if str is empty
return Permutations_By_Swapping(str, list) ; else recurse
}</lang>
Examples:<lang AutoHotkey>for each, line in StrSplit(Permutations_By_Swapping(1234), "`n")
result .= line "`tSign: " (mod(A_Index,2)? 1 : -1) "`n"
MsgBox, 262144, , % result
return</lang>
Outputs:<pre>4321 Sign: 1
3421 Sign: -1
3241 Sign: 1
3214 Sign: -1
4231 Sign: 1
2431 Sign: -1
2341 Sign: 1
2314 Sign: -1
4213 Sign: 1
2413 Sign: -1
2143 Sign: 1
2134 Sign: -1
4312 Sign: 1
3412 Sign: -1
3142 Sign: 1
3124 Sign: -1
4132 Sign: 1
1432 Sign: -1
1342 Sign: 1
1324 Sign: -1
4123 Sign: 1
1423 Sign: -1
1243 Sign: 1
1234 Sign: -1</pre>
 
=={{header|BBC BASIC}}==
299

edits