String | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

String

A string with parentheses is well bracketed if all parentheses are matched: every opening bracket has a matching closing bracket and vice versa. Write a Python function wellbracketed(s) that takes a string s containing parentheses and returns True if s is well bracketed and False otherwise. Hint: Keep track of the nesting depth of brackets. Initially the depth is 0. The depth increases with each opening bracket and decreases with each closing bracket. What are the constraints on the value of the nesting

24th Feb 2018, 3:10 PM
SUNIL PATIL
SUNIL PATIL - avatar
3 Answers
+ 3
@lunarCoffee but in your solution ' )()( ' this will return true
24th Feb 2018, 3:21 PM
code learner
code learner - avatar
+ 1
well_bracketed = lambda s: True if s.count("(") == s.count(")") else False should [EDIT:not] work fine
24th Feb 2018, 3:17 PM
LunarCoffee
LunarCoffee - avatar
+ 1
I need to go rn, ill be back in ~1 hour
24th Feb 2018, 3:22 PM
LunarCoffee
LunarCoffee - avatar