#!/usr/bin/env python3
import array # provides access to primitive arrays
def copy(self):
'''
this behaves like a copy
constructor that makes a detailed copy of self
NOTE: the capacity of
the copy matches the elements in self unless
the bag is empty, in which case it should be at least 1
Postcondition:
x.copy() is x returns false
type(x.copy()) == type(x)
x.copy() == x
internal capacity of copy is just enough to hold self.__used
items
Returns: a new DoubleArrayBag object that
contains a copy of all
the elements in self and its capacity is equal to
number of elements in this bag
Raises: MemoryError if dynamic memory
allocation fails
'''
copyCapacity =
self.__used
if (copyCapacity <
1):
copyCapacity = 1
newBag =
DoubleArrayBag(copyCapacity)
# STUDENT
IMPLEMENTATION GOES HERE
# must copy
all elements from self.__data into
# the newBag
and update any attributes to
# adhere to
the class invariant
return newBag
# end copy
def ensureCapacity(self, newCapacity: int):
'''
potentially increase
capacity of the bag
Precondition: newCapacity must be an integer and > 0
Postcondition: The
bag's capacity is at least newCapacity.
If the capacity was already at or greater than
newCapacity, then the capacity is left
unchanged.
Args: newCapacity (int)
Raises: TypeError
if newCapacity is not an integer
ValueError if newCapacity not > 1
MemoryError if dynamic memory exhausted
'''
if not
isinstance(newCapacity, int):
raise TypeError("newCapacity must be an integer")
if not newCapacity >=
1:
raise ValueError("newCapacity must be >= 1")
if len(self.__data)
< newCapacity:
pass
# STUDENT WORK GOES HERE
# create a new, bigger array with the capacity
# newCapacity, copy all the elements from
# self.__data into the new array, and then
# update the reference self.__data to refer
# to the new array
# end if
# end ensureCapacity
#!/usr/bin/env python3
import array # provides access to primitive arrays
def copy(self):
'''
this behaves like a copy constructor that makes a detailed copy of
self
NOTE: the capacity of the copy matches the elements in self
unless
the bag is empty, in which case it should be at least 1
Postcondition:
x.copy() is x returns false
type(x.copy()) == type(x)
x.copy() == x
internal capacity of copy is just enough to hold
self.__used
items
Returns: a new DoubleArrayBag object that contains a copy of
all
the elements in self and its capacity is equal to
number of elements in this bag
Raises: MemoryError if dynamic memory allocation fails
'''
copyCapacity = self.__used
if (copyCapacity < 1):
copyCapacity = 1
newBag = DoubleArrayBag(copyCapacity)
# STUDENT IMPLEMENTATION GOES HERE
# must copy all elements from self.__data into
# the newBag and update any attributes to
# adhere to the class invariant
n = len(self.__data) # calculate length of the data
for i in range(n):
newBag[i] = self.__data[i] # copy all elements into newBag
newBag.capacity = n # update the newBag capacity
return newBag
# end copy
def ensureCapacity(self, newCapacity: int):
'''
potentially increase capacity of the bag
Precondition: newCapacity must be an integer and > 0
Postcondition: The bag's capacity is at least newCapacity.
If the capacity was already at or greater than
newCapacity, then the capacity is left
unchanged.
Args: newCapacity (int)
Raises: TypeError if newCapacity is not an integer
ValueError if newCapacity not > 1
MemoryError if dynamic memory exhausted
'''
if not isinstance(newCapacity, int):
raise TypeError("newCapacity must be an integer")
if not newCapacity >= 1:
raise ValueError("newCapacity must be >= 1")
if len(self.__data) < newCapacity:
pass
# STUDENT WORK GOES HERE
# create a new, bigger array with the capacity
# newCapacity, copy all the elements from
# self.__data into the new array, and then
# update the reference self.__data to refer
# to the new array
# end if
new_array = (newCapacity * ctypes.py_object)() # create a bigger
array with newCapacity
n = len(self.__data) # calculate the length of data
for k in range(n):
new_array[k] = self.__data[k] # copy all the elements into the new
array
self.__data = new_array # update the reference self.__data
self.capacity = newCapacity # update the capacity
# end ensureCapacity
COMMENT DOWN FOR ANY QUERY
PLEASE GIVE A THUMBS UP ALSO
#!/usr/bin/env python3 import array # provides access to primitive arrays def copy(self): ''' ...
Below is the Graph file that
needs to be modified(using Python3) :
#!/usr/bin/python3
# Simple Vertex class
class Vertex:
""" Lightweight vertex structure for a graph.
Vertices can have the following labels:
UNEXPLORED
VISITED
Assuming the element of a vertex is string type
"""
__slots__ = '_element', '_label'
def __init__(self, element, label="UNEXPLORED"):
""" Constructor. """
self._element = element
self._label = label
def element(self):
""" Return element associated with this vertex. """
return self._element
def getLabel(self):
""" Get label assigned to...
1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available // copy original's array member into this new array { // Please complete the function here } else { cerr << "*Inadequate memory to allocate...
C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...
1.Suppose that the goop function from the previous question changes the value of z[1]. Does this change effect the value of the actual argument? A. Yes B. No 2.Here is a function declaration: void goo(int* x) { *x = 1; } Suppose that a is an int* variable pointing to some integer, and *a is equal to zero. What is printed if you print *a after the function call goo(a)? A. 0 B. 1 C. address of a D. address...
Multiple Choice Multiple Choice Section 4.1 Pointers and Dynamic Memory Consider the following statements: int *p; int i; int k; i = 42; k = i; p = &i; After these statements, which of the following statements will change the value of i to 75? A. k = 75; B. *k = 75; C. p = 75; D. *p = 75; E. Two or more of the answers will change i to 75. Consider the following statements: int i =...
Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...
This assignment is comprised of 3 parts: All files needed are located at the end of the directions. Part 1: Implementation of Dynamic Array, Stack, and Bag First, complete the Worksheets 14 (Dynamic Array), 15 (Dynamic Array Amortized Execution Time Analysis), 16 (Dynamic Array Stack), and 21 (Dynamic Array Bag). These worksheets will get you started on the implementations, but you will NOT turn them in. Do Not Worry about these, they are completed. Next, complete the dynamic array and...
Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int # define TYPE_SIZE sizeof(int) # endif # ifndef LT # define LT(A, B) ((A) < (B)) # endif # ifndef EQ # define EQ(A, B) ((A) == (B)) # endif typedef struct DynArr DynArr; /* Dynamic Array Functions */ void initDynArr(DynArr *v, int capacity); DynArr *newDynArr(int cap); void freeDynArr(DynArr *v); void deleteDynArr(DynArr *v); int sizeDynArr(DynArr *v); void addDynArr(DynArr *v, TYPE val); TYPE getDynArr(DynArr *v, int...
8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....
This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...