Hi so I need help making this detect not only overlapping Parentheses but also o
ID: 3695685 • Letter: H
Question
Hi so I need help making this detect not only overlapping
Parentheses but also overlapping Braces: { and } and Brackets: [ and ] in python 3.4. def check_parentheses(s): """ Return True if the parentheses in string s match, otherwise False. """ j = 0 for c in s: if c == ')': j -= 1 if j < 0: return False elif c == '(': j += 1 return j == 0 def find_parentheses(s): """ Find and return the location of the matching parentheses pairs in s. Given a string, s, return a dictionary of start: end pairs giving the indexes of the matching parentheses in s. Suitable exceptions are raised if s contains unbalanced parentheses. """ # The indexes of the open parentheses are stored in a stack, implemented # as a list stack = [] parentheses_locs = {} for i, c in enumerate(s): if c == '(': stack.append(i) elif c == ')': try: parentheses_locs[stack.pop()] = i except IndexError: raise IndexError('Too many close parentheses at index {}' .format(i)) if stack: raise IndexError('No matching close parenthesis to open parenthesis ' 'at index {}'.format(stack.pop())) return parentheses_locs test_strings = [ 'as (adjks) sdj(ds(dfsf)) fdd(dsd(dsdss(1))dsds)ddsd', 'as (adjks) sdj(ds(dfsf) fdd(dsd(dsdss(1))dsds)ddsd', 'as (adjks) sdj(ds(dfsf)) fdd)(dsd(dsdss(1))dsds)ddsd', ] for i, s in enumerate(test_strings, start=1): print(' test string {}: {}'.format(i, s)) print('Parentheses match?', check_parentheses(s)) try: parentheses_locs = find_parentheses(s) print('Parentheses locations: {}'.format(str( sorted([(k,v) for k, v in parentheses_locs.items()]) ))) except IndexError as e: print(str(e))
Explanation / Answer
ef check_parentheses(s): """ Return True if the parentheses in string s match, otherwise False. """ j = 0 for c in s: if c == ')': j -= 1 if j < 0: return False elif c == '(': j += 1 return j == 0 def find_parentheses(s): """ Find and return the location of the matching parentheses pairs in s. Given a string, s, return a dictionary of start: end pairs giving the indexes of the matching parentheses in s. Suitable exceptions are raised if s contains unbalanced parentheses. """ # The indexes of the open parentheses are stored in a stack, implemented # as a list stack = [] parentheses_locs = {} for i, c in enumerate(s): if c == '(': stack.append(i) elif c == ')': try: parentheses_locs[stack.pop()] = i except IndexError: raise IndexError('Too many close parentheses at index {}' .format(i)) if stack: raise IndexError('No matching close parenthesis to open parenthesis ' 'at index {}'.format(stack.pop())) return parentheses_locs test_strings = [ 'as (adjks) sdj(ds(dfsf)) fdd(dsd(dsdss(1))dsds)ddsd', 'as (adjks) sdj(ds(dfsf) fdd(dsd(dsdss(1))dsds)ddsd', 'as (adjks) sdj(ds(dfsf)) fdd)(dsd(dsdss(1))dsds)ddsd', ] for i, s in enumerate(test_strings, start=1): print(' test string {}: {}'.format(i, s)) print('Parentheses match?', check_parentheses(s)) try: parentheses_locs = find_parentheses(s) print('Parentheses locations: {}'.format(str( sorted([(k,v) for k, v in parentheses_locs.items()]) ))) except IndexError as e: print(str(e)) OUTPUT test string 1: as (adjks) sdj(ds(dfsf)) fdd(dsd(dsdss(1))dsds)ddsd Parentheses match? True Parentheses locations: [(3, 9), (14, 23), (17, 22), (28, 46), (32, 41), (38, 40)] test string 2: as (adjks) sdj(ds(dfsf) fdd(dsd(dsdss(1))dsds)ddsd Parentheses match? False No matching close parenthesis to open parenthesis at index 14 test string 3: as (adjks) sdj(ds(dfsf)) fdd)(dsd(dsdss(1))dsds)ddsd Parentheses match? False Too many close parentheses at index 28Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.