Balanced brackets: Difference between revisions
Content deleted Content added
SqrtNegInf (talk | contribs) m →Imperative Version: Fix link: Perl 6 --> Raku |
Simple9371 (talk | contribs) →{{header|Batch File}}: Improved code and new example |
||
Line 1,147: | Line 1,147: | ||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
Uses |
Uses the rewrite rule <code>"[]" -> null</code> to check if brackets are balanced. |
||
<lang dos>:: Balanced Brackets Task from Rosetta Code |
<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 |
set "num_strings=10" |
||
set howmanystrings=10 |
|||
:: the main thing |
|||
cls |
|||
for /l %% |
for /l %%s in (1, 1, %num_strings%) do ( |
||
call :generate |
|||
call :check |
|||
) |
) |
||
echo |
echo( |
||
pause |
|||
::/The Main Thing. |
|||
exit /b 0 |
|||
:: |
:: 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 |
|||
⚫ | |||
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 %%c in (1, 1, %num_pairs%) do ( |
|||
set samp=!samp!%put1% |
|||
⚫ | |||
⚫ | |||
⚫ | |||
:add |
|||
set "left=!string:~0,%%x!" |
|||
if not %i%==%numofpairs% ( |
|||
set "right=!string:~%%x!" |
|||
⚫ | |||
) |
|||
set /a oppos=%j%-!rnd! |
|||
set "string=!left!]!right!" |
|||
::A new trick for substitution of delayed variables... |
|||
⚫ | |||
⚫ | |||
⚫ | |||
set str1=!samp:~-%%A! |
|||
set str2=!samp:~0,%%B! |
|||
) |
|||
set samp=!str2!%put2%!str1! |
|||
⚫ | |||
⚫ | |||
⚫ | |||
goto :EOF |
goto :EOF |
||
::/Generate strings of brackets. |
|||
:: |
:: check for balance |
||
:check |
|||
::Uses Markov Algorithm. |
|||
set "new=%string%" |
|||
:checkforbalance |
|||
set "changes=!samp!" |
|||
:check_loop |
:check_loop |
||
if " |
if "%new%" equ "" ( |
||
⚫ | |||
if "!input!"=="!changes!" goto notbal |
|||
⚫ | |||
⚫ | |||
set input=!changes! |
|||
) else if "%old%" equ "%new%" ( %== unchangeable already? ==% |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
:itsbal |
|||
⚫ | |||
⚫ | |||
rem apply rewrite rule "[]" -> null |
|||
⚫ | |||
set "old=%new%" |
|||
⚫ | |||
⚫ | |||
:notbal |
|||
⚫ | |||
⚫ | |||
⚫ | |||
goto :EOF |
|||
::/Check for Balance.</lang> |
|||
{{out}} |
{{out}} |
||
⚫ | |||
<pre> |
|||
⚫ | |||
][[]][][][[]]][[[[]] is NOT Balanced. |
|||
[[[ |
[[[][]][][]][[][][]] is Balanced. |
||
] |
][[[[[]]][][][][][]] is NOT Balanced. |
||
[][[ |
]][][[][]]]][]][[[[[ is NOT Balanced. |
||
[[[] |
[[[][[][][[]]]][][]] is Balanced. |
||
][]]][[]][[][[][[]][ is NOT Balanced. |
|||
[][][]][ |
[[[][[][]][[[]]][]]] is Balanced. |
||
]][]]][[[][]][[][[[] is NOT Balanced. |
|||
][[]][][[]][][]][[][ is NOT Balanced. |
|||
Press any key to continue . . .</pre> |
Press any key to continue . . .</pre> |