Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import array # provides access to primitive arrays # Class Invariant: # 1. The number of elements in the bag is stored in the #

import array # provides access to primitive arrays

# Class Invariant: # 1. The number of elements in the bag is stored in the # instance variable __used which can be no greater than # len(__data) # 2. For an empty bag, we do not care what is stored in # any element of __data. For a non-empty bag, the elements # are stored at __data[0] through __data[used-1] # and we don't care what is stored in any elements beyond # that. # 3. The array __data can store only double items. Attempts # to store items of other types will cause a run-time error # to be raised.

class DoubleArrayBag(object): ''' An DoubleArrayBag is a collection of double numbers

Limitations: 1. The capacity of an DoubleArrayBag is set to a fixed capacity of MAX_CAPACITY items. 2. Because of the linear algorithms used in this implementation. large DoubleArrayBags will have poor performance.

''' MAX_CAPACITY = 4000

def __init__(self): ''' Precondition: initialCapacity must be >= 0

Postcondition: The bag has been initialized and is in a well-defined, empty state with MAX_CAPACITY elements for storage

Raises: MemoryError if not able to allocate space to accommodate MAX_CAPACITY items ''' self.__used = 0 # Below allocates an array of doubles. Elements of the array are # accessed with indices in range 0 <= index <= MAX_CAPACITY -1 # In addition, only doubles may be stored in the array. Disciplined # use of this array will avoid the methods append, remove, extend, etc. # which are very expensive in run-time performance. self.__data = array.array('d', range(0, DoubleArrayBag.MAX_CAPACITY)) # end __init__

def copy(self): ''' this behaves like a copy constructor that makes a detailed copy of self

Postcondition: x.copy() is x returns false type(x.copy()) == type(x) x.copy() == x

Returns: a new DoubleArrayBag object that contains a copy of all the elements in self

Raises: MemoryError if dynamic memory allocation fails ''' newBag = DoubleArrayBag() newBag.__data = array.array('d', range(0, DoubleArrayBag.MAX_CAPACITY)) # OK to copy immutable object references; thus, next logic should copy # self.__used items from self.__data to newBag.__data and must update # newBags.__used

# STUDENT CODE HERE TO ACCOMPLISH ABOVE

return newBag # end copy

def size(self) -> int: ''' Returns: an integer representing numbers of items in the bag. ''' return self.__used # end size

def __eq__(self, other): ''' compare self with an other DoubleArrayBag

Precondition: other must be an instance of DoubleArrayBag

Postconditions: x == x is True if x == y then y == x x == None is False

Args: other must be an instance of DoubleArrayBag

Returns: boolean whether self is equal to other in number of items in the bag and in the location and value of the items in the bag

NOTE: if the value of other is None, return False ''' if other is None: return False # end if

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

if self is other: return True # end if

if self.__used != other.__used: return False # end if

index = 0 isEqual = True # assume equality while isEqual and index < self.__used: '''student should remove this line''' pass

# Code that compares self.__data[index] with # other.__data[index] and sets isEqual to False

# STUDENT CODE HERE TO ACCOMPLISH ABOVE

HOW TO CODE THIS ON PYTHON?PLEASE EXPLAIN THE LOGIC BEHIND IT AND IF POSSIBLE HOW AND WHY IT IS CODED THAT WAY ? #BEGINNER AT PROGRAMMING. NO EXPERIENCE

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

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

Describe ERP and how it can create efficiency within a business

Answered: 1 week ago

Question

What are some sources of ethical guidance?

Answered: 1 week ago