logo Hurry, Grab up to 30% discount on the entire course
Order Now logo

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Alejandro PerryyTechnical writing
(5/5)

784 Answers

Hire Me
expert
Andrei KrushenkoMarketing
(5/5)

687 Answers

Hire Me
expert
Jon CulshawComputer science
(5/5)

720 Answers

Hire Me
expert
Jaydon KeySocial sciences
(5/5)

918 Answers

Hire Me
Python Programming
(5/5)

Implement a Dynamic Array class by completing the skeleton code provided in the file dynamic_array.py.

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Part 1 - Summary and Specific Instructions

1. Implement a Dynamic Array class by completing the skeleton code provided in the file dynamic_array.py. The DynamicArray class will use a StaticArray object as its underlying data storage container and will provide many methods similar to those we are used to when working with Python lists. Once completed, your implementation will include the following methods:

resize() append()

insert_at_index() remove_at_index() slice()

merge() map() filter() reduce()

* Several class methods, like is_empty(), length(), get_at_index(), and

set_at_index() have been pre-written for you.

2. We will test your implementation with different types of objects, not just integers. We guarantee that all such objects will have correct implementations of methods

    eq   ,    lt   ,    gt   ,    ge   ,    le   , and    str   .

3. The number of objects stored in the array at any given time will be between 0 and 1,000,000 inclusive. An array must allow for the storage of duplicate objects.

4. Variables in the DynamicArray class are not marked as private. For this portion of the assignment, you are allowed to access and change their values directly. Note that getter and setter methods have already been provided for you.

5. RESTRICTIONS: You are not allowed to use ANY built-in Python data structures and/or their methods (lists, dictionaries, etc.). You must solve this portion of the assignment by importing and using objects of the StaticArray class (prewritten for you) and using class methods to write your solution.

 

You are also not allowed to directly access any variables of the StaticArray class (like self.data._data[]). All work must be done by using only StaticArray class methods.

 resize(self, new_capacity: int) -> None:

This method changes the capacity of the underlying storage for the array elements. It does not change the values or the order of any elements currently stored in the dynamic array.

It is intended to be an “internal” method of the Dynamic Array class, called by other class methods, such as append(), remove_at_index(), or insert_at_index(), to manage the capacity of the underlying storage data structure.

The method should only accept positive integers for new_capacity. Additionally, new_capacity cannot be smaller than the number of elements currently stored in the dynamic array (which is tracked by the self.size variable). If new_capacity is not a positive integer or if new_capacity < self.size, this method should not do any work and should just exit.

Example #1:

da = DynamicArray()

print(da.size, da.capacity, da.data) da.resize(8)

print(da.size, da.capacity, da.data) da.resize(2)

print(da.size, da.capacity, da.data) da.resize(0)

print(da.size, da.capacity, da.data)

 

Output:

0 4 STAT_ARR Size: 4 [None, None, None, None]

0 8 STAT_ARR Size: 8 [None, None, None, None, None, None, None, None]

0 2 STAT_ARR Size: 2 [None, None]

0 2 STAT_ARR Size: 2 [None, None]

 

NOTE: Example 2 below will not work properly until after the append() method is implemented.

Example #2:

da = DynamicArray([1, 2, 3, 4, 5, 6, 7, 8]) print(da)

da.resize(20) print(da) da.resize(4) print(da)

Output:

DYN_ARR Size/Cap: 8/8 [1, 2, 3, 4, 5, 6, 7, 8]

DYN_ARR Size/Cap: 8/20 [1, 2, 3, 4, 5, 6, 7, 8]

DYN_ARR Size/Cap: 8/20 [1, 2, 3, 4, 5, 6, 7, 8]

 append(self, value: object) -> None:

This method adds a new value at the end of the dynamic array.

If the internal storage associated with the dynamic array is already full, you need to DOUBLE its capacity before adding a new value.

Example #1:

da = DynamicArray()

print(da.size, da.capacity, da.data) da.append(1)

print(da.size, da.capacity, da.data) print(da)

Output:

0 4 STAT_ARR Size: 4 [None, None, None, None]

1 4 STAT_ARR Size: 4 [1, None, None, None] DYN_ARR Size/Cap: 1/4 [1]

Example #2:

da = DynamicArray() for i in range(9):

da.append(i + 101) print(da)

Output:

DYN_ARR

Size/Cap:

1/4

[101]

DYN_ARR Size/Cap: 2/4 [101, 102]

DYN_ARR Size/Cap: 3/4 [101, 102, 103]

DYN_ARR Size/Cap: 4/4 [101, 102, 103, 104]

DYN_ARR Size/Cap: 5/8 [101, 102, 103, 104, 105]

DYN_ARR Size/Cap: 6/8 [101, 102, 103, 104, 105, 106]

DYN_ARR Size/Cap: 7/8 [101, 102, 103, 104, 105, 106, 107]

DYN_ARR Size/Cap: 8/8 [101, 102, 103, 104, 105, 106, 107, 108]

DYN_ARR Size/Cap: 9/16 [101, 102, 103, 104, 105, 106, 107, 108, 109]

Example #3:

da = DynamicArray() for i in range(600):

da.append(i) print(da.size) print(da.capacity)

Output:

600

1024

 

insert_at_index(self, index: int, value: object) -> None:

This method adds a new value at the specified index position in the dynamic array. Index 0 refers to the beginning of the array. If the provided index is invalid, the method raises a custom “DynamicArrayException”. Code for the exception is provided in the skeleton file. If the array contains N elements, valid indices for this method are [0, N] inclusive.

If the internal storage associated with the dynamic array is already full, you need to DOUBLE its capacity before adding a new value.

Example #1:

da = DynamicArray([100]) print(da) da.insert_at_index(0, 200)

da.insert_at_index(0, 300)

da.insert_at_index(0, 400) print(da) da.insert_at_index(3, 500) print(da) da.insert_at_index(1, 600) print(da)

Output:

DYN_ARR Size/Cap: 1/4 [100]

DYN_ARR Size/Cap: 4/4 [400, 300, 200, 100]

DYN_ARR Size/Cap: 5/8 [400, 300, 200, 500, 100]

DYN_ARR Size/Cap: 6/8 [400, 600, 300, 200, 500, 100]

Example #2:

da = DynamicArray() try:

da.insert_at_index(-1, 100) except Exception as e:

print("Exception raised:", type(e)) da.insert_at_index(0, 200)

try:

da.insert_at_index(2, 300) except Exception as e:

print("Exception raised:", type(e)) print(da)

Output:

Exception raised: <class ' main .DynamicArrayException'> Exception raised: <class ' main .DynamicArrayException'> DYN_ARR Size/Cap: 1/4 [200]

 Example #3:

da = DynamicArray() for i in range(1, 10):

index, value = i - 4, i * 10 try:

da.insert_at_index(index, value) except Exception as e:

print("Cannot insert value", value, "at index", index) print(da)

Output:

Cannot insert value 10 at index -3 Cannot insert value 20 at index -2 Cannot insert value 30 at index -1

DYN_AR Size/Cap: 6/8 [40, 50, 60, 70, 80, 90] 

(5/5)
Attachments:

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme