Inheritance (based on 8.38) You MUST use inheritance for this problem. A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack.  A stack is often referred to as a last-in first-out (LIFO) container because the last item inserted is the first removed.  Implement a Stack class using Note that this means you may be able to inherit some of the methods below.  Which ones?  (Try not writing those and see if it works!) Constructor/_init__ - Can construct either an empty stack, or initialized with a list of items, the first item is at the bottom, the last is at the top. push() – take an item as input and push it on the top of the stack pop() – remove and return the item at the top of the stack isEmpty() – returns True if the stack is empty, False otherwise [] – return the item at a given location, [0] is at the bottom of the stack len() – return length of the stack The object is to make this client code work:   ''' >>> s = Stack() >>> s.push('apple') >>> s Stack(['apple']) >>> s.push('pear') >>> s.push('kiwi') >>> s Stack(['apple', 'pear', 'kiwi']) >>> top = s.pop() >>> top 'kiwi' >>> s Stack(['apple', 'pear']) >>> len(s) 2 >>> s.isEmpty() False >>> s.pop() 'pear' >>> s.pop() 'apple' >>> s.isEmpty() True >>> s = Stack(['apple', 'pear', 'kiwi']) >>> s = Stack(['apple', 'pear', 'kiwi']) >>> s[0] 'apple' >>> ''' 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.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Please use Python for this question:

 

  1. Inheritance (based on 8.38) You MUST use inheritance for this problem. A stack is a sequence container type that, like a queue, supports very restrictive access methods: all insertions and removals are from one end of the stack, typically referred to as the top of the stack.  A stack is often referred to as a last-in first-out (LIFO) container because the last item inserted is the first removed.  Implement a Stack class using Note that this means you may be able to inherit some of the methods below.  Which ones?  (Try not writing those and see if it works!)
    1. Constructor/_init__ - Can construct either an empty stack, or initialized with a list of items, the first item is at the bottom, the last is at the top.
    2. push() – take an item as input and push it on the top of the stack
    3. pop() – remove and return the item at the top of the stack
    4. isEmpty() – returns True if the stack is empty, False otherwise
    5. [] – return the item at a given location, [0] is at the bottom of the stack
    6. len() – return length of the stack

The object is to make this client code work:

 

'''

>>> s = Stack()

>>> s.push('apple')

>>> s

Stack(['apple'])

>>> s.push('pear')

>>> s.push('kiwi')

>>> s

Stack(['apple', 'pear', 'kiwi'])

>>> top = s.pop()

>>> top

'kiwi'

>>> s

Stack(['apple', 'pear'])

>>> len(s)

2

>>> s.isEmpty()

False

>>> s.pop()

'pear'

>>> s.pop()

'apple'

>>> s.isEmpty()

True

>>> s = Stack(['apple', 'pear', 'kiwi'])

>>> s = Stack(['apple', 'pear', 'kiwi'])

>>> s[0]

'apple'

>>>

'''

  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 4 steps with 3 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY