Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON: ###CAN YOU HELP CODE THIS PYTHON ASSIGNMENT### def copy(self) -> LongArraySequence: ''' this is the copy constructor that makes an exact copy of self

PYTHON: ###CAN YOU HELP CODE THIS PYTHON ASSIGNMENT###

def copy(self) -> LongArraySequence: ''' this is the copy constructor that makes an exact copy of self and returns the copy with exactly the capacity to hold the all the elements of this object -- thus, capacity of copy may be smaller than capacity of self

Postcondition: creates a new sequence with the capacity to hold all the defined elements of this sequence

Return: a new LongArraySequence object that contains a copy of all the elements in self

Raises: MemoryError if dynamic memory allocation fails ''' newBag = LongArraySequence(1)

# STUDENT IMPLEMENTATION GOES HERE # all attributes of self must be copied into new bag

return newBag # end copy

def __eq__(self, other: LongArraySequence) -> int: ''' compare self with an other LongArraySequence -- this means that collection of items in self are pairwise equal to items in other and that the cursors of self and other are equal

Precondition: other must be an instance of LongArraySequence

Postcondition: if a == b, then b == a a == a is True a == None is False if a == b, then hash(a) == hash(b)

Args: other must be an instance of LongArraySequence

Return: boolean whether self is equal to other ''' isEqual = True # assume equality

if other is None: return False # end if

if not isinstance(other, self.__class__): return False # end if

hashTest = hash(self) == hash(other) # STUDENT IMPLEMENTATION GOES HERE # must ensure that all attributes of self and other # match including testing whether elements in the # data arrays are pair-wise equal

return isEqual

def isCurrent(self) -> int: ''' Return: a boolean indicating whether the container's internal cursor is currently defined ''' # STUDENT IMPLEMENTATION GOES HERE # replace below bogus return with correct logic that matches # class invariant

return False

def __add__(self, other) -> LongArraySequence: ''' this method overloads the + operator for concatenation meaning that this method combines all the elements in this sequence and all the elements in the other sequence by placing them in a new sequence and maintains the relative ordering of the two sequences

Precondition: other must be an instance of LongArraySequence otherwise TypeError is raised

Postcondition: a new LongArraySequence is created containing copies of all the elements in this sequence plus all the elements in other. The new sequence's capacity is exactly equal to the number of elements in both sequences. The enhanced sequence's cursor should be undefined.

Args: other (LongArraySequence)

Returns: a new sequence that is the combination of all elements in this sequence and the other sequence

Raises: TypeError if other not an instance of LongArraySequence MemoryError if dynamic memory is exhausted ''' if not isinstance(other, self.__class__): raise TypeError("argument to + NOT another LongArraySequence")

newCapacity = self.__used + other.__used if newCapacity < 1: newSequence = LongArraySequence(1) else: newSequence = LongArraySequence(newCapacity) # STUDENT IMPLEMENTATION GOES HERE

return newSequence # end __add__

def addBefore(self, newEntry: ElementType) -> None: ''' adds newEntry to the LongArraySequence if capacity available; otherwise, capacity is expanded to accommodate addition -- newEntry placed in position immediately before the cursor's position or at beginning of sequence if there is no valid current position of cursor

Precondition: newEntry must be an integer; otherwise, method raises a TypeError exception.

Postcondition: If isCurrent() is True then newEntry is placed in sequence just before the entry at the cursor's position. If not isCurrent(), then newEntry is placed at the beginning of the sequence. The internal cursor remains at same relative position and, thus, is "pointing" to the newEntry.

Args: newEntry (int)

Raises: TypeError is newElement not an int or long and MemoryError if not enough dynamic memory available to accommodate expansion ''' if not(isinstance(newEntry, ElementType)): raise TypeError("newEntry must be an integer or long") # we must first check to see if we have enough room to add newEntry # and if not expand capacity by doubling capacity at each expansion # print("In addBefore before adding: " + str(self.__data) ) if len(self.__data) == self.__used: self.ensureCapacity(self.__used * 2)

# STUDENT IMPLEMENTATION GOES HERE

# print("In addBefore after adding: " + str(self.__data) ) # end addBefore

def addAfter(self, newEntry: ElementType) -> None: ''' adds newEntry to the LongArraySequence if capacity available; otherwise, capacity is expanded to accommodate addition -- newEntry placed in position immediately after the cursor's position or at end of sequence if there is no valid current position of cursor

Precondition: newEntry must be an integer or long; otherwise, method raises a TypeError exception.

Postcondition: If isCurrent() is True then newEntry is placed in sequence just after the entry at the cursor's position. If not isCurrent(), then newEntry is placed at the end of the sequence. The internal cursor "points" to this newEntry. Otherwise, relative ordering of elements remains unchanged except for the newElement being inserted into the sequence.

Args: newEntry (int)

Raises: MemoryError if not enough dynamic memory available to accommodate expansion ''' if not(isinstance(newEntry, ElementType)): raise TypeError("newEntry must be an integer or long") # we must first check to see if we have enough room to add newEntry # and if not expand capacity by doubling capacity at each expansion # print("In addAfter before adding ", str(self.__data) ) if len(self.__data) == self.__used: self.ensureCapacity(self.__used * 2)

# STUDENT IMPLEMENTATION GOES HERE

# print("In addAfter after adding ", str(self.__data) ) # end add

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

1st Edition

1597496251, 978-1597496254

More Books

Students also viewed these Databases questions

Question

5. Give examples of pricing strategy in action.

Answered: 1 week ago