Permutations: Difference between revisions

Line 872:
OK
---------------------------</pre>
=={{header|Batch File}}==
Recursive permutation generator. Batch does'nt have local variables, so vars must be reset after a recursive call that modifies them. Swapping letters in a string is tricky too..
<lang Batch File>
:: Permutations in Batch
@echo off
setlocal enabledelayedexpansion
set arr=ABCD
set /a n=4
echo !arr!
call :permu %n%
goto:eof
 
:permu [%1]
set /a num=%1-1
for /L %%c in (1,1,%num%) do (
::batch doe's nt have local vars so I reset num after the call
if !num! gtr 1 call:permu !num! & set /a num=%1-1
set /a n1="%1%%2"
if !n1! neq 0 (call :swapit %1 1 ) else (call :swapit %1 %%c)
)
if !num! gtr 1 call:permu !num!
goto :eof
 
:swapit [%1] [%2]
set /a from=%1-1,to=%2-1
set temp1=!arr:~%from%,1!
set temp2=!arr:~%to%,1!
set arr=!arr:%temp1%=@!
set arr=!arr:%temp2%=%temp1%!
set arr=!arr:@=%temp2%!
echo !arr!
goto:eof
</lang>
{{out}}
<pre>
ABCD
BACD
CABD
ACBD
BCAD
CBAD
DBAC
BDAC
ADBC
DABC
BADC
ABDC
ACDB
CADB
DACB
ADCB
CDAB
DCAB
DCBA
CDBA
BDCA
DBCA
CBDA
BCDA
</pre>
 
=={{header|BBC BASIC}}==