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-28 06:45
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Symmetric output bonus question (challenging) View 4,185 Replies 64
Floor 61 Posted 2008-07-14 10:48 ·  中国 江苏 苏州 常熟市 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Originally posted by zw19750516 at 2008-7-14 10:39 AM:
The efficiency problem lies here: echo %input%|findstr >nul||set str=#%str:~1,-1%%#
In batch processing, try to use if for judgment as much as possible and use findstr less.


As mentioned earlier, this is for compatibility. What I'm most proud of is this sentence. And in fact, there is only a judgment of 2 letters here. No matter how long your CODE is, it's still 2 letters.

High efficiency and conciseness should be based on accuracy, right?

Compared with the code on floor 38, it runs much faster, and I admit it because it gives the answer almost instantly. But as the author said, it can't handle repeated letters.

My code on floor 57 needs to wait a bit. I want to improve it but due to the level problem, I can't improve it anymore.
Also, who can tell me what's wrong with the second paragraph of the code on floor 57? Thanks


[ Last edited by quya on 2008-7-14 at 11:20 AM ]
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 62 Posted 2008-07-14 13:00 ·  中国 江苏 苏州 常熟市 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Originally posted by zw19750516 at 2008-7-14 08:38 AM:
Old-fashioned, the first consideration in code is efficiency, and the second is conciseness. Your solution has an efficiency issue, and ('echo %str%') can be modified to ("%str%").


After considering it, I have to face the problem of low efficiency, so the modification is as follows:


@echo off
set/p input=Please enter:
set string=0123456789abcdefghij

:pro1
set str=%input:~0,1%
set input=%input:~1%
call set string=%%string:%str%=#%%
if defined input goto pro1
set "str= "

:pro2
set str=%string:~-1%%str%%string:~0,1%
if %string:~0,1%==# set str=#%str:~1,-1%%#
if %string:~-1%==# set str=#%str:~1,-1%%#
set string=%string:~1,-1%
if not defined string goto end
goto pro2

:end
for /f "tokens=1,2 delims= " %%i in ("%str%") do echo %%j ^| %%i
pause


Now the speed is also very fast.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bat-zw +6 2008-07-14 16:06
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 63 Posted 2008-07-14 16:08 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
There is another problem to point out:
The if judgment statement had better add quotes. if "%string:~0,1%"=="#" or if "%string:~0,1%" equ "#"
批处理之家新域名:www.bathome.net
Floor 64 Posted 2008-07-14 18:34 ·  中国 山西 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
Simplify further, making the idea clearer


@echo off
set/p input=Please enter:
set tab=012t456789abcd6fghi3

:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input goto pro1

set "out= "
:pro2
set out=#%out%#
if not "%tab:~0,1%"=="#" if not "%tab:~-1%"=="#" set out=%tab:~-1%%out:~1,-1%%tab:~0,1%
set tab=%tab:~1,-1%
if defined tab goto pro2

for /f "tokens=1,2 delims= " %%i in ("%out%") do echo %%j ^| %%i
pause
Floor 65 Posted 2008-07-14 23:58 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by qzwqzw at 2008-7-14 18:34:
Simplify it further, the idea is clearer

@echo off
set/p input=Please enter:
set tab=012t456789abcd6fghi3
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input ...
How to display the input changed to #
I'll make a change

@echo off
setlocal enabledelayedexpansion
set/p input=Please enter:
set tab=012t456789abcd6fghi3
set str=%tab%
set /a n-=1,m=0
:pro1
call set tab=%%tab:%input:~0,1%=#%%
set input=%input:~1%
if defined input goto pro1
:pro2
set/a n+=1,m-=1
if "!tab:~,1!"=="#" (set var1=!var1!!str:~%n%,1!&set var2=!str:~%m%,1!!var2!)else (
if "!tab:~-1!"=="#" (set var1=!var1!!str:~%n%,1!&set var2=!str:~%m%,1!!var2!)else set var1=%var1%#&set var2=#%var2%)
set tab=%tab:~1,-1%
if defined tab goto pro2
echo %var1% ^| %var2%
pause
简单!简单!再简单!
‹ Prev 1 3 4 5
Forum Jump: