Question

what is the worst-case asymptotic performance of each method? from Deque import Deque class Array_Deque(Deque): def...

what is the worst-case asymptotic performance of each method?

from Deque import Deque

class Array_Deque(Deque):


  def __init__(self):
    # capacity starts at 1; we will grow on demand.
    self.__capacity = 1
    self.__contents = [None] * self.__capacity
    self.__size = 0
    self.__front = 0
    self.__back = 0
    
  def __str__(self):
    if (self.__size == 0):
      return "[ ]"
    display_string = "[ "
    for i in range(self.__size):
      display_string = display_string + str(self.__contents[(self.__front + i) % self.__capacity])
      if (i != self.__size - 1):
        display_string = display_string + ", "
      
    display_string = display_string + " ]"
    return display_string  
    
    
  def __len__(self):
    return self.__size


  def __grow(self):
    # Note: list ranges exclude the second bound (eg. contents[0:2] contains only the zeroeth and first element.)
    if (self.__front > self.__back):
      self.__contents = self.__contents[self.__front : self.__capacity] + self.__contents[0 : self.__back + 1]
    else:
      self.__contents = self.__contents[self.__front : self.__back + 1]
    self.__front = 0
    self.__back = self.__capacity - 1
    more_room = [None] * self.__capacity    
    self.__capacity = self.__capacity * 2
    self.__contents = self.__contents + more_room #possible improvement remove cata
    
  def push_front(self, val):
    if ((((self.__front -1 + self.__capacity) % self.__capacity) == self.__back) and self.__size != 0):
      self.__grow()
    self.__front = self.__front - 1
    self.__front = (self.__front + self.__capacity) % self.__capacity 
    self.__contents[self.__front] = val
    self.__size = self.__size + 1
    
  def pop_front(self):
    val_popped = [None]
    if (self.__size != 0):
      val_popped = self.__contents[self.__front]
      self.__front = self.__front + 1
      self.__front = self.__front % self.__capacity
      self.__size = self.__size - 1
    return val_popped
    
  def peek_front(self):
    return self.__contents[self.__front]
    
  def push_back(self, val):
    if ((((self.__back + 1) % self.__capacity) == self.__front) and self.__size != 0):
      self.__grow()
    self.__back = self.__back + 1
    self.__back = self.__back % self.__capacity
    self.__contents[self.__back] = val
    self.__size = self.__size + 1
  
  def pop_back(self):
    val_popped = self.__contents[self.__back]
    if (self.__size != 0):
      self.__back = self.__back - 1
      self.__back = (self.__back + self.__capacity) % self.__capacity
      self.__size = self.__size - 1
    return val_popped


  def peek_back(self):
    return self.__contents[self.__back]


# No main section is necessary. Unit tests take its place.
  # if __name__ == '__main__':
  # a = Array_Deque()
  # print(a)
  # a.push_front(1)
  # print(a)
  # b = a.peek_front()
  # print(b)
  # a.pop_front()
  # print(a)
  # a.push_front(1)
  # print(a)
  # a.push_front(2)
  # print(a)
  # a.push_front(3)
  # print(a)
  # c = Array_Deque()
  # print(c)
  # d = c.peek_front()
  # print(d)
  # e = c.pop_front()
  # print(c)
  # print(e)
0 0
Add a comment Improve this question Transcribed image text
Answer #1
 

1) init ---> O(1) as it just initializes the value

2) __str__ ---> O(N) as it returns the string representation of string

3) __len__ ---> O(1) as it just return the length

4) __grow ---> O(1) as it just grows the lists

5) push_front ---> O(1)

6) pop_front ---> O(1)

7) peek_front ---> O(1)

8) push_back ---> O(1)

9) pop_back ---> O(1)

10) peek_back ---> O(1)

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
what is the worst-case asymptotic performance of each method? from Deque import Deque class Array_Deque(Deque): def...
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 -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None   

    PYTHON -------------------------------------------------------- class LinkedList:    def __init__(self):        self.__head = None        self.__tail = None        self.__size = 0    # Return the head element in the list    def getFirst(self):        if self.__size == 0:            return None        else:            return self.__head.element        # Return the last element in the list    def getLast(self):        if self.__size == 0:            return None        else:            return self.__tail.element    # Add an element to the beginning of the list    def addFirst(self, e):        newNode = Node(e) # Create a new node        newNode.next = self.__head # link...

  • PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree,...

    PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree, the BinaryTreeclass, he wrote test code and is having problems understanding the error. Locate his problem and explain how you would fix it. class Node(object): def __init__(self, data=None): self.data = data def __str__(self): return "NODE: " + str(self.data)    class Tree(object): def __init__(self): self.root_node = None self.size = 0    def __len__(self): return self.size    def add(self, data): raise NotImplementedError("Add method not implemented.")    def inorder_traversal(self): raise NotImplementedError("inorder_traversal...

  • from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked...

    from __future__ import annotations from typing import Any, Optional class _Node: """A node in a linked list. Note that this is considered a "private class", one which is only meant to be used in this module by the LinkedList class, but not by client code. === Attributes === item: The data stored in this node. next: The next node in the list, or None if there are no more nodes. """ item: Any next: Optional[_Node] def __init__(self, item: Any) ->...

  • import math ''' Finish the code below as described. Use the completed class Square as an...

    import math ''' Finish the code below as described. Use the completed class Square as an example. ''' class Square: ''' Each Square has a width and can calculate its area, its perimeter, and return a string representation of itself. ''' def __init__(self, width): ''' (float) -> None Create a new Square with the given width. ''' self.width = width def get_area(self): ''' () -> float Return this square's area. ''' return self.width*self.width def get_perimeter(self): ''' () -> float Return...

  • Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list...

    Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list using node. (from chapter 24). Your code should utilize the selectionsort method from attached and create another method call selectionsort in linkedlist class. To demonstrate the usage of the selection sort method, you should manually create a link list of random integer (say of 5 numbers), and you need to demonstrate the use the selection sort to sorted the link list. Please submit your...

  • class Leibniz:    def __init__(self):        self.total=0               def calculate_pi(self,n):       ...

    class Leibniz:    def __init__(self):        self.total=0               def calculate_pi(self,n):        try:            self.total, sign = 0, 1            for i in range(n):                term = 1 / (2 * i + 1)                self.total += term * sign                sign *= -1                self.total *= 4            return self.total        except Exception as e:   ...

  • ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value):...

    ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def __repr__(self): return "{}".format(self.value) def __init__(self): self.top = None def shuffle(self): card_list = 4 * [x for x in range(2, 12)] + 12 * [10] random.shuffle(card_list) self.top = None for card in card_list: new_card = self.Card(card) new_card.next = self.top self.top = new_card def __repr__(self): curr = self.top out = "" card_list = [] while curr is not None:...

  • # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sV...

    # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild())+')' return sVal #### Functions and classes to help with Homework 6 #### You should not make any changes to the functions and classes in this file, #### but you should understand what they do and how they work. import sys ### This function will be re-defined in hw6.py. However, it is needed for ExpTree, ###...

  • Need Help! in English Can not get program to run  plz include detailed steps as comments of...

    Need Help! in English Can not get program to run  plz include detailed steps as comments of what i did not do what I have done so far class Point: def __init__(self): self._x = 0 self._y = 0 def getX(self): return self._x def setX(self, val): self._x = val def getY(self): return self._y def setY(self, val): self._y = val def __init__(self, initX = 0, initY = 0): self._x = initX self._y = initY def __str__(self): return '<'+str(self._x)+', '+str(self._y)+ '>' def scale(self, factor):...

  • How do you do this? class Event: """A new calendar event.""" def __init__(self, start_time: int, end_time:...

    How do you do this? class Event: """A new calendar event.""" def __init__(self, start_time: int, end_time: int, event_name: str) -> None: """Initialize a new event that starts at start_time, ends at end_time, and is named name. Precondition: 0 <= start_time < end_time <= 23 >>> e = Event(12, 13, 'Lunch') >>> e.start_time 12 >>> e.end_time 13 >>> e.name 'Lunch' """    self.start_time = start_time self.end_time = end_time self.name = event_name def __str__(self) -> str: """Return a string representation of this...

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