Question 1.    Inheritance: 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 inheritance.   inherit the following methods below: a.    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. b.    push() - take an item as input and push it on the top of the stack c.    pop() - remove and return the item at the top of the stack d.    isEmpty() - returns True if the stack is empty, False otherwise e.    [] - return the item at a given location, [0] is at the bottom of the stack f.     len() - return length of the stack   Output for Question 1: ##### Stack #####          >>> s = Stack() >>> len( vars(s)) # really inheriting? failure on this test means Stack is probably not properly inheriting 0 >>> isinstance(s,list) # inheriting? True   >>> 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' >>>    if you receive an error on s2 then you are probably not calling the list constructor init.     >>> s = Stack() >>> s.push('apple') >>> s Stack(['apple']) >>> s2 = Stack() >>> s2.push('pear') >>> s2 # if this fails - see the TEST file for explanation Stack(['pear'])     check that the Stack is really inheriting from list   >>> s = Stack() >>> isinstance(s, list) True >>> len( vars(s) )==0 True     Question 2: 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 otherwise. Your solution must use a Stack.     Output for Question 2:   ##### parenthesesMatch #####     >>> 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

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

Please use Python for this question:

 

Question 1.    Inheritance: 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 inheritance.

 

inherit the following methods below:

a.    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.

b.    push() - take an item as input and push it on the top of the stack

c.    pop() - remove and return the item at the top of the stack

d.    isEmpty() - returns True if the stack is empty, False otherwise

e.    [] - return the item at a given location, [0] is at the bottom of the stack

f.     len() - return length of the stack

 

Output for Question 1:

##### Stack #####       

 

>>> s = Stack()

>>> len( vars(s)) # really inheriting? failure on this test means Stack is probably not properly inheriting

0

>>> isinstance(s,list) # inheriting?

True

 

>>> 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'

>>> 

 

if you receive an error on s2 then you are probably not calling the list

constructor init.

 

 

>>> s = Stack()

>>> s.push('apple')

>>> s

Stack(['apple'])

>>> s2 = Stack()

>>> s2.push('pear')

>>> s2 # if this fails - see the TEST file for explanation

Stack(['pear'])

 

 

check that the Stack is really inheriting from list

 

>>> s = Stack()

>>> isinstance(s, list)

True

>>> len( vars(s) )==0

True

 

 

Question 2: 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 otherwise. Your solution must use a Stack.

 

 

Output for Question 2:

 

##### parenthesesMatch #####

 

 

>>> 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

 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Knowledge Booster
Stack
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