Balanced brackets: Difference between revisions

Content added Content deleted
m (→‎Imperative Version: Fix link: Perl 6 --> Raku)
(→‎{{header|Batch File}}: Improved code and new example)
Line 1,147: Line 1,147:


=={{header|Batch File}}==
=={{header|Batch File}}==
Uses a Markov algorithm/code <code>"[]" -> null</code> to check if brackets are balanced.
Uses the rewrite rule <code>"[]" -> null</code> to check if brackets are balanced.
<lang dos>:: Balanced Brackets Task from Rosetta Code Wiki
<lang dos>:: Balanced Brackets Task from Rosetta Code
:: Batch File Implementation
:: Batch File Implementation
Line 1,154: Line 1,154:
setlocal enabledelayedexpansion
setlocal enabledelayedexpansion


set "num_pairs=10"
::The Main Thing...
set numofpairs=10
set "num_strings=10"

set howmanystrings=10
:: the main thing
cls
for /l %%. in (1,1,%howmanystrings%) do (
for /l %%s in (1, 1, %num_strings%) do (
call :generate
call :generate
call :checkforbalance
call :check
)
)
echo.&pause&exit /b
echo(
pause
::/The Main Thing.
exit /b 0


::Generate strings of brackets...
:: generate strings of brackets
:generate
: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 %%c in (1, 1, %num_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! %% (!ctr! + 1)"
)
for %%x in (!rnd!) 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 "ctr+=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
goto :EOF
::/Generate strings of brackets.


::Check for Balance...
:: check for balance
:check
::Uses Markov Algorithm.
set "new=%string%"
:checkforbalance
set "changes=!samp!"
:check_loop
:check_loop
if "!changes!"=="" goto itsbal
if "%new%" equ "" (
echo(
if "!input!"=="!changes!" goto notbal
echo(%string% is Balanced.

goto :EOF
set input=!changes!
) else if "%old%" equ "%new%" ( %== unchangeable already? ==%
set "changes=!input:[]=!"
echo(
goto check_loop
echo(%string% is NOT Balanced.
goto :EOF
:itsbal
)
echo.
rem apply rewrite rule "[]" -> null
echo %samp% is Balanced.
set "old=%new%"
goto :EOF
set "new=%old:[]=%"
:notbal
goto check_loop</lang>
echo.
echo %samp% is NOT Balanced.
goto :EOF
::/Check for Balance.</lang>
{{out}}
{{out}}
<pre>[][][[[[]]][]]]][[][ is NOT Balanced.
<pre>
[][][[]][][]]][][][[ is NOT Balanced.


[[[[[]][[]][]][][]]] is Balanced.
][[]][][][[]]][[[[]] is NOT Balanced.


[[[[[]]]]][][][]][][ is NOT Balanced.
[[[][]][][]][[][][]] is Balanced.


][[[[[[[]]][]]][][]] is NOT Balanced.
][[[[[]]][][][][][]] is NOT Balanced.


[][[[[]]]][[[[]]]][] is Balanced.
]][][[][]]]][]][[[[[ is NOT Balanced.


[[[][[[]][][]]]][][] is Balanced.
[[[][[][][[]]]][][]] is Balanced.


[[][[]][[]]]]][][[[] is NOT Balanced.
][]]][[]][[][[][[]][ is NOT Balanced.


[][][]][][]]][][][[[ is NOT Balanced.
[[[][[][]][[[]]][]]] is Balanced.


[[]][][][[][[[]]]][] is Balanced.
]][]]][[[][]][[][[[] is NOT Balanced.


[[[[[][[][[]]]]][]]] is Balanced.
][[]][][[]][][]][[][ is NOT Balanced.


Press any key to continue . . .</pre>
Press any key to continue . . .</pre>