Permutations by swapping: Difference between revisions
Content added Content deleted
Alpha bravo (talk | contribs) (Added AutoHotkey) |
Alpha bravo (talk | contribs) 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 |
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( |
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> |
Outputs:<pre>1234 Sign: 1 |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
3421 Sign: -1 |
3421 Sign: -1 |
||
3241 Sign: 1 |
3241 Sign: 1 |
||
3214 Sign: -1 |
3214 Sign: -1 |
||
2314 Sign: 1 |
|||
2341 Sign: -1 |
|||
2431 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> |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |