Question

PYTHON - Write and test the following program that meets the following conditions ------------------------------------------------------- write a...

PYTHON - Write and test the following program that meets the following conditions
    -------------------------------------------------------
    write a program that Tests the methods of stack for empty and
    non-empty stacks using the data in 
    is_empty, push, pop, peek
    *Testing pop and peek while empty throws exception
    -------------------------------------------------------
   CONDITIONS
        source - list of data (list of ?)
    RETURNS
        None
   ------------------------------------------------------- 

CODE FOR : Empty, Push, Pop & Peek

def is_empty(self):
  
result = True
if len(self._values) > 0:
result = False
return result


def push(self, value):
  
self._values.append(deepcopy(value))
return


def pop(self):
  
assert len(self._values) > 0, "Cannot pop from an empty stack"
value = self._values.pop()
return value


def peek(self):
assert len(self._values) > 0, "Cannot peek at an empty stack"

value = deepcopy(self._values[-1])
return value

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

Testing of program

from copy import deepcopy
class stack():

   def __init__(self,stk):
       self._values = stk

   def is_empty(self):
       result = True
       if len(self._values) > 0:
           result = False
       return result


   def push(self, value):
       self._values.append(deepcopy(value))
       return


   def pop(self):
       assert len(self._values) > 0, "Cannot pop from an empty stack"
       value = self._values.pop()
       return value


   def peek(self):
       assert len(self._values) > 0, "Cannot peek at an empty stack"

       value = deepcopy(self._values[-1])
       return value


a = [1,2,3,4]

s = stack(a);
while not s.is_empty():

   print(s.peek())
   s.pop()

s.pop() # This throws exception :

# assert len(self._values) > 0, "Cannot pop from an empty stack"
# AssertionError: Cannot pop from an empty stack

s.peek() # this too throws execption as stack is empty

# assert len(self._values) > 0, "Cannot peek at an empty stack"
# AssertionError: Cannot peek at an empty stack

Add a comment
Know the answer?
Add Answer to:
PYTHON - Write and test the following program that meets the following conditions ------------------------------------------------------- write a...
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
  • PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we...

    PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we will implement the DrippingStack class. This is a fixed capacity stack; it's size does not increase or decrease automatically. Capacity of this DrippingStack will be determined during initialization. When push is invoked with the DrippingStack at full capacity, rather than throwing a FullStack exception, a more typical semantic is to accept the pushed element at the top while dripping the oldest element from the...

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

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

  • Write a C++ program that will determine if one input string is a subsequence of a...

    Write a C++ program that will determine if one input string is a subsequence of a second input string. In a subsequence, all the characters in the first string will also be in the second string in the same order. For example: “abc” is a subsequence of “qzabc”. While the characters must be in the same order, they do not have to be consecutive. For example: “abc” is also a subsequence of “aaqbzcw”. We will use two stacks one and...

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

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an ...

    Java. Must not use Java API java.util.Stack /** A class of stacks whose entries are stored in an array. Implement all methods in ArrayStack class using resizable array strategy, i.e. usedoubleArray() Must throw StackException during exception events in methods:    peek(), pop(), ArrayStack(int initialCapacity) Do not change or add data fields Do not add new methods */ import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Write a Python function that uses the stack data structure to check if a string is...

    Write a Python function that uses the stack data structure to check if a string is a palindrome or not. You can use any of the dollowing stack methods: push(), pop(), peek, empty(). Note that a palindrome is a word, phrase, or sequence that reads the same backwards as forward

  • Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The...

    Dynamic Implementation of Stack - The purpose is to use our dynamic implementation of stack. The application will be to add large numbers. Review adding large numbers Remember that we can use stacks to safely add integer values that overflow the int data type g. in Java, the maximum possible int value Integer.MAX_VALUE is: 2147483647 so any int addition larger than this will overflow and fail Using stacks to add large numbers safely Will actually represent the large integers to...

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