Menu

[solved]-Anyone Know Solve Python Please Take Screenshots Code Clearly See Solved Please Answer Que Q39032841

Does anyone know how to solve these in Python? Please takescreenshots of the code, so I can clearly see how it was solved.please answer all questions in the picture!

LeakyStack 2 A Leaky Stack is similar to a stack except that the Leaky Stack has a bound on the number of items that can be sAdds el em to the top of the stack def pop(self) Remove and return the element at the top of the stack Exception if the staStacks with Queues 3 In this problem, try to implement a stack using a queue. Write a class called class QStack that has implor raise an Exception if the stack is empty def top(self) Returns (without removing) the element at the top of the stack

LeakyStack 2 A Leaky Stack is similar to a stack except that the Leaky Stack has a bound on the number of items that can be stored in it at once. During initialization, the maximum size N is given. If the Leaky Stack has N elements, it is considered full. When an element is added to a Leaky Stack that is full, the element at the bottom of the stack is removed to make room for the new element, which is then placed at top of the stack as usual For example, consider the following code: leaky s LeakyStack(5) leaky_s.push(17) leaky s.push (42) leaky s.push(6) leaky_s.push(31) leaky s.push(28) The Leaky Stack has a maximum size of 5 (specified when it is created with the constructor) so it is now full (because we have pushed 5 items onto it). If we now make a call to leaky s.push (2) , the item at the bottom of the stack, 17, is removed to make space for the new item, 2. This is shown below, where we show the contents of the Leaky Stack both before and after the call to leaky s.push (2): Before: Before: 28 2 28 31 31 6 12 6 42 A class that implements the Leaky Stack ADT would need to provide the following methods: def _init__(self, max_num_of_elems) Initialize an emp ty leaky stack ‘ ‘ ‘ def _len__(self): Return the number of elements in the stack ‘ ‘ ‘ def is_empty (self) Returns True if the stack is empty” def push (self, elem): Adds el em to the top of the stack” def pop(self) Remove and return the element at the top of the stack Exception if the stack is empty” or raise an def top(self) ‘Returns (without removing) the element at the top of the stack or raise an Exception if the stack is empty’ ‘ ‘ In this problem, you will provide two differemt implementations for the Leaky Stack ADT, using different data structures. 1. Implement a Leaky Stack using a dynamic array, in a class called class ArrayLeakyStack. All operations must run in time O(1) in the worst case. Hint: To handle the “leaky” part of removing the bottom element when pushing to a full stack, think of using a similar “circular” approach as we did with queues. 2. Implement a Leaky Stack using a linked list, in a class called class LinkedLeakyStack. Your class should include a data member that is an instance of the DoublyLinkedList class we wrote in lecture (you can get the code from NYU Classes) Stacks with Queues 3 In this problem, try to implement a stack using a queue. Write a class called class QStack that has implements the stack ADT. It should have an instance of the class ArrayQueue as a data member (use the ArrayQueue class we wrote in lecture – you can get the code from NYU Classes). You can only access and modify this ArrayQueue instance by using the methods that are defined as part of the ArrayQueue class (that is, the methods that make up the interface of the Queue ADT). Your QStack class may also use O(1) additional space for other data members as necessary (this is a constant amount of extra spaceso you may not include extra data structures or collections of non-constant size) Recall that as the QStack will implement the Stack ADT, you must define all the operations of the Stack ADT. A skeleton of the class along with the methods you must implement is below: class QStack: def_init__ (self, max_num_of_elems) Initialize an empty leaky stack” self.data queue = Array Queue ( # You may need to add to this cons tructor, such as adding more data memb ers # But you should at minimum have the ArrayQueue data member as above def len__(self) Return the number of elements in the stack’ ‘ ‘ def is_empty (self) ‘Returns True if the stack is empty’ def push(self, elem) Adds elem to the top of the stack” def pop(self) Remove and return the element at the t op of the stack or raise an Exception if the stack is empty’ ” def top(self) ‘Returns (without removing) the element at the top of the stack or raise an Exception if the stack is empty”’ Hint: You will not be able to achieve O(1) worst-case running times for all operations. In fact, some operations may be very inefficient. That is okay: There is no running time requirement for this problem (however, see below). Analyze the running time of the operations in your implementation, particularly push and pop. In the code you submit, include the running time of each of your operations in a comment at the top of each method’s implementation. While there is no requirement for efficiency, you must analyze the running times and include them in your submission for full credit Also think about, but don’t submit: Which of the operations, push or pop, is more efficient in your implementation? If your pop is more efficient, can you think of an alternate implementation where push would be more efficient (possibly at the expense of making pop less efficient)? If your push is more efficient, can you think of an alternate implementation where pop would be more efficient (at the expense of making push more expensive)? Show transcribed image text LeakyStack 2 A Leaky Stack is similar to a stack except that the Leaky Stack has a bound on the number of items that can be stored in it at once. During initialization, the maximum size N is given. If the Leaky Stack has N elements, it is considered full. When an element is added to a Leaky Stack that is full, the element at the bottom of the stack is removed to make room for the new element, which is then placed at top of the stack as usual For example, consider the following code: leaky s LeakyStack(5) leaky_s.push(17) leaky s.push (42) leaky s.push(6) leaky_s.push(31) leaky s.push(28) The Leaky Stack has a maximum size of 5 (specified when it is created with the constructor) so it is now full (because we have pushed 5 items onto it). If we now make a call to leaky s.push (2) , the item at the bottom of the stack, 17, is removed to make space for the new item, 2. This is shown below, where we show the contents of the Leaky Stack both before and after the call to leaky s.push (2): Before: Before: 28 2 28 31 31 6 12 6 42 A class that implements the Leaky Stack ADT would need to provide the following methods: def _init__(self, max_num_of_elems) Initialize an emp ty leaky stack ‘ ‘ ‘ def _len__(self): Return the number of elements in the stack ‘ ‘ ‘ def is_empty (self) Returns True if the stack is empty” def push (self, elem):
Adds el em to the top of the stack” def pop(self) Remove and return the element at the top of the stack Exception if the stack is empty” or raise an def top(self) ‘Returns (without removing) the element at the top of the stack or raise an Exception if the stack is empty’ ‘ ‘ In this problem, you will provide two differemt implementations for the Leaky Stack ADT, using different data structures. 1. Implement a Leaky Stack using a dynamic array, in a class called class ArrayLeakyStack. All operations must run in time O(1) in the worst case. Hint: To handle the “leaky” part of removing the bottom element when pushing to a full stack, think of using a similar “circular” approach as we did with queues. 2. Implement a Leaky Stack using a linked list, in a class called class LinkedLeakyStack. Your class should include a data member that is an instance of the DoublyLinkedList class we wrote in lecture (you can get the code from NYU Classes)
Stacks with Queues 3 In this problem, try to implement a stack using a queue. Write a class called class QStack that has implements the stack ADT. It should have an instance of the class ArrayQueue as a data member (use the ArrayQueue class we wrote in lecture – you can get the code from NYU Classes). You can only access and modify this ArrayQueue instance by using the methods that are defined as part of the ArrayQueue class (that is, the methods that make up the interface of the Queue ADT). Your QStack class may also use O(1) additional space for other data members as necessary (this is a constant amount of extra spaceso you may not include extra data structures or collections of non-constant size) Recall that as the QStack will implement the Stack ADT, you must define all the operations of the Stack ADT. A skeleton of the class along with the methods you must implement is below: class QStack: def_init__ (self, max_num_of_elems) Initialize an empty leaky stack” self.data queue = Array Queue ( # You may need to add to this cons tructor, such as adding more data memb ers # But you should at minimum have the ArrayQueue data member as above def len__(self) Return the number of elements in the stack’ ‘ ‘ def is_empty (self) ‘Returns True if the stack is empty’ def push(self, elem) Adds elem to the top of the stack” def pop(self) Remove and return the element at the t op of the stack
or raise an Exception if the stack is empty’ ” def top(self) ‘Returns (without removing) the element at the top of the stack or raise an Exception if the stack is empty”’ Hint: You will not be able to achieve O(1) worst-case running times for all operations. In fact, some operations may be very inefficient. That is okay: There is no running time requirement for this problem (however, see below). Analyze the running time of the operations in your implementation, particularly push and pop. In the code you submit, include the running time of each of your operations in a comment at the top of each method’s implementation. While there is no requirement for efficiency, you must analyze the running times and include them in your submission for full credit Also think about, but don’t submit: Which of the operations, push or pop, is more efficient in your implementation? If your pop is more efficient, can you think of an alternate implementation where push would be more efficient (possibly at the expense of making pop less efficient)? If your push is more efficient, can you think of an alternate implementation where pop would be more efficient (at the expense of making push more expensive)?

Expert Answer


Answer to Does anyone know how to solve these in Python? Please take screenshots of the code, so I can clearly see how it was solv… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *