Question
Need help the first picture is the code I have so far the second are the requirements. It’s a deque array in python
class dequeArray: DEFAULT-CAPACITY 10 #moderate capacity for all new queues def init (self): self.capacity-5 capacity self.capacity self.data self, make array(self. capacity) self. size self. front-θ def len (self): return self. size def _getiten-(self, k); #Return element at index k if not θ k < self. size: raise IndexError(invalid index) return self._data[k] def isEmpty(self): if self. data0: return False else: return True def append(self, item): #add an element to the back of the queue if self. size self.capacity: self. data.pop(8) else: avail-(self-front + self.-size) self. data[avail] item self. size1 % len(self, data) def make array(self, c): capacity self, capacity turn (capacity ctypes.py object) def removeFirst(self): if self. size self.capacity: self. data.pop(8) else: answer self. data[self. _front] self. data[self. front] - None self-front-(self.-front + 1)、len(self.data) self. size 1 print(answer)
media%2F0a0%2F0a05dbfe-c59e-4af4-b47e-bf
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import ctypes

class DequeArray:

def __init__(self):

self.capacity = 5

capacity = self.capacity

self._data = self._make_array(self.capacity)

self._size = 0

self._front = 0

def _make_array(self, c):

capacity = self.capacity

return(capacity*ctypes.py_object)()

def isEmpty(self) :

if self._data == 00:

return False

else:

return True

def __len__(self):

return self._size

def __getitem__(self, index): #return element at index k

if not 0 <= index <self._size:

raise IndexError('invalid index')

return self._data[index]

def __setitem__(self, index, value):

if not 0 <= index <self._size:

raise IndexError('invalid index')

self._data[index] = value

def append(self, item):

if self._size == self.capacity:

self._data.pop(0)

else:

avail = (self._front + self._size)% len(self._data)

self._data[avail] = item

self._size += 1

def preappend(self, item): # update front index after preappend

for index in range(self._size()):

index+=1

self._front = 0

self._size = index

self.insert(self._front,value)

def removeFirst(self): #update index after removal

if self._size == self.capacity:

self._data.pop(0)

else:

answer = self._data[self._front]

self._data[self._front] = None

self._front = (self._front + 1)%len(self._data)

self._size -= 1

return answer

def removeLast(self):

if self._size == self.capacity:

self._data.pop(0)

else:

value = self._data[self._size]

self._data[self._size] = None

self._size -= 1

return value

def insert(self, index, item):

if self._size == self.capacity:

self._data.pop(self._data[self._size])

temp = index

for index in (index, self._size()):

index+=1

self.__setitem__(temp, item)

# def remove(self, index, item):

# def print(self):

d = DequeArray()

print(d.isEmpty())

d.insert(1,10)

d.append(2)

d.preappend(3)

print(d.__len__())

print(d.__getitem__(1)) # return element at index k

d.__setitem__(2,5) # set value 5 at index 2

print(d.removeFirst())

print(d.removeLast())

print(d.remove(0)) #Remove and return the item at the index, and shift the subsequent items

print(d.print())

Add a comment
Know the answer?
Add Answer to:
Need help the first picture is the code I have so far the second are the...
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. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

  • PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment...

    PYTHON. Continues off another code(other code is below). I don't understand this. Someone please help! Comment the lines please so I can understand. There are short and med files lengths for each the list of names/ids and then search id file. These are the input files: https://codeshare.io/aVQd46 https://codeshare.io/5M3XnR https://codeshare.io/2W684E https://codeshare.io/5RJwZ4 LinkedList ADT to store student records(code is below). Using LinkedList ADT instead of the Python List. You will need to use the Student ADT(code is below) Imports the Student class...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

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

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

  • In this part, you will complete the code to solve a maze.

    - Complete the code to solve a maze- Discuss related data structures topicsProgramming-----------In this part, you will complete the code to solve a maze.Begin with the "solveMaze.py" starter file.This file contains comment instructions that tell you where to add your code.Each maze resides in a text file (with a .txt extension).The following symbols are used in the mazes:BARRIER = '-' # barrierFINISH = 'F' # finish (goal)OPEN = 'O' # open stepSTART = 'S' # start stepVISITED = '#' #...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • Hey I have a task which consists of two part. Part A asks for writing a...

    Hey I have a task which consists of two part. Part A asks for writing a program of WORD & LINE CONCORDANCE APPLICATION in python which I have completed it. Now the second part has given 5 dictionary implementation codes namely as: (ChainingDict, OpenAddrHashDict with linear probing, OpenAddrHashDict with quadratic probing, and 2 tree-based dictionaries from lab 12 (BST-based dictionary implementation) and asks for my above program WORD & LINE CONCORDANCE APPLICATION  to use these implemented code and show the time...

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