Permutations by swapping: Difference between revisions

Content added Content deleted
(Added AutoHotkey)
mNo edit summary
Line 18: Line 18:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Permutations_By_Swapping(str, list:=""){
<lang AutoHotkey>Permutations_By_Swapping(str, list:=""){
ch := SubStr(str, 1, 1) ; get left-most charachter of str
ch := SubStr(str, 1, 1) ; get left-most charachter of str
for each, line in StrSplit(list, "`n") ; for each line in list
for i, line in StrSplit(list, "`n") ; for each line in list
loop % StrLen(line) + 1 ; loop each possible position
loop % StrLen(line) + 1 ; loop each possible position
Newlist .= RegExReplace(line, mod(order,2) ? "(?=.{" A_Index-1 "}$)" : "^.{" A_Index-1 "}\K", ch) "`n"
Newlist .= RegExReplace(line, mod(i,2) ? "(?=.{" A_Index-1 "}$)" : "^.{" A_Index-1 "}\K", ch) "`n"
list := Newlist ? Trim(Newlist, "`n") : ch ; recreate list
list := Newlist ? Trim(Newlist, "`n") : ch ; recreate list
if !str := SubStr(str, 2) ; remove charachter from left hand side
if !str := SubStr(str, 2) ; remove charachter from left hand side
return list ; done if str is empty
return list ; done if str is empty
return Permutations_By_Swapping(str, list) ; else recurse
return Permutations_By_Swapping(str, list) ; else recurse
}</lang>
}</lang>
Examples:<lang AutoHotkey>for each, line in StrSplit(Permutations_By_Swapping(1234), "`n")
Examples:<lang AutoHotkey>for each, line in StrSplit(Permutations_By_Swapping(1234), "`n")
Line 31: Line 31:
MsgBox, 262144, , % result
MsgBox, 262144, , % result
return</lang>
return</lang>
Outputs:<pre>4321 Sign: 1
Outputs:<pre>1234 Sign: 1
1243 Sign: -1
1423 Sign: 1
4123 Sign: -1
4132 Sign: 1
1432 Sign: -1
1342 Sign: 1
1324 Sign: -1
3124 Sign: 1
3142 Sign: -1
3412 Sign: 1
4312 Sign: -1
4321 Sign: 1
3421 Sign: -1
3421 Sign: -1
3241 Sign: 1
3241 Sign: 1
3214 Sign: -1
3214 Sign: -1
4231 Sign: 1
2314 Sign: 1
2431 Sign: -1
2341 Sign: -1
2341 Sign: 1
2431 Sign: 1
2314 Sign: -1
4231 Sign: -1
4213 Sign: 1
4213 Sign: 1
2413 Sign: -1
2413 Sign: -1
2143 Sign: 1
2143 Sign: 1
2134 Sign: -1
2134 Sign: -1</pre>
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}}==
=={{header|BBC BASIC}}==