python class Node(): def __init__(self, value): pass class PostScript(): def __init__(self): pass    def push(self, value): pass    def pop(self): return None    def peek(self): pass    def is_empty(self): pass def stack(self): pass    def exch(self): pass    def index(self): pass    def...

70.2K

Verified Solution

Question

Programming

python

class Node():

def __init__(self, value):
pass

class PostScript():

def __init__(self):
pass
  
def push(self, value):
pass
  
def pop(self):
return None
  
def peek(self):
pass
  
def is_empty(self):
pass

def stack(self):
pass
  
def exch(self):
pass
  
def index(self):
pass
  
def clear(self):
pass
  
def dup(self):
pass
  
def equal(self):
pass
  
def depth(self):
pass
  
stack = PostScript()

def decode(command_list):
for object in command_list:
if object == '=':
stack.equal()
elif object == 'count':
stack.depth()
elif object == 'clear':
stack.clear()
elif object == 'dup':
stack.dup()
elif object == 'exch':
stack.exch()
elif object == 'index':
stack.index()
elif object == 'pop':
stack.pop()
elif object == 'stack':
stack.stack()
else:
stack.push(object)

command = input()
command_list = command.split()
decode(command_list)
  

For this assignment you are to implement a PostScript commandinterpreter using a Stack. You must implement the stack using asingly-linked list. Solutions that use a data structure other thana singly-linked list will received nocredit.

The interpreter must read and execute a string of PostScriptcommands based on the following definitions:
1. =
objn objn-1 … obj1 = : objn-1 … obj1
This command removes the topmost object form the stack andprints it.

  1. count
    objn … obj1count : [depth of the stack] objn … obj1
    This command puts the depth of the stack on the top of thestack.
  2. clear
    objn … obj1clear : [empty stack]
    This command removes all objects from the stack.
  3. dup
    objn … obj1 = : objn objn … obj1
    This command adds a duplicate copy of the top object to the stack.
  4. exch
    objn objn-1 … obj1 exch : objn-1 objn … obj1
    This command exchanges the position of the top two elements of thestack.
  5. index
    objn … obj1 a index : objn … obja … obj1
    This command adds a copy of the ath object to the top thestack.
  6. pop
    objn objn-1 … obj1 pop : objn-1 …. obj1
    This command removes the top element from the stack.
  7. stack
    This command prints a copy of the entire stack, starting from thetop and working to the bottom..

    The input will be a series of data and commands separated by aspace. The data will go onto the stack. Commands do not go onto thestack. You must write at least one separate method for each of thecommands. You may reuse the standard Stack methods discussed inclass: push(), pop(), peek() and is_empty(). A template file hasbeen provided.

  

Answer & Explanation Solved by verified expert
4.2 Ratings (612 Votes)
class Nodedef initselfvalueselfvalue valueselfnext Noneclass PostScriptdef initselfselftop Nonedef pushselfvaluenode Nodevalueif selftop Noneselftop nodeelsenodenext selftopselftop nodedef popselfif selftop Noneselftop selftopnextdef peekselfif selftop Nonereturn selftopvalueelsereturn Nonedef    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students