- 1

Answer this

Write a function matched(s) that takes as input a string s and checks if the brackets "(" and ")" in s are matched: that is, every "(" has a matching ")" after it and every ")" has a matching "(" before it. Your function should ignore all other symbols that appear in s. Your function should return True if s has matched brackets and False if it does not

10th Feb 2017, 4:21 PM
Kannan M
Kannan M - avatar
2 odpowiedzi
+ 1
def matched(s): brackets_counter = 0 i = 0 while brackets_counter >= 0 and i < len(s): if s[i] == '(': brackets_counter += 1 elif s[i] == ')': brackets_counter -= 1 i += 1 if brackets_counter == 0: return True else: return False
28th Jan 2021, 3:04 PM
S Bharani
S Bharani - avatar
0
It sounds like a kind of codewarrior problem ^^
10th Feb 2017, 4:31 PM
visph
visph - avatar