Data update
This commit is contained in:
parent
61b93a2cd1
commit
5af6d93694
858 changed files with 20572 additions and 2082 deletions
|
|
@ -1,22 +1,20 @@
|
|||
USING: io formatting locals kernel math sequences unicode.case ;
|
||||
IN: balanced-brackets
|
||||
USING: combinators formatting kernel math random sequences strings ;
|
||||
IN: rosetta-code.balanced-brackets
|
||||
|
||||
:: balanced ( str -- )
|
||||
0 :> counter!
|
||||
1 :> ok!
|
||||
str
|
||||
[ dup length 0 > ]
|
||||
[ 1 cut swap
|
||||
"[" = [ counter 1 + counter! ] [ counter 1 - counter! ] if
|
||||
counter 0 < [ 0 ok! ] when
|
||||
]
|
||||
while
|
||||
drop
|
||||
ok 0 =
|
||||
[ "NO" ]
|
||||
[ counter 0 > [ "NO" ] [ "YES" ] if ]
|
||||
if
|
||||
print ;
|
||||
: balanced? ( str -- ? )
|
||||
0 swap [
|
||||
{
|
||||
{ CHAR: [ [ 1 + t ] }
|
||||
{ CHAR: ] [ 1 - dup 0 >= ] }
|
||||
[ drop t ]
|
||||
} case
|
||||
] all? swap zero? and ;
|
||||
|
||||
readln
|
||||
balanced
|
||||
: 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
|
||||
|
|
|
|||
|
|
@ -1,20 +1,22 @@
|
|||
USING: io formatting locals kernel math sequences unicode.case ;
|
||||
IN: balanced-brackets
|
||||
|
||||
: map-braces ( -- qout )
|
||||
[
|
||||
{
|
||||
{ "[" [ drop 1 ] }
|
||||
{ "]" [ drop -1 ] }
|
||||
[ drop 0 ]
|
||||
} case
|
||||
]
|
||||
;
|
||||
:: balanced ( str -- )
|
||||
0 :> counter!
|
||||
1 :> ok!
|
||||
str
|
||||
[ dup length 0 > ]
|
||||
[ 1 cut swap
|
||||
"[" = [ counter 1 + counter! ] [ counter 1 - counter! ] if
|
||||
counter 0 < [ 0 ok! ] when
|
||||
]
|
||||
while
|
||||
drop
|
||||
ok 0 =
|
||||
[ "NO" ]
|
||||
[ counter 0 > [ "NO" ] [ "YES" ] if ]
|
||||
if
|
||||
print ;
|
||||
|
||||
: balanced? ( str -- ? )
|
||||
map-braces map sum 0 =
|
||||
;
|
||||
|
||||
"[1+2*[3+4*[5+6]-3]*4-[3*[3+3]]]" balanced?
|
||||
-- Data stack:
|
||||
t
|
||||
readln
|
||||
balanced
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue