Permutations by swapping: Difference between revisions
m
no edit summary
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Alpha bravo (talk | contribs) mNo edit summary |
||
Line 18:
=={{header|AutoHotkey}}==
<lang AutoHotkey>Permutations_By_Swapping(str, list:=""){
ch := SubStr(str, 1, 1) ; get left-most charachter of str
for
loop % StrLen(line) + 1 ; loop each possible position
Newlist .= RegExReplace(line, mod(
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")
Line 31:
MsgBox, 262144, , % result
return</lang>
Outputs:<pre>
1243 Sign: -1▼
4123 Sign: -1▼
4132 Sign: 1▼
1432 Sign: -1▼
1342 Sign: 1▼
1324 Sign: -1▼
3142 Sign: -1▼
4312 Sign: -1▼
3421 Sign: -1
3241 Sign: 1
3214 Sign: -1
4213 Sign: 1
2413 Sign: -1
2143 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}}==
|