please be thorough with explanations. thank you
Working code implemented in Python and appropriate comments provided for better understanding.
Source Code for main.py:
import ctypes # provides low-level arrays
def make_array(n):
return (n * ctypes.py_object)()
class ArrayList:
def __init__(self):
self.data = make_array(1)
self.capacity = 1
self.n = 0
def __len__(self):
return self.n
def append(self, val):
if (self.n == self.capacity):
self.resize(2 * self.capacity)
self.data[self.n] = val
self.n += 1
def resize(self, new_size):
new_array = make_array(new_size)
for i in range(self.n):
new_array[i] = self.data[i]
self.data = new_array
self.capacity = new_size
def extend(self, iter_collection):
for elem in iter_collection:
self.append(elem)
def insert(self, index, val):
if (not (-self.n <= index <= self.n - 1)):
raise IndexError('invalid index')
if (self.n == self.capacity):
self.resize(2 * self.capacity)
self.append(1)
for i in range(-1, -(index+1),-1):
self[i] = self[i-1]
self[index] = val
def pop(self, index = -1):
if self.n == 0:
raise IndexError("empty list")
if abs(index) > self.n:
raise IndexError("out of bound")
if index < 0 and index != -1:
index += self.n
if index == -1:
temp = self.data[self.n-1]
self.data[self.n-1] = None
else:
temp = self.data[index]
self.data[index] = None
for i in range(index + 1, self.n):
self.data[i-1] = self.data[i]
self.n -= 1
if ((self.n) < (self.capacity//4)):
self.resize(self.capacity//2)
return temp
def __getitem__(self, ind):
if (not (-self.n <= ind <= self.n - 1)):
raise IndexError('invalid index')
if (ind < 0):
ind = self.n + ind
return self.data[ind]
def __setitem__(self, ind, val):
if (not (-self.n <= ind <= self.n - 1)):
raise IndexError('invalid index')
if (ind < 0):
ind = self.n + ind
self.data[ind] = val
def __repr__(self):
data_as_strings = [str(self.data[i]) for i in range(self.n)]
return '[' + ', '.join(data_as_strings) + ']'
def __add__(self, other):
res = ArrayList()
res.extend(self)
res.extend(other)
return res
def __iadd__(self, other):
self.extend(other)
return self
def __mul__(self, times):
res = ArrayList()
for i in range(times):
res.extend(self)
return res
def __rmul__(self, times):
return self * times
def main():
arr_lst = ArrayList()
for i in range(1, 5+1):
arr_lst.append(i)
print(arr_lst)
print(arr_lst.pop())
print(arr_lst)
main()
Code Screenshots:
Sample Output Screenshots:
please be thorough with explanations. thank you Question 2 Consider the implementation we made in class...
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...
JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...
Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...
iii.print out the arraylist iv.reverse all the elements v.print out this arraylist vi.make a clone of the arraylist vii.remove all the elements at any odd index of the original arraylist (not the cloned one) viii.print out the original arraylist ix.reverse the cloned arraylist x.print out the cloned arraylist (this arraylist should still contain the original sequence of elements in order) xi.merge the cloned arraylist to the original arraylist (please think about what happens and draw a diagram for yourself to...
JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{ private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum capacity private T[] data; //underlying array // ADD MORE PRIVATE MEMBERS HERE IF NEEDED! @SuppressWarnings("unchecked") public SmartArray(){ //constructor //initial capacity of the array should be DEFAULT_CAPACITY } @SuppressWarnings("unchecked") public SmartArray(int initialCapacity){ // constructor // set the initial capacity of...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...
Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDE Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS 2) ArrayList ADT that uses a linked list internally (call it LArrayList) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....
Modify the LinkedCollection class to be a SortedLinkedCollecton class and see how that effects our implementation for adding and removing items. You should reference the SortedArrayCollection class provided for how these algorithms should be implemented. What needs to change here? Is it a lot of code or not much? Include a toString method that creates and returns a string that correctly represents the current collection. Include a test driver application that demonstrates your class correctly. //--------------------------------------------------------------------------- // LinkedCollection.java // //...
In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element); /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...
Create a Java code that includes all the methods from the Lecture slides following the ADTs LECTURE SLIDES Collect/finish the Java code (interface and the complete working classes) from lecture slides for the following ADTS: 4) Queue ADT that uses a linked list internally (call it LQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! For each method you develop, add comments and estimate the big-O running time of its algorithm....