 StringTools - Maple Programming Help

Home : Support : Online Help : Programming : Names and Strings : StringTools Package : English Text : StringTools/IsBalanced

StringTools

 IsBalanced
 determine if fences in a string are balanced

 Calling Sequence IsBalanced( s, left, right )

Parameters

 s - string; string to test left - character; left fence character right - character; right fence character

Description

 • The IsBalanced(s,left,right) command checks whether the string s is balanced with respect to fence characters left and right. The characters left and right must be distinct, but are otherwise unrestricted.
 • Typical fence characters include, but are not limited to, braces ($"\left\{"$ and $"\right\}"$), parentheses ($"\left("$ and $"\right)"$), brackets ($"\left["$ and $"\right]"$) and scare quotes ($""$ and $"\text{'}"$).
 • The string s is deemed balanced with respect to the given fence characters if each open fence in left is matched by a subsequent, corresponding close fence in right within s. Note that it is not sufficient that corresponding fences be equal in number in s.
 • If s is the empty string, or left and right are empty strings, the command returns true.
 • All of the StringTools package commands treat strings as (null-terminated) sequences of $8$-bit (ASCII) characters.  Thus, there is no support for multibyte character encodings, such as unicode encodings.

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{IsBalanced}\left("f\left(x\right)","\left(","\right)"\right)$
 ${\mathrm{true}}$ (1)
 > $\mathrm{IsBalanced}\left("f\left(x,g\left(s,t\right)\right)","\left(","\right)"\right)$
 ${\mathrm{true}}$ (2)
 > $Y≔"\left(define y \left(lambda \left(f\right) \left(let \left(\left(g \left(lambda \left(h\right) \left(lambda \left(x\right) \left(\left(f \left(h h\right)\right) x\right)\right)\right)\right)\right) \left(g g\right)\right)\right)\right)":$
 > $\mathrm{IsBalanced}\left(Y,"\left(","\right)"\right)$
 ${\mathrm{true}}$ (3)
 > $\mathrm{IsBalanced}\left("\left(\left(call/cc call/cc\right) \left(call/cc call/cc\right)\right)","\left(","\right)"\right)$
 ${\mathrm{true}}$ (4)
 > $s≔\mathrm{sprintf}\left("%a",\mathrm{eval}\left(\mathrm{copy},1\right)\right)$
 ${s}{≔}{"proc \left(A\right) local X, str; option Copyright \left(c\right) 1990 by the University of Waterloo. All rights reserved.; if 1 < _npassed and _passed\left[2\right] = \left(\text{'}:-deep\text{'}\right) then if type\left(A,\text{'}\left\{module, table, procedure, moduledefinition\right\}\text{'}\right) then op\left(1,sscanf\left(sprintf\left("%m",eval\left(A\right)\right),"%m"\right)\right) else op\left(1,sscanf\left(sprintf\left("%m",A\right),"%m"\right)\right) end if elif type\left(A,\text{'}rtable\text{'}\right) then rtable\left(rtable_indfns\left(A\right),rtable_dims\left(A\right),A,rtable_options\left(A\right),\left(\text{'}:-readonly\text{'}\right) = false\right) elif type\left(A,\text{'}table\text{'}\right) then if type\left(A,\text{'}array\text{'}\right) then array\left(A\right) elif type\left(A,\text{'}cache\text{'}\right) then Cache\left(A\right) else table\left(A\right) end if elif type\left(A,\text{'}procedure\text{'}\right) then subs\left(X = X,eval\left(A\right)\right) elif type\left(A,\text{'}\left\{module`, moduledefinition\right\}\text{'}\right) then if type\left(A,\text{'}record\text{'}\right) then Record\left(eval\left(A\right)\right) elif type\left(A,\text{'}object\text{'}\right) then Object\left(A\right) else error "cannot copy a module or module definition" end if else A end if end proc"}$ (5)
 > $\mathrm{IsBalanced}\left(s,"\left(","\right)"\right)$
 ${\mathrm{true}}$ (6)
 > $\mathrm{seq}\left(\mathrm{sin}\left(\frac{\mathrm{\pi }i}{3.0}\right),i=1..5\right):$
 > $s≔\mathrm{sprintf}\left("%a",\mathrm{op}\left(4,\mathrm{eval}\left(\mathrm{sin},1\right)\right)\right)$
 ${s}{≔}{"Cache\left(512,\left(\text{'}permanent\text{'}\right) = \left[0 = 0, I = I*sinh\left(1\right), -infinity = undefined, 1/4*Pi = 1/2*2^\left(1/2\right), 1/2*Pi = 1, Pi = 0, 1/6*Pi = 1/2, 1/3*Pi = 1/2*3^\left(1/2\right), infinity = undefined\right]\right)"}$ (7)
 > $\mathrm{IsBalanced}\left(s,"\left(\left[","\right)\right]"\right)$
 ${\mathrm{true}}$ (8)
 > $\mathrm{IsBalanced}\left(\mathrm{sprintf}\left("%a",\mathrm{eval}\left(\mathrm{sin},1\right)\right),"\left[\left\{\left(","\right]\right\}\right)"\right)$
 ${\mathrm{true}}$ (9)
 > $\mathrm{IsBalanced}\left("\newtheorem\left\{thm\right\}\left[lemma\right]\left\{Theorem\right\}","\left[\left\{","\right]\right\}"\right)$
 ${\mathrm{true}}$ (10)
 > $\mathrm{IsBalanced}\left("\newtheorem\left\{thm\right\}\left[lemma\right]\left\{Theorem\right\}","\left[\left\{","\right\}\right]"\right)$
 ${\mathrm{false}}$ (11)
 > $\mathrm{IsBalanced}\left("\left[itex\right]2ab\left[/itex\right]","<",">"\right)$
 ${\mathrm{true}}$ (12)
 > $\mathrm{mml}≔\mathrm{MathML}:-\mathrm{Export}\left({\left(\mathrm{sin}\left(\mathrm{\alpha }\right)+\mathrm{cos}\left(\mathrm{\beta }\right)-\mathrm{\pi }\right)}^{10}\right):$
 > $\mathrm{IsBalanced}\left(\mathrm{mml},"&<",";>"\right)$
 ${\mathrm{true}}$ (13)
 > $\mathrm{IsBalanced}\left("ab\left[cd\right]ef","\left[","\right]"\right)$
 ${\mathrm{true}}$ (14)
 > $\mathrm{IsBalanced}\left("ab\right]cd\left[ef","\left[","\right]"\right)$
 ${\mathrm{false}}$ (15)
 > $\mathrm{IsBalanced}\left("ab\right]cd\left[ef","\right]","\left["\right)$
 ${\mathrm{true}}$ (16)
 > $\mathrm{IsBalanced}\left("a\left[b\left[cd\right]ef","\left[","\right]"\right)$
 ${\mathrm{false}}$ (17)
 > $\mathrm{IsBalanced}\left("a\left[b\left[cd\right]\right]\right]\right]\right]ef","\left[","\right]"\right)$
 ${\mathrm{false}}$ (18)
 > $\mathrm{IsBalanced}\left("a\left[\left[\left[b","\left(","\right)"\right)$
 ${\mathrm{true}}$ (19)
 > $\mathrm{IsBalanced}\left("abcde","a","e"\right)$
 ${\mathrm{true}}$ (20)