China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-19 23:51
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Permutations and Combinations View 6,106 Replies 36
Floor 31 Posted 2008-12-21 04:21 ·  中国 重庆 联通
中级用户
★★
Credits 363
Posts 162
Joined 2007-04-22 21:31
19-year member
UID 86104
Gender Male
Status Offline
Thanks to so many experts, it's perfect!
Floor 32 Posted 2008-12-21 04:36 ·  中国 重庆 联通
中级用户
★★
Credits 363
Posts 162
Joined 2007-04-22 21:31
19-year member
UID 86104
Gender Male
Status Offline
But now there is a new problem, such as repeated input characters, for example: 13543. Using the code from the previous post can perfectly get all the shuffled combinations. But if there are repeated characters among these 5 characters, then there will also be completely identical multiple strings in the output results.

For example, 13543 will generate the following results, and each combination appears twice. Is there any way to make it not generate redundant identical strings when generating?

----------------------------------list.x----------------------------------------------
13543
13534
13453
13435
13354
13345
15343
............
13345
33541
33415
33451
35134
35143
35314
35341
35413
35431
34135
34153
34315
34351
34513
34531
-----------------------------------------------end------------------------------------------

[ Last edited by clian76 on 2008-12-21 at 04:37 ]
Floor 33 Posted 2008-12-21 11:34 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Can you first delete the duplicate characters in the input string and then perform permutation and combination?
Floor 34 Posted 2008-12-21 17:53 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
No temporary file

@echo off&setlocal enabledelayedexpansion
set/p str=Please enter characters:
:lp
if defined str (
set num=!str:~,1!
if not defined ..!num! (
set /a n+=1
set ..!num!=b
set "_!n!=!str:~,1!"&set "str=!str:~1!"
goto lp
) else (
set "str=!str:~1!"
goto lp
))
call:next
echo %mn% combinations
pause&exit
:next
set/a m+=1
if %m% leq %n% for /l %%i in (1 1 %n%) do (
for %%j in (%~1) do set .%%j=%%j
if not defined .%%i call :next "%~1 %%i"
for /l %%k in (1 1 %n%) do set .%%k=
) else (
for /l %%l in (1 1 %n%) do set .%%l=
for %%m in (%~1) do set/p=!_%%m!<nul
echo.
set/a mn+=1
)
set/a m-=1


Simplified on Brother batman's code, there are temporary files

@echo off&setlocal enabledelayedexpansion
set/p str=Please enter characters:
:lp
set num=!str:~,1!
if not defined .!num! (
set str1=!str1!!num!
set /a n+=1
set .!num!=b
set "_!n!=!str:~,1!"&set "str=!str:~1!"
if defined str goto lp
)else (
set "str=!str:~1!"
if defined str goto lp
)
set/a n-=1
>b.bat echo @echo off^&setlocal enabledelayedexpansion
>>b.bat echo set "str=%str1%"
>>b.bat echo for /l %%%%i in (0 1 %n%) do set _%%%%i=^^!str:~%%%%i,1^^!^&set var0=^^!var0^^! %%%%i
for /l %%i in (0 1 %n%) do (
set/a m+=1
if !m! gtr %n% (
>>b.bat echo for %%%%%%i in (^^!var%%i^^!^) do echo !var!^^!_%%%%%%i^^!)else (
>>b.bat echo for %%%%%%i in (^^!var%%i^^!^) do ^(set "var!m!=^!var%%i:%%%%%%i=^!"
set "var=!var!^!_%%%%%%i^!"
set "k=!k!^)"
))
>>b.bat echo set/a t+=1
>>b.bat echo %k%
>>b.bat echo echo %%t%% combinations
call "b.bat"
del "b.bat"
pause>nul


[ Last edited by terse on 2008-12-24 at 03:12 ]
简单!简单!再简单!
Floor 35 Posted 2008-12-23 13:49 ·  中国 重庆 联通
中级用户
★★
Credits 363
Posts 162
Joined 2007-04-22 21:31
19-year member
UID 86104
Gender Male
Status Offline
I tested the code from the previous post, but it didn't achieve the desired requirement. The code from the previous post only tells how many combinations there are. But it didn't exclude the existing duplicate combinations.

My approach is to use FINDSTR to search for itself again after generating a file to filter out duplicate strings. It's just a bit cumbersome. I hope experts can come up with a simple one.

Finally, thank the experts again! They're all amazing!
Floor 36 Posted 2008-12-24 03:14 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by clian76 at 2008-12-23 13:49:
Tested the code above, but it didn't meet the desired requirement. The code above only tells how many combinations there are. But it doesn't exclude the existing duplicate combinations.
My method is to generate a...


Revise it again
Delete the duplicate characters in the input string and then combine them. I wonder if it meets the requirement?
简单!简单!再简单!
Floor 37 Posted 2009-12-13 10:23 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Modified

```batch
@echo off&setlocal enabledelayedexpansion
call :func "0" "" "abcdef"
pause &exit
:func
set str=%~3
if "%str:~1%"=="" (set /a na=%~1+1
echo,%~2%str%
goto :eof)
set na=0
:loop
set /a nb=na+1
call :func "%na%" "%~2!str:~%na%,1!" "!str:~0,%na%!!str:~%nb%!"
set str=%~3
if not "!str:~%na%!"=="" goto :loop
set /a na=%~1+1

```
Forum Jump: