class Syntax: def match(self, input_1): match= {'[':']','{':'}','(':')'} begin = ['[','{','(',')',']','}'] tmp = [] front = 0 end = 0 for i in input_1: if i in begin: if i in begin[:3]: tmp += i front +=1 else: end += 1 if len(tmp) == 0 or i != match[tmp[-1]]: return False else: tmp = tmp[:-1] if front != end: return False return True """ Problem Statement Create a class called Syntax. Given a String that contains, among other characters, parentheses '()', square brackets '[ ]' and curly braces '{}', create a solution that will determine if the string is balanced. The String is considered balanced if all of the parentheses, brackets, and braces are nested, opened, and closed properly. The rules are as follows: There must be equal numbers of beginning and ending delimiters for each type. Any time a closing delimiter occurs, it must match the most recent unmatched open delimiter. An ending delimiter can never occur without a cooresponding begining delimiter of the same type. EXAMPLES: x(y(z{test})abc) - should return true () - should return true ([)] - should return false [[]](()){{{}}} - should return true abc(def(ghi) - should return false Here is the method signature: boolean match(String input) We will check to make sure the input is valid. Definition Class: Syntax Method: match Parameters: String Returns: boolean Method signature: boolean match(String param0) (be sure your method is public) """
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question