Question

1. The Operand Stack - opstack The operand stack should be implemented as a Python list....

1. The Operand Stack - opstack The operand stack should be implemented as a Python list. The list will contain Python integers, strings, and later in Part 2 code arrays. Python integers and lists on the stack represent Postscript integer constants and array constants. Python strings which start with a slash / on the stack represent names of Postscript variables. When using a list as a stack, assume that the top of the stack is the end of the list (i.e., the pushing and popping happens at the end of the list).

The operand stack: define the operand stack and its operations opstack = [ ] #assuming top of the stack is the end of the list # Now define the helper functions to push and pop values on the opstack # (i.e, add/remove elements to/from the end of the Python list) # Remember that there is a Postscript operator called "pop" so we choose # different names for these functions. # Recall that `pass` in python is a no-op: replace it with your code.

def opPop():

pass

# opPop should return the popped value. # The pop() function should call opPop to pop the top value from the opstack, but it will ignore the popped value.

def opPush(value):

pass

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN PYTHON:

opStack=[] #list to act as stack

def opPop(): #function to pop the top of the stack
global opStack
if len(opStack)==0: #if stack is empty
return None
else:
pop=opStack[len(opStack)-1] #popping the top
opStack=opStack[:len(opStack)-1] #removing the top
return pop #return top

def opPush(value): #function to push a value into the stack
global opStack
opStack.append(value) #appending value to top of stack
  
def opPrint(): #function to print the stack
global opStack
for i in opStack:
print(i,end=' ')
print()
  
#testing the functions
opPush(1) #stack is now 1
print('1 pushed!')
opPush(2) #stack is now 1 2
print('2 pushed!')
opPush(3) #stack is now 1 2 3
print('3 pushed!')
print('Stack currently:')
opPrint()
value=opPop() #stack is now 1 2
if value==None:
print('Stack underflow!')
else:
print(value,'popped!')
print('Stack currently:')
opPrint()

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
1. The Operand Stack - opstack The operand stack should be implemented as a Python list....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 11.2 Problem Set 2: PostScript Stack Commands (in python please) For this assignment you are to...

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

  • ii. (30) A Stack is a container commonly used to provide a way to keep organized...

    ii. (30) A Stack is a container commonly used to provide a way to keep organized so that the last one pushed' onto the top of the stack is thth one 'popped (removed)- so stacks are called "last-in-first-out". on top of the stack is visible, but after removing the top item, t pushed item is visible, since it is back on top. The following Python co is a recursive definition of the class Stack. Read the code, and answer te...

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • help finish Queue, don't think I have the right thing. # 1. After studying the Stack...

    help finish Queue, don't think I have the right thing. # 1. After studying the Stack class and testStack() functions in stack.py # complete the Queue class below (and test it with the testQueue function) # # 2. Afer studying and testing the Circle class in circle.py, # complete the Rectangle class below (and test it with the testRectangle function) # # # 3. SUBMIT THIS ONE FILE, with your updates, TO ICON. # # # NOTE: you may certainly...

  • Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings...

    Python 3.7.3 ''' Problem 3 Write a function called names, which takes a list of strings as a parameter. The strings are intended to represent a person's first and last name (with a blank in between). Assume the last names are unique. The function should return a dictionary (dict) whose keys are the people's last names, and whose values are their first names. For example: >>> dictionary = names([ 'Ljubomir Perkovic', \ 'Amber Settle', 'Steve Jost']) >>> dictionary['Settle'] 'Amber' >>>...

  • Assume you are working with a stack implementation of the linked list definition pasted below, write a member method “po...

    Assume you are working with a stack implementation of the linked list definition pasted below, write a member method “pop”.  The method should return a value (in the “popped” node).  Assume the existence of the node references called “TheStack” and “Top”. These references point to the start (or bottom) and top of the stack (or back of the list). ------- Definition:             class node {           boolean data;           node link; }          

  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

  • It's in Python 3. Please make sure it displays correct strings lenght And displays correct character...

    It's in Python 3. Please make sure it displays correct strings lenght And displays correct character lenght 1 def sum list(numbers): Sums a list of integers param numbers: a list of intege rs return: the sum of the integers in numbe rs def get_string_lengths (st rings) : 10 11 Given a list of strings, return a list of intege rs representing the lengths of the input strings :param strings: a list of strings return: a list of integers represent ing...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • C++ Carefully review Program 19-2 in your textbook, MathStack. This is a static stack, meaning that the size of the stack is set at the beginning of the program (see line 11): MathStack stack(5). I wo...

    C++ Carefully review Program 19-2 in your textbook, MathStack. This is a static stack, meaning that the size of the stack is set at the beginning of the program (see line 11): MathStack stack(5). I would like you to modify this program as follows: 1. Implement it as a dynamic stack (do not use a static stack as it is designed in the book). Use a linked list to implement this. There's code in the book. 2. Add functionality for...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT