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:45
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] Permutations and Combinations View 6,104 Replies 36
Floor 16 Posted 2008-06-29 19:18 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
My head is about to break, and I can only use the substitution method to calculate. The following code is theoretically suitable for permutations within 10 digits, but I don't know why it has problems when it comes to 9 digits. It's frustrating.

@echo off&setlocal enabledelayedexpansion
if exist pl.txt del /q pl.txt
:set
cls&set /p s1=Please enter the character group (please separate characters with spaces):
if not defined s1 goto set
for %%i in (%s1%) do set /a num+=1
for %%a in (%s1%) do (
set s2=!s1:%%a=!
set str=%%a
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%b in (!s2!) do (
set s3=!s2:%%b=!
set str=%%a%%b
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%c in (!s3!) do (
set s4=!s3:%%c=!
set str=%%a%%b%%c
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%d in (!s4!) do (
set s5=!s4:%%d=!
set str=%%a%%b%%c%%d
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%e in (!s5!) do (
set s6=!s5:%%e=!
set str=%%a%%b%%c%%d%%e
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%f in (!s6!) do (
set s7=!s6:%%f=!
set str=%%a%%b%%c%%d%%e%%f
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%g in (!s7!) do (
set s8=!s7:%%g=!
set str=%%a%%b%%c%%d%%e%%f%%g
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%h in (!s8!) do (
set s9=!s9:%%h=!
set str=%%a%%b%%c%%d%%e%%f%%g%%h
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%i in (!s9!) do (
set s10=!s9:%%i=!
set str=%%a%%b%%c%%d%%e%%f%%g%%h%%i
if "!str:~%num%!" neq "" echo !str!>>pl.txt
for %%j in (!s10!) do (
set str=%%a%%b%%c%%d%%e%%f%%g%%h%%i%%j
if "!str:~%num%!" neq "" echo !str!>>pl.txt
))))))))))
start pl.txt


[ Last edited by zw19750516 on 2008-6-29 at 07:28 PM ]
批处理之家新域名:www.bathome.net
Floor 17 Posted 2008-06-29 21:29 ·  中国 陕西 西安 电信
银牌会员
★★★★
钻石会员
Credits 2,278
Posts 1,020
Joined 2007-11-19 13:34
18-year member
UID 103127
Gender Male
Status Offline
Use recursion. I think the core code can be done in no more than 3 lines. Who wants to challenge?
Brother zw:
Your code generates temporary files and it's not called recursion. Your code can be classified as "unconventional"
山外有山,人外有人;低调做人,努力做事。

进入网盘(各种工具)~~ 空间~~cmd学习
Floor 18 Posted 2008-06-29 21:34 ·  中国 广东 广州 电信
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
Too much is troublesome, the main thing is that it is realized. If you want to arrange more, and so on...
@echo off
setlocal enabledelayedexpansion
:begin
cls
set "n=0"
set /p s1=Please enter up to 8 characters separated by spaces:
for %%i in (%s1%) do (set /a n+=1)
if %n% gtr 8 goto :begin
for %%a in (!s1!) do (
if "%n%"=="1" (echo %%a) else (set s2=!s1:%%a=!)
for %%b in (!s2!) do (
if "%n%"=="2" (echo %%a%%b) else (set s3=!s2:%%b=!)
for %%c in (!s3!) do (
if "%n%"=="3" (echo %%a%%b%%c) else (set s4=!s3:%%c=!)
for %%d in (!s4!) do (
if "%n%"=="4" (echo %%a%%b%%c%%d) else (set s5=!s4:%%d=!)
for %%e in (!s5!) do (
if "%n%"=="5" (echo %%a%%b%%c%%d%%e) else (set s6=!s5:%%e=!)
for %%f in (!s6!) do (
if "%n%"=="6" (echo %%a%%b%%c%%d%%e%%f) else (set s7=!s6:%%f=!)
for %%g in (!s7!) do (
if "%n%"=="7" (echo %%a%%b%%c%%d%%e%%f%%g) else (set s8=!s7:%%g=!)
for %%h in (!s8!) do (
echo %%a%%b%%c%%d%%e%%f%%g%%h
))))))))
pause&goto :begin
Floor 19 Posted 2008-06-29 21:35 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
I think if this problem is thoroughly understood, it will be of great use...
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 20 Posted 2008-06-30 00:18 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
What is the use? Generate a password dictionary for brute-force cracking?
Floor 21 Posted 2008-06-30 01:05 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
It's not just brute-force cracking... In short, it's related to decoding...
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 22 Posted 2008-06-30 14:59 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
If it's for guessing passwords, maybe it's more appropriate to change the code on floor 18 to the following:
@echo off
setlocal enabledelayedexpansion
:begin
cls
for /l %%a in (1 1 10) do (set "s%%a=")
set "n="&set "var="&set "m="&set "count="
set /p var=Please enter less than 10 characters (it doesn't matter if you use spaces or not):
:more_
set "s1=!s1!!var:~,1! "
if not "!var:~1!"=="" set var=!var:~1!&goto :more_
for %%i in (%s1%) do (set /a m+=1)
if %m% gtr 10 (echo.&echo ...Input error...&echo.&pause&goto :begin)
set /p n=Please enter the number of characters to combine (default is all):
set "t=%time%"
if "%n%"=="" set "n=%m%"
for %%a in (!s1!) do (
if "%n%"=="1" (set /a count+=1&echo %%a) else (set s2=!s1:%%a=!)
for %%b in (!s2!) do (
if "%n%"=="2" (set /a count+=1&echo %%a%%b) else (set s3=!s2:%%b=!)
for %%c in (!s3!) do (
if "%n%"=="3" (set /a count+=1&echo %%a%%b%%c) else (set s4=!s3:%%c=!)
for %%d in (!s4!) do (
if "%n%"=="4" (set /a count+=1&echo %%a%%b%%c%%d) else (set s5=!s4:%%d=!)
for %%e in (!s5!) do (
if "%n%"=="5" (set /a count+=1&echo %%a%%b%%c%%d%%e) else (set s6=!s5:%%e=!)
for %%f in (!s6!) do (
if "%n%"=="6" (set /a count+=1&echo %%a%%b%%c%%d%%e%%f) else (set s7=!s6:%%f=!)
for %%g in (!s7!) do (
if "%n%"=="7" (set /a count+=1&echo %%a%%b%%c%%d%%e%%f%%g) else (set s8=!s7:%%g=!)
for %%h in (!s8!) do (
if "%n%"=="8" (set /a count+=1&echo %%a%%b%%c%%d%%e%%f%%g%%h) else (set s9=!s8:%%h=!)
for %%i in (!s9!) do (
if "%n%"=="9" (set /a count+=1&echo %%a%%b%%c%%d%%e%%f%%g%%h%%i) else (set s10=!s9:%%i=!)
for %%j in (!s10!) do (
set /a count+=1&echo %%a%%b%%c%%d%%e%%f%%g%%h%%i%%j
))))))))))
echo.&echo There are %count% combinations&echo.
echo %t%
echo %time%
echo.&pause&goto :begin
Floor 23 Posted 2008-06-30 17:40 ·  中国 重庆 渝中区 联通
初级用户
★★
Credits 198
Posts 89
Joined 2007-09-06 23:22
18-year member
UID 96746
Gender Male
From 重庆
Status Offline
I use the "insertion method" and find that the efficiency is still pretty good, and the code is also short:
@echo off&setlocal enabledelayedexpansion
set/p chr=Please enter the letters to be arranged, separated by spaces:
for %%i in (%chr%) do set a=%%i
set - %a%=ok&set chr=!chr:%a%=!
for %%i in (%chr%) do call :out %%i
for /f "delims=-= tokens=1" %%i in ('set -') do echo %%i
pause>nul

:out
for /f "delims=-= tokens=1" %%i in ('set -') do (
set - %1%%i=ok
for %%j in (%%i) do (
for %%k in (%%i) do (set str=!str! %%k&if %%j==%%k set str=!str! %1)
set -!str!=ok&set str=
)
set -%%i=
)
Floor 24 Posted 2008-06-30 18:51 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
After testing, when entering 7 numbers, the code on floor 23 took more than thirty seconds, while that on floor 22 only took a few seconds. Let's give it a try?? I have to go to work. I'll check the result tomorrow, haha
Floor 25 Posted 2008-06-30 20:41 ·  中国 重庆 渝中区 联通
初级用户
★★
Credits 198
Posts 89
Joined 2007-09-06 23:22
18-year member
UID 96746
Gender Male
From 重庆
Status Offline
Sure, calling too many times naturally lowers efficiency. My key point is not to limit the number of characters.
Floor 26 Posted 2008-12-01 16:31 ·  中国 重庆 联通
中级用户
★★
Credits 363
Posts 162
Joined 2007-04-22 21:31
19-year member
UID 86104
Gender Male
Status Offline
If the given characters have duplicates, it's an error! For example: abbcd. Another example: 12321. Can this bug be modified and removed?
Floor 27 Posted 2008-12-17 17:19 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Article, code: 523066680
blog: http://hi.baidu.com/523066680
Posted at: http://www.cn-dos.net/forum/viewthread.php?tid=41243&fpage=1&highlight=&page=2 Floor 27

First, post the code
  
@echo off&setlocal enabledelayedexpansion
title code by hi.baidu.com/523066680
errorcommand>list.x 2>nul
echo,Allow string repetition, the number of characters is indefinite, don't try characters like spaces, etc. &echo.
set /p str="Enter the string whose permutation conditions will be enumerated (in the form like: abcd): "
call :fo "%str%" ""
echo,--------The information has been imported into the list.x file, please check.
pause
exit

:fo
if %1=="" (echo,%~2 &echo,%~2>>list.x&goto :eof)
if not defined _%~1 (set _%~1=-1)
set str=%~1

:foa
set /a _%~1+=1,foa=_%~1,fob=foa+1
call :fo "!str:~0,%foa%!!str:~%fob%!" "%~2!str:~%foa%,1!"
set str=%~1
set /a foa=_%~1
if not "!str:~%foa%,-1!"=="" (goto :foa)
set "_%~1="
goto :eof



I'm here. The blackboard newspaper is finished. I'm extremely relaxed. I saw this problem and "studied" it for a few days. I have some results and want to share them.
Main content: 1. Talk about the previous floor's code
2. Share what I learned in the process of solving the problem
First, talk about the content of the previous floor
The multiple for proposed by plp626 is the fastest. Although it can only handle a specified number of characters, it can be modified. The modification by floor 22 is good. I think it can meet the needs when there are up to 10 characters, especially the speed is as fast as directly typing the answer.
But as a scholar, it is necessary to seek a better way. So the code on floor 23 is very short (Good!), and meets the requirements. Although I didn't have time to understand it, the temporary problem found is:
When I enter 6 characters, when I press Enter at the end, it doesn't exit. I guess
Maybe there is a dead loop. Looking at it again, oh, sweat. Please add exit after pause>nul when testing.
In addition, it is natural that the running time is long when the number of input characters is large. I don't think this should be picked on.

I think the code on floor 23 is very short and concise, worthy of learning.

Next, first talk about what I learned in the process of solving the problem:
[Mainly wanting to discuss the recursive call of parameters. Regarding this problem, I saw Batman's fast solution last time,
I won't reprint it. Looking forward to Brother Batman's follow-up post
]

1. Also want to improve the plp626 code. I played with the method of using %var% to represent a command execution. The following is the understanding version:
  
@echo off
set fo="for %%1 in (a b c) do (for %%2 in (a b c) do ("
set end="))"

%fo:"=%
echo %%1 %%2
%end:"=%
pause


It feels too fun, not "automated" enough, right? OK, we use for to determine how many layers of for to use. I once tried to reorganize the idea of multiple For,
But there are too many variables and symbols, it's really impossible to do, stop working, don't waste the idea, do something else. Use this idea to realize:
List all combinations including itself of these symbols according to the symbols input by the user
This is a simpler topic. For example, if you enter 1 2, then output 1 1, 1 2, 2 1, 2 2. The advantage is mainly that the number of input characters is indefinite.
Although I don't understand "password dictionary", but it feels that this code should be more suitable (for example, when the password is 223456).
The code is as follows:
  
@echo off &setlocal enabledelayedexpansion
title code by hi.baidu.com/523066680
set "fo=" &set "end=" &set "n=0"
set /p "str=Enter characters, separated by spaces: "
for %%a in (%str%) do set /a n+=1

for /l %%x in (1,1,%n%) do (
set fo="for %%%%x in (%str%) do (!fo!"
set end="!end!)"
set echo=!echo! %%%%x
)

%fo:"=%
echo %echo%
%end:"=%
pause



2. Return of call parameters
I think this may be achievable with Call. I remember that when Call returns, the following things are all original. Will the parameters also be original?



So, don't expect the variables of set to have "overlapping memory functions", either add numbers, but it will be very complicated.

-----------------The following content talks about call parameters
Talked too much nonsense, start to explore how powerful call is (understanding version of call parameter return):
  
@echo off
call :a 123
:a
echo %1

:b
if %1==123 (call :a 456) else (goto :eof)
echo %1
pause
exit


It's still easy to understand? Here's a multi-layer one, play with call:
  
@echo off
echo,call :fo "abcd"
call :fo "abcd"
pause
exit

:fo
if %1=="" (echo,Parameter 1 has no more characters, return call, next sentence &goto :eof)
set str=%~1

:foa
echo,call :fo "%str:~1%"
call :fo "%str:~1%"

echo Returned, now the value of parameter 1 is %~1
goto :eof
::Must be very depressed, right? Why does the following echo show 4 times? The mystery is all in goto :eof because it was called multiple times itself.


It's getting more and more interesting to play. I don't plan to analyze my own code in detail. This article is not to confuse everyone, so I mainly use Chinese to describe it and talk about some things that may be useful.

Friendly reminder: Many problems have troubled me for a long time, and the last time I sorted out all the ideas was when I closed the computer. It can be seen: It's not good for thinking to face the computer often.
----------------End
My knowledge and ability are really limited. I feel that this layered call and layered return should be the "recursion" mentioned in the previous floor.
I hope those who have information can share the relevant materials. Now I feel like I have entered a completely unknown logical field....

[ Last edited by 523066680 on 2008-12-19 at 10:41 ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
pusofalse +13 2008-12-18 12:35
moniuming +15 2008-12-21 12:44
xeibobin +2 2009-09-20 06:56
Floor 28 Posted 2008-12-18 10:36 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
I worked so hard writing this, and I have to spend money to post it in an internet café. How can I not give it a thumbs up?

If no one pays attention to it, I won't bother writing in the future....

[ Last edited by 523066680 on 2008-12-18 at 11:06 ]
Floor 29 Posted 2008-12-18 11:51 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
19-year member
UID 59307
Status Offline
Sometimes it's better to leave people some room for free thinking. Ideas are only for discussion, and the simpler the better. If written too detailed, it's easy to cause a fixed mindset.

A little suggestion, please don't take offense.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
523066680 +5 2008-12-19 10:25
┌───────┐
├→学习→实践→┤
└───────┘
Floor 30 Posted 2008-12-19 10:24 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
That's very good and makes sense. I've decided to revise the article to only discuss the batch processing techniques I learned during the process.
Forum Jump: