Question
11.2 Problem Set 2: PostScript Stack Commands (in python please) For this assignment you are to implement a PostScript command interpreter using a Stack. You
11.2 Problem Set 2: PostScript Stack Commands (in python please)
For this assignment you are to implement a PostScript command interpreter using a Stack. You must implement the stack using a singly-linked list. Solutions that use a data structure other than a singly-linked list will received no credit. The interpreter must read and execute a string of PostScript commands based on the following definitions: 1. = objn objn-1 obj1 = : objn-1 obj1 This command removes the topmost object form the stack and prints it.
- count objn obj1count : [depth of the stack] objn obj1 This command puts the depth of the stack on the top of the stack.
- clear objn obj1clear : [empty stack] This command removes all objects from the stack.
- dup objn obj1 = : objn objn obj1 This command adds a duplicate copy of the top object to the stack .
- exch objn objn-1 obj1 exch : objn-1 objn obj1 This command exchanges the position of the top two elements of the stack.
- index objn obj1 a index : objn obja obj1 This command adds a copy of the ath object to the top the stack.
- pop objn objn-1 obj1 pop : objn-1 . obj1 This command removes the top element from the stack.
- stack This command prints a copy of the entire stack, starting from the top and working to the bottom.. The input will be a series of data and commands separated by a space. The data will go onto the stack. Commands do not go onto the stack. You must write at least one separate method for each of the commands. You may reuse the standard Stack methods discussed in class: push(), pop(), peek() and is_empty(). A template file has been provided.
class Node():
def __init__(self, value): pass
class PostScript():
def __init__(self): pass def push(self, value): pass def pop(self): pass 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)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started