PYTHON CODE PLEASEEEE!! 1. Implement ArrayStack, ArrayQueue and ArrayList covered in the lecture 2 (ArrayBased Lists). All...

50.1K

Verified Solution

Question

Programming

PYTHON CODE PLEASEEEE!!

1. Implement ArrayStack, ArrayQueue and ArrayList covered in thelecture 2 (ArrayBased Lists). All the data structures should befully functional and must follow the logic presented in thelecture. When calling remove and add, use try and catch to handlingthe exceptions. Learning objectives: CLO 1, CLO 3 Test your datastructures using the following tests.

• Remove one element from an empty Stack, Queue, List.

• Stack: Add 5 elements and remove them checking that they arein opposite order of insertion, e.g., Inserting the sequence5,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 arein the same order of insertion, e.g., Inserting the sequence1,2,3,4,5 result in the sequence 1,2,3,4,5 when removing.

• List: Add 5 elements in different positions (including thefirst 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) shouldreturn i + 1. Remove 2 elements, e.g., index 2 and 3 and the finalarray should be \"1,2,5\".

2. Implement a RandomQueue to randomly remove the books. This isan implementation of the Queue interface in which the remove()operation removes an element that is chosen uniformly at randomamong all the elements currently in the queue. The add(x) andremove() operations in a RandomQueue should run in amortizedconstant time per operation. The template does not containRandomQueue.py. Therefore, you have to create the file. Thefollowing code shows how to define the class: from ArrayQueueimport ArrayQueue class RandomQueue(ArrayQueue): def __init__(self): ArrayQueue.__init__(self) def remove(self): ''' You can accessany member variable of ArrayQueue: self.a, self.n, self.j since itinheritance from ArrayQueue ''' ... Learning objectives: CLO 3Hint: Use the random methods in your language randint from themodule 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 operationreturn 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 listrepresentation 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 listrepresentation 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

Answer & Explanation Solved by verified expert
4.2 Ratings (730 Votes)
As per your question The Python 3x program is writtenperfectly and correctly Please have a look on the source coderefer inline comments for better understandingand the run outputbelowdefining a stackarrayStack def addElementStackele arrayStackappendeledef popElementStack try return arrayStackpop except IndexError printThe arrayStack is empty except Error as e printSomething went wrong edefining an queuearrayQueue def addElementQueueele arrayQueueappendeledef    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students