combinations - Batch - Find Out Permutation -
i new batch , know if can find out combinations of numbers in order.
in case have 49 numbers 1 - 49 , , have pick 6 numbers results.
for example:
1 2 3 4 5 6 1 2 3 4 5 7 ... 1 2 3 4 5 49 1 2 3 4 6 7 1 2 3 4 6 8
etc...
this old code:
@echo off > newfile & setlocal enabledelayedexpansion set a=44 set b=45 set c=46 set d=47 set e=48 set f=49 /l %%a in (1 1 !a!) ( /l %%b in (2 1 !b!) ( /l %%c in (3 1 !c!) ( /l %%d in (4 1 !d!) ( /l %%e in (5 1 !e!) ( /l %%f in (6 1 !f!) ( echo.%%a %%b %%c %%d %%e %%f ))))))) >> newfile goto :eof
however returns:
1 2 3 4 5 6 1 2 3 4 5 7 ... 1 2 3 4 5 49 1 2 3 4 6 6
two 6's appeared.
i don't seem able fix it, please help, much!
when post question should post efforts solve it, describe method used , problems had; otherwise may similar answers no explanations @ all, one:
edit: users dbenham , aschipfl indicated, original code have small bug: set /a i=m-1
line should placed after :nextset
label. right code:
@echo off setlocal enabledelayedexpansion set "n=%1" set "m=%2" set "line=" /l %%i in (1,1,%m%) ( set "c[%%i]=%%i" set "line=!line! ^!c[%%i]^!" ) :nextset set /a i=m-1 /l %%j in (!c[%m%]!,1,%n%) ( set "c[%m%]=%%j" echo %line% ) :nextpos set "c=!c[%i%]!" if %c% equ %n% ( set /a i-=1 if !i! equ 0 goto :eof goto nextpos ) /l %%i in (%i%,1,%m%) ( set /a c+=1,c[%%i]=c ) if !c[%m%]! gtr %n% goto nextpos goto nextset
obviously, corrected code generate larger number of results , version particularly slow... :(
the new version below use exact same code of dbenham's solution; advantage may change parameters used generate result in easy way:
@echo off setlocal enabledelayedexpansion set "n=%1" set "m=%2" set /a j=n-m, prev=0 set "for=" & set "line=" & set "endfor=" /l %%i in (1,1,%m%) ( set /a j+=1 set "for=!for! set /a start=!prev!+1 & /l %%%%i in (^!start^!,1,!j!) (" set "line=!line! %%%%i" set "endfor=!endfor!)" set "prev=%%%%i" ) rem echo !for! echo !line! %endfor% %for% echo %line% %endfor%
output example:
c:\> test.bat 6 4 1 2 3 4 1 2 3 5 1 2 3 6 1 2 4 5 1 2 4 6 1 2 5 6 1 3 4 5 1 3 4 6 1 3 5 6 1 4 5 6 2 3 4 5 2 3 4 6 2 3 5 6 2 4 5 6 3 4 5 6
to results, use: test.bat 49 6
2nd edit: faster method added
when problem solve excessive time process takes, obvious alternative use faster programming language. solution below use jscript, similar batch file programming:
@if (@codesection == @batch) @then @echo off echo start: %time% cscript //nologo //e:jscript "%~f0" > result.txt echo end: %time% goto :eof @end // jscript code section ( var a=1; <= 44; ++a ) { ( var b=a+1; b <= 45; ++b ) { ( var c=b+1; c <= 46; ++c ) { ( var d=c+1; d <= 47; ++d ) { ( var e=d+1; e <= 48; ++e ) { ( var f=e+1; f <= 49; ++f ) { wscript.echo(a,b,c,d,e,f); } } } } } }
this batch-jscript hybrid script; save .bat extension. program took little less 9 minutes in cheap-and-slow lap-top computer generate 239 mb file 13983816 lines.
Comments
Post a Comment