new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
27
Task/Balanced_brackets/Python/balanced_brackets.py
Normal file
27
Task/Balanced_brackets/Python/balanced_brackets.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
>>> def gen(N):
|
||||
... txt = ['[', ']'] * N
|
||||
... random.shuffle( txt )
|
||||
... return ''.join(txt)
|
||||
...
|
||||
>>> def balanced(txt):
|
||||
... braced = 0
|
||||
... for ch in txt:
|
||||
... if ch == '[': braced += 1
|
||||
... if ch == ']':
|
||||
... braced -= 1
|
||||
... if braced < 0: return False
|
||||
... return braced == 0
|
||||
...
|
||||
>>> for txt in (gen(N) for N in range(10)):
|
||||
... print ("%-22r is%s balanced" % (txt, '' if balanced(txt) else ' not'))
|
||||
...
|
||||
'' is balanced
|
||||
'[]' is balanced
|
||||
'[][]' is balanced
|
||||
'][[[]]' is not balanced
|
||||
'[]][[][]' is not balanced
|
||||
'[][[][]]][' is not balanced
|
||||
'][]][][[]][[' is not balanced
|
||||
'[[]]]]][]][[[[' is not balanced
|
||||
'[[[[]][]]][[][]]' is balanced
|
||||
'][[][[]]][]]][[[[]' is not balanced
|
||||
Loading…
Add table
Add a link
Reference in a new issue