In: Computer Science
PYTHON CODE PLEASEEEE!!
1. Implement ArrayStack, ArrayQueue and ArrayList covered in the lecture 2 (ArrayBased Lists). All the data structures should be fully functional and must follow the logic presented in the lecture. When calling remove and add, use try and catch to handling the exceptions. Learning objectives: CLO 1, CLO 3 Test your data structures using the following tests.
• Remove one element from an empty Stack, Queue, List.
• Stack: Add 5 elements and remove them checking that they are in opposite order of insertion, e.g., Inserting the sequence 5,4,3,2,1 result in the sequence 1,2,3,4,5 when removing
• Queue: Add 5 elements and remove them checking that they are in the same order of insertion, e.g., Inserting the sequence 1,2,3,4,5 result in the sequence 1,2,3,4,5 when removing.
• List: Add 5 elements in different positions (including the first and last) and check that they are in order, e.g., add(0, 4), add(0, 1), add(1, 3), add(1, 2), and add(4, 5). Then get(i) should return i + 1. Remove 2 elements, e.g., index 2 and 3 and the final array should be "1,2,5".
2. Implement a RandomQueue to randomly remove the books. This is an implementation of the Queue interface in which the remove() operation removes an element that is chosen uniformly at random among all the elements currently in the queue. The add(x) and remove() operations in a RandomQueue should run in amortized constant time per operation. The template does not contain RandomQueue.py. Therefore, you have to create the file. The following code shows how to define the class: from ArrayQueue import ArrayQueue class RandomQueue(ArrayQueue): def __init__(self) : ArrayQueue.__init__(self) def remove(self): ''' You can access any member variable of ArrayQueue: self.a, self.n, self.j since it inheritance from ArrayQueue ''' ... Learning objectives: CLO 3 Hint: Use the random methods in your language randint from the module random. Test your RandomQueue using the following tests.
• Remove one element from an empty RandomQueue
• Add 5 elements and remove all. Check that the remove operation return random values
import numpy as np
from Interfaces import Queue
class ArrayQueue(Queue):
def __init__(self):
self.n = 0
self.j = 0
self.a = self.new_array(1)
def new_array(self, n: int) ->np.array:
return np.zeros(n, np.object)
def resize(self):
'''
Resize the array
'''
b = [None]*max(1,2*self.n)
for k in range(0,self.n):
b[k] = self.a[(self.j+k) % len(self.a)]
self.a = b
self.j = 0
pass
def add(self, x : np.object) :
'''
shift all j > i one position to the right
and add element x in position i
'''
if self.n +1 > len(self.a):
self._resize()
self.a[(self.j +self.n)% len(self.a)] = x
self.n = self.n + 1
return True
pass
def remove(self) -> np.object :
'''
remove the first element in the queue
'''
x = self.a[self.j]
self.j = (self.j +1) % len(self.a)
self.n = self.n-1
if len(self.a) >= 3*self.n:
self._resize()
return x
pass
def size(self) :
return self.n
def __str__(self):
s = "["
for i in range(0, self.n):
s += "%r" % self.a[(i + self.j) % len(self.a)]
if i < self.n-1:
s += ","
return s + "]"
def __iter__(self):
self.iterator = 0
return self
def __next__(self):
if self.iterator < self.n:
x = self.a[self.iterator]
self.iterator +=1
else:
raise StopIteration()
return x
"""An implementation of the adjacency list representation of a graph"""
from Interfaces import Graph, List
import numpy as np
import copy
import ArrayList
import ArrayStack
class AdjacencyList(Graph):
def __init__(self, n : int):
self.n = n
self.adj = np.zeros(n, object)
for i in range(self.n):
self.adj[i] = ArrayList.ArrayList()
def add_edge(self, i : int, j : int):
# todo
pass
def remove_edge(self, i : int, j : int):
# todo
pass
def has_edge(self, i : int, j: int) ->bool:
# todo
pass
def out_edges(self, i) -> List:
# todo
pass
def in_edges(self, i) -> List:
# todo
pass
def bfs(self, r : int, dest: int):
# todo
pass
def dfs(self, r : int, dest: int):
# todo
pass
def __str__(self):
s = ""
for i in range(0, self.n):
s += "%i,%r\n" % (i, self.adj[i].__str__())
return s
"""An implementation of the adjacency list representation of a graph"""
from Interfaces import Graph, List
import numpy as np
import copy
import ArrayList
import ArrayStack
class AdjacencyList(Graph):
def __init__(self, n : int):
self.n = n
self.adj = np.zeros(n, object)
for i in range(self.n):
self.adj[i] = ArrayList.ArrayList()
def add_edge(self, i : int, j : int):
# todo
pass
def remove_edge(self, i : int, j : int):
# todo
pass
def has_edge(self, i : int, j: int) ->bool:
# todo
pass
def out_edges(self, i) -> List:
# todo
pass
def in_edges(self, i) -> List:
# todo
pass
def bfs(self, r : int, dest: int):
# todo
pass
def dfs(self, r : int, dest: int):
# todo
pass
def __str__(self):
s = ""
for i in range(0, self.n):
s += "%i,%r\n" % (i, self.adj[i].__str__())
return s
As per your question, The Python 3.x program is written perfectly and correctly. Please have a look on the source code (refer in-line comments for better understanding)and the run output below:-
#defining a stack
arrayStack = []
def addElementStack(ele):
arrayStack.append(ele)
def popElementStack():
try:
return arrayStack.pop()
except IndexError:
print("The arrayStack is empty")
except Error as e:
print("Something went wrong: ", e)
#defining an queue
arrayQueue = []
def addElementQueue(ele):
arrayQueue.append(ele)
def dequeueElementQueue():
try:
return arrayQueue.pop(0)
except IndexError:
print("The arrayQueue is empty")
except Error as e:
print("Something went wrong")
#defining an arraylist
arrayList = []
def addElementIndexList(index: int, ele):
arrayList.insert(index, ele)
def removeIndexElementList(index):
diff = finalLength - len(arrayList)
try:
return arrayList.pop(index - diff)
except IndexError:
print("The arrayList is empty")
except:
print("Something went wrong")
#performing operations on the above defined arrayStack, arrayQueue, and arrayList
#removing one element from an empty arrayStack
extElement = popElementStack()
print(extElement)
#removing one element from an empty arrayQueue
extElement = dequeueElementQueue()
print(extElement)
#removing one element from an empty arrayList
finalLength = int(len(arrayList))
extElement = removeIndexElementList(0)
print(extElement)
#Adding 5 elements in arrayStack
for i in range(1,6):
addElementStack(i)
#Removing the stored elements from arrayStack
print("arrayStack Elements:")
for i in range(0, len(arrayStack)):
extElement = popElementStack()
print(extElement)
#Adding 5 elements in arrayQueue
for i in range(1, 6):
addElementQueue(i)
#Removing the stored elements from arrayQueue
print("arrayQueue Elements:")
for i in range(0, len(arrayQueue)):
extElement = dequeueElementQueue()
print(extElement)
#Adding 5 elements in arrayList
addElementIndexList(0, 4)
addElementIndexList(0, 1)
addElementIndexList(1, 3)
addElementIndexList(1, 2)
addElementIndexList(4, 5)
#Removing the stored elements from arrayList
finalLength = int(len(arrayList)) #updating the length of arrayList
extElement = removeIndexElementList(2) #removing 2nd index element
extElement = removeIndexElementList(3) #removing 3rd index element
print("arrayList Elements after the all operation on arrayList:")
print(arrayList) #displaying the rest arrayList elements
=> The Run Output of the above program(as per your 1st question including all sub-parts):-
Sorry for not providing the solution of both of the questions, because we are only answering one question(up to four sub parts) at one time due to time constraint.
Thank You.. please don't forget to rate it.