Implementing a Stack ADT using a linked solution. The implementations are linked Nodes. class Node: def __init__(self, data, node=None): # Initialize this node, insert data, and set the next node if any self.data = data self.chain = node class SubSetStack: def __init__(self, data=None): # Initialize this stack, and store data if it exists def push(self, data): # Add data to the beginning of the stack def pop(self): # Remove the element at the beginning of the stack. # Return the data in the element at the beginning of the stack, or None if the stack is empty def top(self): # Return the data in the element at the beginning but does not remove it. # Return None if stack is empty. def __len__(self): # Return the number of elements in the stack def subset_sum(target, sub_list): # Returns True if Target can be formed from sub_list repeated # some arbitrary number of times. In the above function a Node class is given. A class called SubSetStack is given. Have to implement the methods push(), pop(), top() and the over loaded __len__() method. Push should add an item to the stack. Pop should remove an item from the stack. Top should return the data of the top item of the stack but does not remove the item. The function ___len__() should return the number of items in the stack. All methods MUST be O(1). Have to create a combination out of 'list of sub_list' to check if the combination equals to given 'target' i.e. determine if there is a subset of the given set with sum equal to given target, the 'sub_set' function needs to be implemented using an iterative method that utilize a Stack. For example:- a) if target=8, sub_list=[2,4], then 2+2+2+2 = 8 is True. b) If target = 4, sub_list = [], i.e. empty list then the result is True. c) If target = 8, sub_list = [3,6], then are no combination which will equal it to '8' then the result is False. d) If target=7, sub_list = [2,3,1], then combination can be 2+2+3, which is True because the combination sum equals to target '7'.

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

Implementing a Stack ADT using a linked solution. The implementations are linked Nodes.

class Node:
  def __init__(self, data, node=None):
      # Initialize this node, insert data, and set the next node if any
      self.data = data
      self.chain = node

class SubSetStack:
  def __init__(self, data=None):
      # Initialize this stack, and store data if it exists
     

  def push(self, data):
      # Add data to the beginning of the stack
     

  def pop(self):
      # Remove the element at the beginning of the stack.
      # Return the data in the element at the beginning of the stack, or None if the stack is empty
     

  def top(self):
      # Return the data in the element at the beginning but does not remove it.
      # Return None if stack is empty.
     

  def __len__(self):
      # Return the number of elements in the stack
     


def subset_sum(target, sub_list):
  # Returns True if Target can be formed from sub_list repeated
  # some arbitrary number of times.
 

In the above function a Node class is given. A class called SubSetStack is given. Have to implement the methods push(), pop(), top() and the over loaded __len__() method. Push should add an item to the stack. Pop should remove an item from the stack. Top should return the data of the top item of the stack but does not remove the item. The function ___len__() should return the number of items in the stack. All methods MUST be O(1).

Have to create a combination out of 'list of sub_list' to check if the combination equals to given 'target' i.e. determine if there is a subset of the given set with sum equal to given target, the 'sub_set' function needs to be implemented using an iterative method that utilize a Stack.

For example:-
a) if target=8, sub_list=[2,4], then 2+2+2+2 = 8 is True. 
b) If target = 4, sub_list = [], i.e. empty list then the result is True. 
c) If target = 8, sub_list = [3,6], then are no combination which will equal it to '8' then the result is False. d) If target=7, sub_list = [2,3,1], then combination can be 2+2+3, which is True because the combination sum equals to target '7'. 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

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