Jump to content

Balanced brackets: Difference between revisions

→‎{{header|Batch File}}: Improved code and new example
m (→‎Imperative Version: Fix link: Perl 6 --> Raku)
(→‎{{header|Batch File}}: Improved code and new example)
Line 1,147:
 
=={{header|Batch File}}==
Uses athe Markovrewrite algorithm/coderule <code>"[]" -> null</code> to check if brackets are balanced.
<lang dos>:: Balanced Brackets Task from Rosetta Code Wiki
:: Batch File Implementation
Line 1,154:
setlocal enabledelayedexpansion
 
set "num_pairs=10"
::The Main Thing...
set numofpairs"num_strings=10"
 
set howmanystrings=10
:: the main thing
cls
for /l %%.s in (1, 1, %howmanystringsnum_strings%) do (
call :generate
call :checkforbalancecheck
)
echo.&pause&exit /b(
pause
::/The Main Thing.
exit /b 0
 
::Generate generate strings of brackets...
:generate
set "string="
set i=0&set j=%numofpairs%&set samp=
rem put %num_pairs% number of "[" in string
set /a toss=%random%%%2
for /l %%xc in (1, 1, %numofpairsnum_pairs%) do (set "string=!string!["
set put1=[&set put2=]
rem put %num_pairs% number of "]" in random spots of string
if %toss%==1 (set put1=]&set put2=[)
set "ctr=%num_pairs%"
for /l %%x in (1,1,%numofpairs%) do (
for /l %%c in (1, 1, %num_pairs%) do (
set samp=!samp!%put1%
set /a "rnd=%!random! %%%%j% (!ctr! + 1)"
)
for /f "tokens=1-2" for %%Ax in ("!rnd! !oppos!") do (
:add
set "left=!string:~0,%%x!"
if not %i%==%numofpairs% (
set "right=!string:~%%x!"
set /a rnd=%random%%%%j%+1
)
set /a oppos=%j%-!rnd!
set "string=!left!]!right!"
::A new trick for substitution of delayed variables...
set /a "j+=1","ictr+=1"
for /f "tokens=1-2" %%A in ("!rnd! !oppos!") do (
)
set str1=!samp:~-%%A!
set str2=!samp:~0,%%B!
)
set samp=!str2!%put2%!str1!
set /a "j+=1","i+=1"
goto :add
)
goto :EOF
::/Generate strings of brackets.
 
::Check check for Balance...balance
:check
::Uses Markov Algorithm.
set "new=%string%"
:checkforbalance
set "changes=!samp!"
:check_loop
if "!changes!%new%"== equ "" goto itsbal(
echo.(
if "!input!"=="!changes!" goto notbal
echo echo(%sampstring% is Balanced.
 
goto :addEOF
set input=!changes!
) else if "%old%" equ "%new%" ( %== unchangeable already? ==%
set "changes=!input:[]=!"
echo.(
goto check_loop
echo echo(%sampstring% is NOT Balanced.
goto :EOF
:itsbal
)
echo.
rem apply rewrite rule "[]" -> null
echo %samp% is Balanced.
set "old=%new%"
goto :EOF
set "changesnew=!input%old:[]=!%"
:notbal
goto check_loop</lang>
echo.
echo %samp% is NOT Balanced.
goto :EOF
::/Check for Balance.</lang>
{{out}}
<pre>[][][[[[]][][]]][][][][ is NOT Balanced.
<pre>
[][][[]][][]]][][][[ is NOT Balanced.
 
[[[][[]][][]][[]][][][[[]] is NOT Balanced.
 
[[[[][]][][]][[][][]][][ is NOT Balanced.
 
][[[[[[[]]][][][][][]] is NOT Balanced.
 
]][][[[][]]]][]][[[[]]]][] is NOT Balanced.
 
[[[][[[]][][[]]]][][]] is Balanced.
 
[[][[]]][[]]]]][[][[][[]][ is NOT Balanced.
 
[[[][[][]][][[]]][][][[[] is NOT Balanced.
 
[[]][][][[][[[][]][[][[[] is NOT Balanced.
 
[[[][[][][][[]][][]][[]]][ is NOT Balanced.
 
Press any key to continue . . .</pre>
535

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.