Balanced brackets: Difference between revisions
Content added Content deleted
(→{{header|Factor}}: Remove an incorrect solution: it fails to check the negative balance, and doesn't implement the random string generation.) |
(→Factor: Add a full task implementation: generate random string and check if it's balanced. The solution doesn't use local variables.) |
||
Line 3,244: | Line 3,244: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<syntaxhighlight lang="factor">USING: combinators formatting kernel math random sequences strings ; |
|||
⚫ | |||
IN: rosetta-code.balanced-brackets |
|||
: balanced? ( str -- ? ) |
|||
0 swap [ |
|||
{ |
|||
{ CHAR: [ [ 1 + t ] } |
|||
{ CHAR: ] [ 1 - dup 0 >= ] } |
|||
[ drop t ] |
|||
} case |
|||
] all? swap zero? and ; |
|||
: bracket-pairs ( n -- str ) |
|||
[ "[]" ] replicate "" concat-as ; |
|||
: balanced-brackets-main ( -- ) |
|||
5 bracket-pairs randomize dup balanced? "" "not " ? |
|||
"String \"%s\" is %sbalanced.\n" printf ; |
|||
MAIN: balanced-brackets-main</syntaxhighlight> |
|||
⚫ | |||
<syntaxhighlight lang="factor">USING: io formatting locals kernel math sequences unicode.case ; |
<syntaxhighlight lang="factor">USING: io formatting locals kernel math sequences unicode.case ; |
||
IN: balanced-brackets |
IN: balanced-brackets |