Please answer the question below in Python:   Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ’([{}])’, returns True if the parentheses, brackets and braces match and False Your solution must use a Stack. For, example:   >>> parenthesesMatch('(){}[]') True >>> parenthesesMatch('{[()]}') True >>> parenthesesMatch('((())){[()]}') True >>> parenthesesMatch('(}') False >>> parenthesesMatch(')(][') # right number, but out of order False >>> parenthesesMatch('([)]') # right number, but out of order False >>> parenthesesMatch('({])') False >>> parenthesesMatch('((())') False >>> parenthesesMatch('(()))') False Hint:  It is not sufficient to just count the number of opening and closing marks.  But, it is easy to write this as a simple application of the Stack class.   Here is an algorithm: Create an empty stack. Iterate over the characters in the given string: If the character is one of opening marks(,[,{ push it on the stack. If the character is one of the closing marks ),],} and the stack is empty, then there were not enough preceding opening marks, so return False. If the character is a closing mark and the stack is not empty, pop an (opening) mark from the stack. If they are not of the same type, ie., ( and ) or [ and ] or { and }, return False, if they are of the same type, move on to the next char. Once the iteration is finished, you know that the parentheses match if and only if the stack is empty.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

Please answer the question below in Python:

 

  1. Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ’([{}])’, returns True if the parentheses, brackets and braces match and False Your solution must use a Stack. For, example:

 

>>> parenthesesMatch('(){}[]')

True

>>> parenthesesMatch('{[()]}')

True

>>> parenthesesMatch('((())){[()]}')

True

>>> parenthesesMatch('(}')

False

>>> parenthesesMatch(')(][') # right number, but out of order

False

>>> parenthesesMatch('([)]') # right number, but out of order

False

>>> parenthesesMatch('({])')

False

>>> parenthesesMatch('((())')

False

>>> parenthesesMatch('(()))')

False

Hint:  It is not sufficient to just count the number of opening and closing marks.  But, it is easy to write this as a simple application of the Stack class.   Here is an algorithm:

  1. Create an empty stack.
  2. Iterate over the characters in the given string:
    1. If the character is one of opening marks(,[,{ push it on the stack.
    2. If the character is one of the closing marks ),],} and the stack is empty, then there were not enough preceding opening marks, so return False.
    3. If the character is a closing mark and the stack is not empty, pop an (opening) mark from the stack. If they are not of the same type, ie., ( and ) or [ and ] or { and }, return False, if they are of the same type, move on to the next char.
  3. Once the iteration is finished, you know that the parentheses match if and only if the stack is empty.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Use of XOR function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education